com.jpattern.orm.classmapper.ClassTableMapBuilder Maven / Gradle / Ivy
package com.jpattern.orm.classmapper;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import com.jpattern.orm.annotation.Column;
import com.jpattern.orm.annotation.Generator;
import com.jpattern.orm.annotation.Id;
import com.jpattern.orm.annotation.Ignore;
import com.jpattern.orm.annotation.Schema;
import com.jpattern.orm.annotation.Table;
import com.jpattern.orm.annotation.Version;
import com.jpattern.orm.crud.AColumnValueGenerator;
import com.jpattern.orm.crud.ColumnValueGeneratorFactory;
import com.jpattern.orm.crud.NullColumnValueGenerator;
import com.jpattern.orm.exception.OrmConfigurationException;
import com.jpattern.orm.util.FieldDefaultNaming;
import com.jpattern.orm.util.GenericWrapper;
/**
*
* @author Francesco Cina
*
* 08/giu/2011
*/
public class ClassTableMapBuilder implements ITableMapBuilder {
private final Class clazz;
private final TableMap tableMap;
public ClassTableMapBuilder(Class clazz, TableMap tableMap) {
this.clazz = clazz;
this.tableMap = tableMap;
}
@Override
public void updateTableMap() {
generateTableName(tableMap);
generateSchemaName(tableMap);
generateColumnNames(tableMap);
}
private void generateColumnNames(TableMap tableMap) {
final Field[] fields = clazz.getDeclaredFields();
for (final Field field : fields) {
if (!Modifier.isStatic( field.getModifiers() ) ) {
final GenericWrapper columnValueGeneratorWrapper = new GenericWrapper(new NullColumnValueGenerator(""));
final StringBuffer columnName = new StringBuffer();
final Ignore ignore = field.getAnnotation(Ignore.class);
final Id id = field.getAnnotation(Id.class);
final Column column = field.getAnnotation(Column.class);
final Generator generator = field.getAnnotation(Generator.class);
final Version version = field.getAnnotation(Version.class);
if (ignore==null) {
verifyAnnotationId(id, field, tableMap);
verifyAnnotationColumn(column, columnName, field);
verifyAnnotationGenerator(generator, columnName, columnValueGeneratorWrapper, field, tableMap);
verifyAnnotationVersion(version, field, tableMap);
tableMap.addColumnMapping(field.getName(), columnName.toString(), columnValueGeneratorWrapper.getValue());
}
}
}
}
private void generateTableName(TableMap tableMap) {
String tableName = FieldDefaultNaming.getJavanameToDBnameDefaultMapping(clazz.getSimpleName());
final Table annotation = clazz.getAnnotation(Table.class);
if ( annotation != null) {
tableName = annotation.name();
}
tableMap.setTableName( tableName );
}
private void generateSchemaName(TableMap tableMap) {
final Schema annotation = clazz.getAnnotation(Schema.class);
if ( annotation != null) {
tableMap.setSchemaName(annotation.name());
}
}
private void verifyAnnotationId(Id id, Field field, TableMap tableMap) {
if (id!=null) {
tableMap.addPrimaryKey(field.getName());
}
}
private void verifyAnnotationColumn(Column column, StringBuffer columnName, Field field) {
if (column!=null) {
columnName.append( column.name() );
} else {
columnName.append( FieldDefaultNaming.getJavanameToDBnameDefaultMapping(field.getName()) );
}
}
private void verifyAnnotationGenerator(Generator generator, StringBuffer columnName, GenericWrapper columnValueGeneratorWrapper, Field field, TableMap tableMap) {
if (generator!=null) {
final AColumnValueGenerator columnValueGenerator = ColumnValueGeneratorFactory.generator(generator.generatorType(), generator.name());
if (tableMap.hasGeneratedField()) {
throw new OrmConfigurationException("A bean can have maximum one field annotated with @" + Generator.class.getSimpleName() + ". Error in class:[" + clazz.getCanonicalName() + "] field: [" + field.getName() + "]");
}
tableMap.setHasGeneratedField(true);
columnValueGenerator.setCacheSize(generator.getCacheSize());
columnValueGenerator.setActivateValues(generator.ifValueIn());
columnValueGenerator.setGeneratedColumnName(columnName.toString());
columnValueGeneratorWrapper.setValue(columnValueGenerator);
}
}
private void verifyAnnotationVersion(Version version, Field field, TableMap tableMap) {
if (version!=null) {
if (tableMap.isVersionable()) {
throw new OrmConfigurationException("A bean can have maximum one field annotated with @" + Version.class.getSimpleName() + ". Error in class:[" + clazz.getCanonicalName() + "] field: [" + field.getName() + "]");
}
tableMap.setVersionable(true);
tableMap.setVersionLoadLockMode( version.lock() );
tableMap.setVersionJavaFieldName(field.getName());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy