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

com.jpattern.orm.query.OrmClassToolMapNameSolver Maven / Gradle / Ivy

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

/**
 * 
 * @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(IOrmClassToolMap ormClassToolMap) {
		this.ormClassToolMap = ormClassToolMap;
	}

	@Override
	public String solvePropertyName(String property) throws OrmException {
		if (resolveWithoutAlias) {
			return solvePropertyNameWithoutAlias(property);
		}
		final String alias = alias(property);
		final String field = field(property);
		if (!registeredClass.containsKey(alias)) {
			throw new OrmException("Alias [" + alias + "] is not associated with an Orm Entity. Registered alias are: " + registeredClass.keySet());
		}
		final String dbColumn = registeredClass.get(alias).getColumnWithJavaName(field).getName();
		return alias + "." + dbColumn;
	}

	@Override
	public String solvePropertyName(String property, String defaultValue) throws OrmException {
		final String alias = alias(property);
		final String field = field(property);
		if (!registeredClass.containsKey(alias)) {
			return defaultValue;
		}
		final String dbColumn = registeredClass.get(alias).getColumnWithJavaName(field).getName();
		return alias + "." + dbColumn;
	}

	@Override
	public void register(Class clazz) throws OrmException {
		register(clazz, clazz.getSimpleName());
	}

	@Override
	public void register(Class clazz, String alias) throws OrmException {
		registeredClass.put(alias, ormClassToolMap.getOrmClassTool(clazz).getClassMapper());
		classAlias.put(clazz, alias);
		if (defaultAlias==null) {
			defaultAlias = alias;
		}
	}

	@Override
	public String alias(Class clazz) throws OrmException {
		if (!classAlias.containsKey(clazz)) {
			throw new OrmException("Class " + clazz + " is not in the query" );
		}
		return classAlias.get(clazz);
	}

	private String alias(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 defaultAlias;
		}
	}

	private String field(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( String property) throws OrmException {
		final String alias = alias(property);
		final String field = field(property);
		if (!registeredClass.containsKey(alias)) {
			throw new OrmException("Alias [" + alias + "] is not associated with an Orm Entity. Registered alias are: " + registeredClass.keySet());
		}
		return registeredClass.get(alias).getColumnWithJavaName(field).getName();
	}

	@Override
	public void alwaysResolveWithoutAlias(boolean resolveWithoutAlias) {
		this.resolveWithoutAlias = resolveWithoutAlias;

	}

	@Override
	public boolean getAlwaysResolveWithoutAlias() {
		return resolveWithoutAlias;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy