
com.jpattern.orm.query.OrmClassToolMapNameSolver Maven / Gradle / Ivy
package com.jpattern.orm.query;
import java.util.LinkedHashMap;
import java.util.Map;
import com.jpattern.orm.classmapper.IClassMapper;
import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmQueryFormatException;
/**
*
* @author Francesco Cina
*
* 22/giu/2011
*/
public class OrmClassToolMapNameSolver implements INameSolver {
private final Map> registeredClass = new LinkedHashMap>();
private final Map, String> classAlias = new LinkedHashMap, String>();
private final IOrmClassToolMap ormClassToolMap;
private boolean resolveWithoutAlias = false;
private String defaultAlias = null;
public OrmClassToolMapNameSolver(final IOrmClassToolMap ormClassToolMap) {
this.ormClassToolMap = ormClassToolMap;
}
@Override
public String solvePropertyName(final String property) throws OrmException {
if (this.resolveWithoutAlias) {
return this.solvePropertyNameWithoutAlias(property);
}
final String alias = this.alias(property);
final String field = this.field(property);
if (!this.registeredClass.containsKey(alias)) {
throw new OrmException("Alias [" + alias + "] is not associated with an Orm Entity. Registered alias are: " + this.registeredClass.keySet());
}
final String dbColumn = this.getDbColumn(alias, field);
return alias + "." + dbColumn;
}
@Override
public String solvePropertyName(final String property, final String defaultValue) throws OrmException {
final String alias = this.alias(property);
final String field = this.field(property);
if (!this.registeredClass.containsKey(alias)) {
return defaultValue;
}
final String dbColumn = this.getDbColumn(alias, field);
return alias + "." + dbColumn;
}
@Override
public void register(final Class> clazz) throws OrmException {
this.register(clazz, clazz.getSimpleName());
}
@Override
public void register(final Class> clazz, final String alias) throws OrmException {
this.registeredClass.put(alias, this.ormClassToolMap.getOrmClassTool(clazz).getClassMapper());
this.classAlias.put(clazz, alias);
if (this.defaultAlias==null) {
this.defaultAlias = alias;
}
}
@Override
public String alias(final Class> clazz) throws OrmException {
if (!this.classAlias.containsKey(clazz)) {
throw new OrmException("Class " + clazz + " is not in the query" );
}
return this.classAlias.get(clazz);
}
private String alias(final String property) throws OrmException {
try {
return property.substring(0, property.lastIndexOf("."));
} catch (final Exception e) {
// throw new OrmException("Error parsing property [" + property + "], the format must be CLASS_NAME.CLASS_FIELD or CLASS_ALIAS.CLASS_FIELD" );
return this.defaultAlias;
}
}
private String field(final String property) throws OrmException {
try {
return property.substring(property.lastIndexOf(".")+1);
} catch (final Exception e) {
throw new OrmException("Error parsing property [" + property + "], the format must be CLASS_NAME.CLASS_FIELD or CLASS_ALIAS.CLASS_FIELD" );
}
}
@Override
public String solvePropertyNameWithoutAlias( final String property) throws OrmException {
final String alias = this.alias(property);
final String field = this.field(property);
if (!this.registeredClass.containsKey(alias)) {
throw new OrmException("Alias [" + alias + "] is not associated with an Orm Entity. Registered alias are: " + this.registeredClass.keySet());
}
return this.getDbColumn(alias, field);
}
@Override
public void alwaysResolveWithoutAlias(final boolean resolveWithoutAlias) {
this.resolveWithoutAlias = resolveWithoutAlias;
}
@Override
public boolean getAlwaysResolveWithoutAlias() {
return this.resolveWithoutAlias;
}
private String getDbColumn(final String alias, final String field) {
String dbColumn = this.registeredClass.get(alias).getColumnWithJavaName(field).getName();
if (dbColumn.isEmpty()) {
throw new OrmQueryFormatException("Field with name [" + field + "] is not present or ignored for alias [" + alias + "]");
}
return dbColumn;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy