001package io.ebean.config;
002
003import io.ebean.util.CamelCaseHelper;
004
005/**
006 * Converts between Camel Case and Underscore based names for both table and
007 * column names (and is the default naming convention in Ebean).
008 *
009 * @author emcgreal
010 * @author rbygrave
011 */
012public class UnderscoreNamingConvention extends AbstractNamingConvention {
013
014  /**
015   * Force toUnderscore to return in upper case.
016   */
017  private boolean forceUpperCase = false;
018
019  /**
020   * The digits compressed.
021   */
022  private boolean digitsCompressed = true;
023
024  /**
025   * Create with a given sequence format.
026   *
027   * @param sequenceFormat the sequence format
028   */
029  public UnderscoreNamingConvention(String sequenceFormat) {
030    super(sequenceFormat);
031  }
032
033  /**
034   * Create with a sequence format of "{table}_seq".
035   */
036  public UnderscoreNamingConvention() {
037    super();
038  }
039
040  /**
041   * Returns the last part of the class name.
042   *
043   * @param beanClass the bean class
044   * @return the table name from class
045   */
046  @Override
047  public TableName getTableNameByConvention(Class<?> beanClass) {
048
049    return new TableName(getCatalog(), getSchema(), toUnderscoreFromCamel(beanClass.getSimpleName()));
050  }
051
052  /**
053   * Converts Camel case property name to underscore based column name.
054   *
055   * @return the column from property
056   */
057  @Override
058  public String getColumnFromProperty(Class<?> beanClass, String propertyName) {
059
060    return toUnderscoreFromCamel(propertyName);
061  }
062
063  /**
064   * Converts underscore based column name to Camel case property name.
065   *
066   * @param beanClass    the bean class
067   * @param dbColumnName the db column name
068   * @return the property from column
069   */
070  @Override
071  public String getPropertyFromColumn(Class<?> beanClass, String dbColumnName) {
072    return toCamelFromUnderscore(dbColumnName);
073  }
074
075  /**
076   * Return true if the result will be upper case.
077   * <p>
078   * False if it will be lower case.
079   * </p>
080   */
081  public boolean isForceUpperCase() {
082    return forceUpperCase;
083  }
084
085  /**
086   * Set to true to make the result upper case.
087   */
088  public void setForceUpperCase(boolean forceUpperCase) {
089    this.forceUpperCase = forceUpperCase;
090  }
091
092  /**
093   * Returns true if digits are compressed.
094   */
095  public boolean isDigitsCompressed() {
096    return digitsCompressed;
097  }
098
099  /**
100   * Sets to true for digits to be compressed (without a leading underscore).
101   */
102  public void setDigitsCompressed(boolean digitsCompressed) {
103    this.digitsCompressed = digitsCompressed;
104  }
105
106  @Override
107  public String getForeignKey(String prefix, String fkProperty) {
108    return prefix + "_" + toUnderscoreFromCamel(fkProperty);
109  }
110
111  /**
112   * Convert and return the string to underscore from camel case.
113   */
114  protected String toUnderscoreFromCamel(String camelCase) {
115    return CamelCaseHelper.toUnderscoreFromCamel(camelCase, digitsCompressed, forceUpperCase);
116  }
117
118  /**
119   * Convert and return the from string from underscore to camel case.
120   */
121  protected String toCamelFromUnderscore(String underscore) {
122    return CamelCaseHelper.toCamelFromUnderscore(underscore);
123  }
124}