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

com.jpattern.orm.classmapper.ClassMapperBuilder 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.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jpattern.logger.ILogger;
import com.jpattern.orm.logger.OrmLogger;
import com.jpattern.orm.util.FieldDefaultNaming;

/**
 * 
 * @author Francesco Cina
 *
 * 22/mag/2011
 */
public class ClassMapperBuilder implements IClassMapperBuilder {

	private final Class clazz;
	private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
	private final ITableMap tableMap;

	public ClassMapperBuilder(Class clazz, ITableMap tableMap) {
		this.clazz = clazz;
		this.tableMap = tableMap;
	}

	/* (non-Javadoc)
	 * @see com.jpattern.orm.mapper.IClassMapperGenerator#generate()
	 */
	@Override
	public IClassMapper generate() {
		logger.info("generate", "generate " + IClassMapper.class.getSimpleName() + " for Class " + clazz.getName());
		final String tableName = tableMap.getTableNameWithSchema();
		final String schemaName = tableMap.getSchemaName();
		logger.info("generate", "table name expected in relation with class " + clazz.getSimpleName() + ": " + tableName + " - schema: " + schemaName);
		final Map fieldColumnMapping = mapField(schemaName, tableName);
		final ClassMapper classMapper = new ClassMapper(clazz, tableMap, fieldColumnMapping);
		initializeColumnNames(classMapper, fieldColumnMapping);
		return classMapper ;
	}

	private Map mapField(String schemaName, String tableName) {
		final Map result = new LinkedHashMap();
		Column col = null;

		for ( final String fieldName : tableMap.getAllFieldNames() ) {
			if ( fieldName.length() > 0) {
				final String columnName = tableMap.getDBColumnName(fieldName);
				logger.info("exploreAllColumns", "DB column [" + columnName + "]" + " will be associated with object field [" + fieldName + "]");
				col = new Column( columnName );
				col.setIdentifying( tableMap.getPrimaryKeyFieldNames().contains(fieldName) );
				setGetterSetter( fieldName, col );
				result.put( fieldName , col );
			} else {
				logger.warn("exploreAllColumns", "Class fiels [" + fieldName + "]" + " is NOT associated with a DB colunm!");
			}
		}
		return result;
	}

	private void setGetterSetter(String javaPropertyName, Column col) {
		final List methods = Arrays.asList( clazz.getMethods() );
		final List fields = Arrays.asList( clazz.getDeclaredFields() );

		Field propertyField = null;
		Method getter = null;
		Method setter = null;
		String fieldName = "";
		String getterName = "";
		String setterName = "";

		for (final Field field : fields) {
			if (javaPropertyName.equals(field.getName())) {
				propertyField = field;
				fieldName = field.getName();
			}
		}

		for (final Method method : methods) {
			if (FieldDefaultNaming.getDefaultGetterName(javaPropertyName).equals(method.getName())) {
				getter = method;
				getterName = method.getName();
			}
			if (FieldDefaultNaming.getDefaultBooleanGetterName(javaPropertyName).equals(method.getName())) {
				getter = method;
				getterName = method.getName();
			}
			if (FieldDefaultNaming.getDefaultSetterName(javaPropertyName).equals(method.getName())) {
				setter = method;
				setterName = method.getName();
			}
		}

		logger.info("setGetterSetter", "property [" + fieldName + "] associated with column [" + col.getName() + "]");
		logger.info("setGetterSetter", "getter for property [" + fieldName + "]: [" + getterName + "]");
		logger.info("setGetterSetter", "setter for property [" + fieldName + "]: [" + setterName + "]");

		col.setField(propertyField);
		col.setSetter(setter);
		col.setGetter(getter);
	}

	private void initializeColumnNames(ClassMapper generated, Map fieldColumnMapping) {
		logger.info("initializeColumnNames", "");
		final List allColumnJavaNamesList = new ArrayList();
		final List allNotGeneratedColumnJavaNamesList = new ArrayList();
		final List allGeneratedColumnJavaNamesList = new ArrayList();
		final List allGeneratedColumnDBNamesList = new ArrayList();
		final List primaryKeyColumnJavaNamesList = new ArrayList();
		final List primaryKeyAndVersionColumnJavaNamesList = new ArrayList();
		final List notPrimaryKeyColumnJavaList = new ArrayList();
		for (final Entry entry : fieldColumnMapping.entrySet()) {
			final String javaFieldName = entry.getKey();
			allColumnJavaNamesList.add(javaFieldName);
			if( entry.getValue().isIdentifying() ) {
				primaryKeyColumnJavaNamesList.add(javaFieldName);
				primaryKeyAndVersionColumnJavaNamesList.add(javaFieldName);
				logger.info("initializeColumnNames", "Field [" + javaFieldName + "] will be used as a Primary Key field");
			} else {
				notPrimaryKeyColumnJavaList.add(javaFieldName);
				logger.info("initializeColumnNames", "Field [" + javaFieldName + "] will be used as a normal field");
			}
			if (tableMap.getGeneratorByJavaFieldName(javaFieldName).isAutoGenerated()) {
				allGeneratedColumnJavaNamesList.add(javaFieldName);
				allGeneratedColumnDBNamesList.add(entry.getValue().getName());
				logger.info("initializeColumnNames", "Field [" + javaFieldName + "] is an autogenerated field");
			} else {
				allNotGeneratedColumnJavaNamesList.add(javaFieldName);
			}

		}
		if (tableMap.isVersionable()) {
			primaryKeyAndVersionColumnJavaNamesList.add(tableMap.getVersionJavaFieldName());
		}

		generated.setAllColumnJavaNames( allColumnJavaNamesList.toArray(new String[0]) );
		generated.setAllNotGeneratedColumnJavaNames( allNotGeneratedColumnJavaNamesList.toArray(new String[0]) );
		generated.setAllGeneratedColumnJavaNames( allGeneratedColumnJavaNamesList.toArray(new String[0]) );
		generated.setAllGeneratedColumnDBNames( allGeneratedColumnDBNamesList.toArray(new String[0]) );
		generated.setNotPrimaryKeyColumnJavaNames( notPrimaryKeyColumnJavaList.toArray(new String[0]) );
		generated.setPrimaryKeyColumnJavaNames( primaryKeyColumnJavaNamesList.toArray(new String[0]) );
		generated.setPrimaryKeyAndVersionColumnJavaNames( primaryKeyAndVersionColumnJavaNamesList.toArray(new String[0]) );
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy