Last active
April 3, 2020 08:19
-
-
Save bnbaek/d1b10d2ce386993e16299929d77f086b to your computer and use it in GitHub Desktop.
enum-common
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.barogo.zoo.core.repository.test.common; | |
import javax.persistence.AttributeConverter; | |
import org.apache.commons.lang3.StringUtils; | |
/** | |
* Created by BNBAEK | |
* Package : io.barogo.zoo.core.repository.test | |
* User: dean | |
* Date: 2020/04/03 | |
* Time: 3:48 오후 | |
*/ | |
public class AbstractLegacyEumAttributeConverter<E extends Enum<E> & LegacyCommonType> implements AttributeConverter<E, String> { | |
private Class<E> targetEnumClass; | |
private boolean nullable; | |
private String enumName; | |
public AbstractLegacyEumAttributeConverter(boolean nullable, String enumName) { | |
this.nullable = nullable; | |
this.enumName = enumName; | |
} | |
/** | |
* Converts the value stored in the entity attribute into the | |
* data representation to be stored in the database. | |
* | |
* @param attribute the entity attribute value to be converted | |
* @return the converted data to be stored in the database | |
* column | |
*/ | |
@Override | |
public String convertToDatabaseColumn(E attribute) { | |
if (!nullable && attribute == null) { | |
throw new IllegalArgumentException(String.format("%s(은)는 NULL로 저장할 수 없습니다", enumName)); | |
} | |
return LegacyEnumValueConvertUtils.toLegacyCode(attribute); | |
} | |
@Override | |
public E convertToEntityAttribute(String dbData) { | |
if (!nullable && StringUtils.isBlank(dbData)) { | |
throw new IllegalArgumentException(String.format("%s(이)가 DB에 NULL로 저장할 수 없습니다", enumName, dbData)); | |
} | |
return LegacyEnumValueConvertUtils.ofLegacyCode(targetEnumClass, dbData); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.barogo.zoo.core.repository.test.common; | |
public interface LegacyCommonType { | |
String getLegacyCode(); | |
String getDesc(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.barogo.zoo.core.repository.test.common; | |
import java.util.EnumSet; | |
import lombok.AccessLevel; | |
import lombok.NoArgsConstructor; | |
import org.apache.commons.lang3.StringUtils; | |
@NoArgsConstructor(access = AccessLevel.PRIVATE) | |
public class LegacyEnumValueConvertUtils { | |
public static <T extends Enum<T> & LegacyCommonType> T ofLegacyCode(Class<T> enumClass, String legacyCode) { | |
if (StringUtils.isBlank(legacyCode)) { | |
return null; | |
} | |
return EnumSet.allOf(enumClass).stream() | |
.filter(v -> v.getLegacyCode().equals(legacyCode)) | |
.findAny() | |
.orElseThrow(() -> new RuntimeException(String.format("enum=[%s], legacyCode=[%s]가 존재하지 않습니다.", enumClass.getName(), legacyCode))); | |
} | |
public static <T extends Enum<T> & LegacyCommonType> String toLegacyCode(T enumValue) { | |
if (enumValue == null) { | |
return ""; | |
} | |
return enumValue.getLegacyCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment