com.jpattern.orm.mapper.clazz.ClassMap Maven / Gradle / Ivy
package com.jpattern.orm.mapper.clazz;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.jpattern.orm.annotation.Version;
import com.jpattern.orm.annotation.table.TableInfo;
import com.jpattern.orm.exception.OrmConfigurationException;
import com.jpattern.orm.exception.OrmException;
/**
*
* @author Francesco Cina
*
* 22/mag/2011
*/
public class ClassMap implements IClassMap {
private final TableInfo tableInfo;
private final Class mappedClass;
private final Map> fieldClassMapByJavaName = new HashMap>();
private final Map> fieldClassMapByDBColumnName = new HashMap>();
private String[] allColumnJavaNames = new String[0];
private String[] allNotGeneratedColumnJavaNames = new String[0];
private String[] primaryKeyColumnJavaNames = new String[0];
private String[] primaryKeyAndVersionColumnJavaNames = new String[0];
private String[] notPrimaryKeyColumnJavaNames = new String[0];
private String[] allGeneratedColumnJavaNames = new String[0];
private String[] allGeneratedColumnDBNames = new String[0];
private boolean versionGenerator = false;
public ClassMap(final Class mappedClass, final TableInfo tableInfo) {
this.mappedClass = mappedClass;
this.tableInfo = tableInfo;
}
@Override
public Class getMappedClass() {
return this.mappedClass;
}
@SuppressWarnings("unchecked")
@Override
public IClassField getClassFieldByJavaName(final String javaName) {
if (this.fieldClassMapByJavaName.containsKey(javaName)) {
return (IClassField) this.fieldClassMapByJavaName.get(javaName);
}
throw new OrmException("Property with name [" + javaName + "] not found in class " + this.mappedClass);
}
@SuppressWarnings("unchecked")
@Override
public IClassField getClassFieldByDBColumnName(final String dbColumnName) {
if (this.fieldClassMapByDBColumnName.containsKey(dbColumnName)) {
return (IClassField) this.fieldClassMapByDBColumnName.get(dbColumnName);
}
throw new OrmException("DB Column with name [" + dbColumnName + "] not mapped for class " + this.mappedClass);
}
public void addClassField(final IClassField classField) {
this.fieldClassMapByJavaName.put(classField.getFieldName(), classField);
this.fieldClassMapByDBColumnName.put(classField.getColumnInfo().getDBColumnName(), classField);
if (classField.getVersionInfo().isVersionable()) {
if (this.versionGenerator) {
throw new OrmConfigurationException("A bean can have maximum one field annotated with @" + Version.class.getSimpleName() + ". Error in class:[" + this.getMappedClass() + "] field: [" + classField.getFieldName() + "]");
}
this.versionGenerator = true;
}
}
public Map> getUnmodifiableFieldClassMap() {
return Collections.unmodifiableMap( this.fieldClassMapByJavaName );
}
@Override
public String[] getAllColumnJavaNames() {
return this.allColumnJavaNames;
}
@Override
public String[] getPrimaryKeyColumnJavaNames() {
return this.primaryKeyColumnJavaNames;
}
@Override
public String[] getNotPrimaryKeyColumnJavaNames() {
return this.notPrimaryKeyColumnJavaNames;
}
public void setAllColumnJavaNames(final String[] allColumnJavaNames) {
this.allColumnJavaNames = allColumnJavaNames;
}
public void setPrimaryKeyColumnJavaNames(final String[] primaryKeyColumnJavaNames) {
this.primaryKeyColumnJavaNames = primaryKeyColumnJavaNames;
}
public void setNotPrimaryKeyColumnJavaNames(final String[] notPrimaryKeyColumnJavaNames) {
this.notPrimaryKeyColumnJavaNames = notPrimaryKeyColumnJavaNames;
}
public void setAllNotGeneratedColumnJavaNames(final String[] allNotGeneratedColumnJavaNames) {
this.allNotGeneratedColumnJavaNames = allNotGeneratedColumnJavaNames;
}
@Override
public String[] getAllNotGeneratedColumnJavaNames() {
return this.allNotGeneratedColumnJavaNames;
}
public void setAllGeneratedColumnJavaNames(final String[] allGeneratedColumnJavaNames) {
this.allGeneratedColumnJavaNames = allGeneratedColumnJavaNames;
}
@Override
public String[] getAllGeneratedColumnJavaNames() {
return this.allGeneratedColumnJavaNames;
}
public void setPrimaryKeyAndVersionColumnJavaNames(
final String[] primaryKeyAndVersionColumnJavaNames) {
this.primaryKeyAndVersionColumnJavaNames = primaryKeyAndVersionColumnJavaNames;
}
@Override
public String[] getPrimaryKeyAndVersionColumnJavaNames() {
return this.primaryKeyAndVersionColumnJavaNames;
}
@Override
public TableInfo getTableInfo() {
return this.tableInfo;
}
@Override
public String[] getAllGeneratedColumnDBNames() {
return this.allGeneratedColumnDBNames;
}
public void setAllGeneratedColumnDBNames(
final String[] allGeneratedColumnDBNames) {
this.allGeneratedColumnDBNames = allGeneratedColumnDBNames;
}
}