001package io.ebeaninternal.dbmigration.model;
002
003import io.ebean.annotation.IdentityGenerated;
004import io.ebean.config.dbplatform.IdType;
005import io.ebeaninternal.dbmigration.migration.CreateTable;
006import io.ebeaninternal.dbmigration.migration.IdentityType;
007import io.ebeaninternal.server.deploy.IdentityMode;
008
009import java.math.BigInteger;
010
011/**
012 * Helper to convert between IdentityMode and CreateTable
013 */
014public class MTableIdentity {
015
016  /**
017   * Return the IdentityMode from CreateTable.
018   */
019  public static IdentityMode fromCreateTable(CreateTable createTable) {
020
021    IdType type = fromType(createTable.getIdentityType());
022    IdentityGenerated generated = fromGenerated(createTable.getIdentityGenerated());
023    int start = toInt(createTable.getIdentityStart(), createTable.getSequenceInitial());
024    int increment = toInt(createTable.getIdentityIncrement(), createTable.getSequenceAllocate());
025    int cache = toInt(createTable.getIdentityCache(), null);
026    String seqName = createTable.getSequenceName();
027
028    return new IdentityMode(type, generated, start, increment, cache, seqName);
029  }
030
031  private static IdentityGenerated fromGenerated(String identityGenerated) {
032    if (identityGenerated == null) {
033      return IdentityGenerated.AUTO;
034    }
035    return IdentityGenerated.valueOf(identityGenerated.toUpperCase());
036  }
037
038  /**
039   * Set the IdentityMode to the CreateTable model.
040   */
041  public static void toCreateTable(IdentityMode identityMode, CreateTable createTable) {
042
043    if (!identityMode.isPlatformDefault()) {
044      createTable.setIdentityType(toType(identityMode.getIdType()));
045    }
046    final String seqName = identityMode.getSequenceName();
047    if (seqName != null && !seqName.isEmpty()) {
048      createTable.setSequenceName(seqName);
049    }
050
051    createTable.setIdentityStart(toBigInteger(identityMode.getStart()));
052    createTable.setIdentityIncrement(toBigInteger(identityMode.getIncrement()));
053    createTable.setIdentityCache(toBigInteger(identityMode.getCache()));
054    final IdentityGenerated generated = identityMode.getGenerated();
055    if (generated != null && generated != IdentityGenerated.AUTO) {
056      createTable.setIdentityGenerated(generated.name().toLowerCase());
057    }
058  }
059
060
061  private static IdType fromType(IdentityType type) {
062    if (type == null) {
063      return IdType.AUTO;
064    }
065    switch (type) {
066      case DEFAULT:
067        return IdType.AUTO;
068      case SEQUENCE:
069        return IdType.SEQUENCE;
070      case IDENTITY:
071        return IdType.IDENTITY;
072      case GENERATOR:
073        return IdType.GENERATOR;
074      case EXTERNAL:
075        return IdType.EXTERNAL;
076    }
077    return IdType.AUTO;
078  }
079
080  private static IdentityType toType(IdType type) {
081    if (type == null) {
082      // intersection or element collection table
083      return null;
084    }
085    switch (type) {
086      case SEQUENCE:
087        return IdentityType.SEQUENCE;
088      case IDENTITY:
089        return IdentityType.IDENTITY;
090      case EXTERNAL:
091        return IdentityType.EXTERNAL;
092      case GENERATOR:
093        return IdentityType.GENERATOR;
094    }
095    return null;
096  }
097
098  private static int toInt(BigInteger firstVal, BigInteger secVal) {
099    if (firstVal != null) {
100      return firstVal.intValue();
101    }
102    return (secVal == null) ? 0 : secVal.intValue();
103  }
104
105  private static BigInteger toBigInteger(int value) {
106    return (value == 0) ? null : BigInteger.valueOf(value);
107  }
108}