net.java.ao.atlassian.AtlassianTableNameConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activeobjects Show documentation
Show all versions of activeobjects Show documentation
This is the full Active Objects library, if you don't know which one to use, you probably want this one.
package net.java.ao.atlassian;
import net.java.ao.RawEntity;
import net.java.ao.schema.Case;
import net.java.ao.schema.TableAnnotationTableNameConverter;
import net.java.ao.schema.TableNameConverter;
import net.java.ao.schema.UnderscoreTableNameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import static net.java.ao.atlassian.ConverterUtils.checkLength;
/**
* This is the table name converter used by the Active Objects plugin. It works according to the following:
*
* - If the {@link net.java.ao.RawEntity entity interface} is annotated with {@link net.java.ao.schema.Table} then the value
* of the annotation is used as the base for naming.
* - Otherwise the {@link Class#getSimpleName() simple class name} is used.
*
* Then the following transformations are applied, in order:
*
* - The base name is transform from camel case to under score and upper case. e.q. {@code MyEntity} becomes
* {@code MY_ENTITY}.
* - The {@link TablePrefix prefix} is then applied, using its {@link TablePrefix#prepend(String) prepend} method.
*
*
* This means that if you refactor your entities and don't want to have to upgrade the database tables used behind
* the scene, you can use the {@link net.java.ao.schema.Table} annotation to your advantage. For example, one could
* refactor the following entity:
*
* public interface MyEntity {}
*
* to
*
* @Table("MyEntity")
* public interface YourEntity {}
*
*/
public final class AtlassianTableNameConverter implements TableNameConverter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final TableNameConverter tableNameConverter;
public AtlassianTableNameConverter(TablePrefix prefix) {
// this the basic conversion we want, under score and upper case
final UnderscoreTableNameConverter baseConverter = new UnderscoreTableNameConverter(Case.UPPER);
tableNameConverter =
new PrefixedTableNameConverter(
Objects.requireNonNull(prefix,"prefix can't be null"),
new TableAnnotationTableNameConverter(baseConverter, baseConverter));
}
@Override
public String getName(Class extends RawEntity>> entityClass) {
final String name = tableNameConverter.getName(entityClass);
checkLength(name,
"Invalid entity, generated table name (" + name + ") for '" + entityClass.getName() + "' is too long! " +
"It should be no longer than " + ConverterUtils.MAX_LENGTH + " chars.");
logger.debug("Table name for '{}' is '{}'", entityClass.getName(), name);
return name;
}
}