PropertyMeta.java

  1. package com.github.mygreen.sqlmapper.core.meta;

  2. import java.lang.reflect.Modifier;
  3. import java.util.Collection;
  4. import java.util.NoSuchElementException;
  5. import java.util.Optional;

  6. import org.springframework.util.LinkedCaseInsensitiveMap;

  7. import com.github.mygreen.sqlmapper.core.annotation.CreatedAt;
  8. import com.github.mygreen.sqlmapper.core.annotation.CreatedBy;
  9. import com.github.mygreen.sqlmapper.core.annotation.EmbeddedId;
  10. import com.github.mygreen.sqlmapper.core.annotation.GeneratedValue;
  11. import com.github.mygreen.sqlmapper.core.annotation.Id;
  12. import com.github.mygreen.sqlmapper.core.annotation.Lob;
  13. import com.github.mygreen.sqlmapper.core.annotation.Transient;
  14. import com.github.mygreen.sqlmapper.core.annotation.UpdatedAt;
  15. import com.github.mygreen.sqlmapper.core.annotation.UpdatedBy;
  16. import com.github.mygreen.sqlmapper.core.annotation.Version;
  17. import com.github.mygreen.sqlmapper.core.id.IdGenerationContext;
  18. import com.github.mygreen.sqlmapper.core.id.IdGenerator;
  19. import com.github.mygreen.sqlmapper.core.type.ValueType;

  20. import lombok.Getter;
  21. import lombok.NonNull;
  22. import lombok.Setter;

  23. /**
  24.  * プロパティのメタ情報です。
  25.  *
  26.  * @author T.TSUCHIE
  27.  * @version 0.3
  28.  *
  29.  */
  30. public class PropertyMeta extends PropertyBase {

  31.     /**
  32.      * 埋め込み型の主キーの子プロパティかどうか。
  33.      */
  34.     @Setter
  35.     private boolean embeddedableId = false;

  36.     /**
  37.      * 埋め込みプロパティの情報
  38.      * <p>key=プロパティ名</p>
  39.      */
  40.     private LinkedCaseInsensitiveMap<PropertyMeta> embeddedablePropertyMetaMap = new LinkedCaseInsensitiveMap<>();

  41.     /**
  42.      * 埋め込みのときの親のプロパティ情報
  43.      */
  44.     private Optional<PropertyMeta> parent = Optional.empty();

  45.     /**
  46.      * カラムのメタ情報
  47.      */
  48.     @Getter
  49.     @Setter
  50.     private ColumnMeta columnMeta;

  51.     /**
  52.      * 値の変換処理
  53.      */
  54.     @Getter
  55.     @Setter
  56.     private ValueType<?> valueType;

  57.     /**
  58.      * IDの生成タイプ
  59.      */
  60.     @Getter
  61.     private Optional<GeneratedValue.GenerationType> idGenerationType = Optional.empty();

  62.     /**
  63.      * IDの生成処理
  64.      */
  65.     @Getter
  66.     private Optional<IdGenerator> idGenerator = Optional.empty();

  67.     /**
  68.      * 生成対象の識別子の情報。
  69.      * <p>ID生成時に渡す際の情報として使用するので、効率化のためにここで事前に作成して保持しておく。
  70.      */
  71.     @Getter
  72.     private Optional<IdGenerationContext> idGenerationContext = Optional.empty();

  73.     /**
  74.      * プロパティのインスタンス情報を作成します。
  75.      * @param name プロパティ名
  76.      * @param propertyType プロパティのクラスタイプ
  77.      */
  78.     public PropertyMeta(String name, Class<?> propertyType) {
  79.         super(name, propertyType);
  80.     }

  81.     /**
  82.      * カラム用のプロパティかどうか判定する。
  83.      * @return カラム情報を持つときtrueを返す。
  84.      */
  85.     public boolean isColumn() {
  86.         return columnMeta != null;
  87.     }

  88.     /**
  89.      * 埋め込みプロパティ情報を追加する
  90.      * @param embeddedablePropertyMeta 埋め込みプロパティ
  91.      */
  92.     public void addEmbeddedablePropertyMeta(@NonNull PropertyMeta embeddedablePropertyMeta) {

  93.         embeddedablePropertyMeta.parent = Optional.of(this);

  94.         // 親が埋め込み主キーの時
  95.         if(hasAnnotation(EmbeddedId.class) && !isTransient()) {
  96.             embeddedablePropertyMeta.embeddedableId = true;
  97.         }

  98.         this.embeddedablePropertyMetaMap.put(embeddedablePropertyMeta.getName(), embeddedablePropertyMeta);

  99.     }

  100.     /**
  101.      * 埋め込み用のクラスのプロパティかどか判定する。
  102.      * @return 埋め込み用のクラスのプロパティの場合trueを変す。
  103.      */
  104.     public boolean hasParent() {
  105.         return parent.isPresent();
  106.     }

  107.     /**
  108.      * 埋め込み用のクラスのプロパティの親情報を取得する。
  109.      * @return 親情のプロパティ情報
  110.      * @throws NoSuchElementException 親が存在しないときにスローされます。
  111.      */
  112.     public PropertyMeta getParent() {
  113.         return parent.get();
  114.     }

  115.     /**
  116.      * 埋め込みプロパティの一覧を取得する。
  117.      * @return
  118.      */
  119.     public Collection<PropertyMeta> getEmbeddedablePopertyMetaList() {
  120.         return embeddedablePropertyMetaMap.values();
  121.     }

  122.     /**
  123.      * 主キーかどうか判定する。
  124.      * <p>アノテーション {@link Id}を付与されているかどうかで判定する。</p>
  125.      * <p>また、親が{@link EmbeddedId} を付与された埋め込みIDの場合は、子も主キーとなるためtrueを返す。</p>
  126.      *
  127.      * @return 主キーの場合は {@literal true}  を返す。
  128.      */
  129.     public boolean isId() {

  130.         return hasAnnotation(Id.class) || embeddedableId;
  131.     }

  132.     /**
  133.      * 埋め込み用のプロパティかどうか判定する。
  134.      * <p>埋め込みプロパティの子の場合は、falseを返す。
  135.      * @return 埋め込みの場合trueを返す。
  136.      */
  137.     public boolean isEmbedded() {
  138.         return hasAnnotation(EmbeddedId.class);
  139.     }

  140.     /**
  141.      * 識別子の生成タイプを設定する。
  142.      * @param generationType IDの生成タイプ
  143.      */
  144.     public void setIdGeneratonType(GeneratedValue.GenerationType generationType) {
  145.         this.idGenerationType = Optional.ofNullable(generationType);
  146.     }

  147.     /**
  148.      * 識別子の生成処理を設定する。
  149.      * @param idGenerator 識別子の生成処理。
  150.      */
  151.     public void setIdGenerator(IdGenerator idGenerator) {
  152.         this.idGenerator = Optional.ofNullable(idGenerator);
  153.     }

  154.     /**
  155.      * 生成対象の識別子の情報を設定する。
  156.      * @param idGenerationContext 生成対象の識別子の情報
  157.      */
  158.     public void setIdGenerationContext(IdGenerationContext idGenerationContext) {
  159.         this.idGenerationContext = Optional.ofNullable(idGenerationContext);
  160.     }

  161.     /**
  162.      * 永続化対象外かどうか判定する。
  163.      * <p>永続化対象外とは、アノテーション {@link Transient}が付与されているか、
  164.      * または、フィールドに修飾子 {@literal transient} が付与されているかどうかで判定します。
  165.      * @return 永続化対象外のとき {@literal true} を返す。
  166.      */
  167.     public boolean isTransient() {
  168.         return hasAnnotation(Transient.class)
  169.                 || field.map(f -> Modifier.isTransient(f.getModifiers())).orElse(false);
  170.     }

  171.     /**
  172.      * バージョンキーかどうか判定する。
  173.      * @return バージョンキーのとき {@literal true} を返す。
  174.      */
  175.     public boolean isVersion() {
  176.         return hasAnnotation(Version.class);
  177.     }

  178.     /**
  179.      * SQLのカラムがLOB(CLOB/BLOC)かどうか判定する。
  180.      * <p>アノテーション {@link Lob} が付与されているかで判定する。</p>
  181.      * @return LOBの場合はtrueを返す。
  182.      */
  183.     public boolean isLob() {
  184.         return hasAnnotation(Lob.class);
  185.     }

  186.     /**
  187.      * 作成日時用のプロパティがかどうか判定する。
  188.      * @return 作成日時用のプロパティのとき {@literal true} を返す。
  189.      */
  190.     public boolean isCreatedAt() {
  191.         return hasAnnotation(CreatedAt.class);
  192.     }

  193.     /**
  194.      * 作成者用のプロパティがかどうか判定する。
  195.      * @return 作成者用のプロパティのとき {@literal true} を返す。
  196.      */
  197.     public boolean isCreatedBy() {
  198.         return hasAnnotation(CreatedBy.class);
  199.     }

  200.     /**
  201.      * 修正日時用のプロパティがかどうか判定する。
  202.      * @return 修正日時用のプロパティのとき {@literal true} を返す。
  203.      */
  204.     public boolean isUpdatedAt() {
  205.         return hasAnnotation(UpdatedAt.class);
  206.     }

  207.     /**
  208.      * 修正者用のプロパティがかどうか判定する。
  209.      * @return 修正者用のプロパティのとき {@literal true} を返す。
  210.      */
  211.     public boolean isUpdatedBy() {
  212.         return hasAnnotation(UpdatedBy.class);
  213.     }


  214. }