001package io.ebeaninternal.dbmigration.model.build;
002
003import io.ebean.config.dbplatform.DbPlatformType;
004import io.ebeaninternal.dbmigration.model.MColumn;
005import io.ebeaninternal.dbmigration.model.MTable;
006import io.ebeaninternal.dbmigration.model.visitor.BeanVisitor;
007import io.ebeaninternal.server.deploy.BeanDescriptor;
008import io.ebeaninternal.server.deploy.InheritInfo;
009
010/**
011 * Used to build the Model objects MTable etc.
012 */
013public class ModelBuildBeanVisitor implements BeanVisitor {
014
015  private final ModelBuildContext ctx;
016
017  public ModelBuildBeanVisitor(ModelBuildContext ctx) {
018    this.ctx = ctx;
019  }
020
021  /**
022   * Return the PropertyVisitor used to read all the property meta data
023   * and in this case add MColumn objects to the model.
024   * <p>
025   * This creates an MTable and adds it to the model.
026   * </p>
027   */
028  @Override
029  public ModelBuildPropertyVisitor visitBean(BeanDescriptor<?> descriptor) {
030
031    if (!descriptor.isInheritanceRoot()) {
032      return null;
033    }
034
035    MTable table = new MTable(descriptor);
036    // add the table to the model
037    ctx.addTable(table);
038
039    InheritInfo inheritInfo = descriptor.getInheritInfo();
040    if (inheritInfo != null && inheritInfo.isRoot()) {
041      // add the discriminator column
042      String discColumn = inheritInfo.getDiscriminatorColumn();
043      String columnDefn = inheritInfo.getColumnDefn();
044      if (columnDefn == null || columnDefn.isEmpty()) {
045        DbPlatformType dbType = ctx.getDbTypeMap().get(inheritInfo.getDiscriminatorType());
046        columnDefn = dbType.renderType(inheritInfo.getColumnLength(), 0);
047      }
048      table.addColumn(new MColumn(discColumn, columnDefn, true));
049    }
050
051    return new ModelBuildPropertyVisitor(ctx, table, descriptor);
052  }
053
054}