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

com.jpattern.orm.query.clause.OrmWhere Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.jpattern.orm.query.clause;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jpattern.orm.query.BaseFindQuery;
import com.jpattern.orm.query.NameSolver;
import com.jpattern.orm.query.NameSolverConsumer;
import com.jpattern.orm.query.NullNameSolver;
import com.jpattern.orm.query.SmartRenderableSqlSubElement;
import com.jpattern.orm.query.clause.where.AndExpressionElement;
import com.jpattern.orm.query.clause.where.EqExpressionElement;
import com.jpattern.orm.query.clause.where.EqPropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.ExpressionElement;
import com.jpattern.orm.query.clause.where.GeExpressionElement;
import com.jpattern.orm.query.clause.where.GePropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.GtExpressionElement;
import com.jpattern.orm.query.clause.where.GtPropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.IEqExpressionElement;
import com.jpattern.orm.query.clause.where.IEqPropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.ILikeExpressionElement;
import com.jpattern.orm.query.clause.where.InExpressionElement;
import com.jpattern.orm.query.clause.where.InSubQueryExpressionElement;
import com.jpattern.orm.query.clause.where.IsNotNullExpressionElement;
import com.jpattern.orm.query.clause.where.IsNullExpressionElement;
import com.jpattern.orm.query.clause.where.LeExpressionElement;
import com.jpattern.orm.query.clause.where.LePropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.LikeExpressionElement;
import com.jpattern.orm.query.clause.where.LtExpressionElement;
import com.jpattern.orm.query.clause.where.LtPropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.NInExpressionElement;
import com.jpattern.orm.query.clause.where.NLikeExpressionElement;
import com.jpattern.orm.query.clause.where.NeExpressionElement;
import com.jpattern.orm.query.clause.where.NePropertiesExpressionElement;
import com.jpattern.orm.query.clause.where.NotExpressionElement;
import com.jpattern.orm.query.clause.where.OrExpressionElement;

/**
 * 
 * @author Francesco Cina
 *
 * 19/giu/2011
 */
public abstract class OrmWhere> extends SmartRenderableSqlSubElement implements Where, NameSolverConsumer{

	List elementList = new ArrayList();
	private NameSolver nameSolver = new NullNameSolver();

	protected abstract T where();

	@Override
	public T allEq(final Map propertyMap) {
		for (final Entry entry : propertyMap.entrySet()) {
			eq(entry.getKey(), entry.getValue());
		}
		return where();
	}

	@Override
	public T eq(final String property, final Object value) {
		final ExpressionElement expressionElement = new EqExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T eqProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new EqPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T le(final String property, final Object value) {
		final ExpressionElement expressionElement = new LeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T leProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new LePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ge(final String property, final Object value) {
		final ExpressionElement expressionElement = new GeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T geProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new GePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T lt(final String property, final Object value) {
		final ExpressionElement expressionElement = new LtExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ltProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new LtPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T gt(final String property, final Object value) {
		final ExpressionElement expressionElement = new GtExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T gtProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new GtPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ieq(final String property, final String value) {
		final ExpressionElement expressionElement = new IEqExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ieqProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new IEqPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ilike(final String property, final String value) {
		final ExpressionElement expressionElement = new ILikeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T in(final String property, final Collection values) {
		final ExpressionElement expressionElement = new InExpressionElement(property, values);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T in(final String property, final Object[] values) {
		return in(property, Arrays.asList( values ));
	}

	@Override
	public T in(final String property, final BaseFindQuery subQuery) {
		final ExpressionElement expressionElement = new InSubQueryExpressionElement(property, subQuery, true);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T nin(final String property, final Collection values) {
		final ExpressionElement expressionElement = new NInExpressionElement(property, values);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T nin(final String property, final Object[] values) {
		return nin(property, Arrays.asList( values ));
	}

	@Override
	public T nin(final String property, final BaseFindQuery subQuery) {
		final ExpressionElement expressionElement = new InSubQueryExpressionElement(property, subQuery, false);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T isNotNull(final String property) {
		final ExpressionElement expressionElement = new IsNotNullExpressionElement(property);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T isNull(final String property) {
		final ExpressionElement expressionElement = new IsNullExpressionElement(property);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T like(final String property, final String value) {
		final ExpressionElement expressionElement = new LikeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T nlike(final String property, final String value) {
		final ExpressionElement expressionElement = new NLikeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T ne(final String property, final Object value) {
		final ExpressionElement expressionElement = new NeExpressionElement(property, value);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T neProperties(final String firstProperty, final String secondProperty) {
		final ExpressionElement expressionElement = new NePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T not(final ExpressionElement expression) {
		final ExpressionElement expressionElement = new NotExpressionElement(expression);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T or(final ExpressionElement expressionOne, final ExpressionElement expressionTwo) {
		final ExpressionElement expressionElement = new OrExpressionElement(expressionOne, expressionTwo);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public T and(final ExpressionElement expressionOne, final ExpressionElement expressionTwo) {
		final ExpressionElement expressionElement = new AndExpressionElement(expressionOne, expressionTwo);
		expressionElement.setNameSolver(this.nameSolver);
		this.elementList.add(expressionElement);
		return where();
	}

	@Override
	public void setNameSolver(final NameSolver nameSolver) {
		this.nameSolver = nameSolver;
	}

	@Override
	public final void doElementRender(final StringBuilder StringBuilder) {
		boolean first = true;
		if (!this.elementList.isEmpty()) {
			StringBuilder.append("WHERE ");
			for (final ExpressionElement expressionElement : this.elementList) {
				if (!first) {
					StringBuilder.append("AND ");
				}
				expressionElement.renderSqlElement(StringBuilder);
				first = false;
			}
		}
	}

	@Override
	public final void appendElementValues(final List values) {
		for (final ExpressionElement expressionElement : this.elementList) {
			expressionElement.appendElementValues(values);
		}
	}

	@Override
	public final int getElementStatusVersion() {
		return this.elementList.size();
	}

}