All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.orm.classmapper.ClassTableMapBuilder Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
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.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;

/**
 * 
 * @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() ) ) {
				AColumnValueGenerator columnValueGenerator = new NullColumnValueGenerator("");
				String columnName = FieldDefaultNaming.getJavanameToDBnameDefaultMapping(field.getName());
				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 (id!=null) {
					tableMap.addPrimaryKey(field.getName());
				}
				if (column!=null) {
					columnName = column.name();
				}
				if (generator!=null) {
					columnValueGenerator = ColumnValueGeneratorFactory.generator(generator.generatorType(), generator.name());
					columnValueGenerator.setCacheSize(generator.getCacheSize());
					columnValueGenerator.setGeneratedColumnName(columnName);
				}
				if (version!=null) {
					if (tableMap.isVersionable()) {
						throw new OrmConfigurationException("A bean can have maximum one field annotated with @Version. Error in class:[" + clazz.getCanonicalName() + "] field: [" + field.getName() + "]");
					}
					tableMap.setVersionable(true);
					tableMap.setVersionLoadLockMode( version.lock() );
					tableMap.setVersionJavaFieldName(field.getName());
				}
				tableMap.addColumnMapping(field.getName(), columnName, columnValueGenerator);
			}
		}

	}

	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());
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy