com.jwebmp.entityassist.querybuilder.builders.WhereExpression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of entity-assist Show documentation
Show all versions of entity-assist Show documentation
A Domain Driven SQL Builder Generator for JPMS/JDK8
package com.jwebmp.entityassist.querybuilder.builders;
import com.jwebmp.entityassist.enumerations.Operand;
import com.jwebmp.logger.LogFactory;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.validation.constraints.NotNull;
import java.util.Optional;
import java.util.logging.Logger;
final class WhereExpression
implements IFilterExpression
{
private static final Logger log = LogFactory.getLog("WhereExpression");
private Attribute expressionAttribute;
private Operand operand;
private Object expressionValue;
private From root;
private CriteriaBuilder criteriaBuilder;
WhereExpression()
{
}
WhereExpression(Attribute expressionAttribute, Operand operand, Object expressionValue)
{
this.expressionAttribute = expressionAttribute;
this.operand = operand;
this.expressionValue = expressionValue;
}
@Override
public int hashCode()
{
return super.hashCode();
}
@Override
public boolean equals(Object obj)
{
return super.equals(obj);
}
@Override
public Optional toPredicate(From entityRoot, CriteriaBuilder builder)
{
root = entityRoot;
criteriaBuilder = builder;
return processWhereExpression(this);
}
private Optional processWhereExpression(WhereExpression whereExpression)
{
Optional result;
result = processWhereNulls(whereExpression);
if (!result.isPresent())
{
result = processWhereEquals(whereExpression);
}
if (!result.isPresent())
{
result = processWhereLike(whereExpression);
}
if (!result.isPresent())
{
result = processWhereLists(whereExpression);
}
if (!result.isPresent())
{
result = processWhereCompare(whereExpression);
}
if (!result.isPresent())
{
log.severe("Unable to generate a where clause for the given expression");
}
return result;
}
@SuppressWarnings("unchecked")
private Optional processWhereNulls(WhereExpression whereExpression)
{
Attribute attribute = whereExpression.getExpressionAttribute();
switch (whereExpression.getOperand())
{
case Null:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().isNull(getRoot().get(SingularAttribute.class.cast(attribute))));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().isNull(getRoot().get(PluralAttribute.class.cast(attribute))));
}
}
case NotNull:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().isNotNull(getRoot().get(SingularAttribute.class.cast(attribute))));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().isNotNull(getRoot().get(PluralAttribute.class.cast(attribute))));
}
}
default:
{
return Optional.empty();
}
}
}
@SuppressWarnings("unchecked")
private Optional processWhereEquals(WhereExpression whereExpression)
{
Attribute attribute = whereExpression.getExpressionAttribute();
Object value = whereExpression.getExpressionValue();
switch (whereExpression.getOperand())
{
case Equals:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().equal(getRoot().get(SingularAttribute.class.cast(attribute)), value));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().equal(getRoot().get(PluralAttribute.class.cast(attribute)), value));
}
}
case NotEquals:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().notEqual(getRoot().get(SingularAttribute.class.cast(attribute)), value));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().notEqual(getRoot().get(PluralAttribute.class.cast(attribute)), value));
}
}
default:
{
return Optional.empty();
}
}
}
@SuppressWarnings("unchecked")
private Optional processWhereLike(WhereExpression whereExpression)
{
Attribute attribute = whereExpression.getExpressionAttribute();
Object value = whereExpression.getExpressionValue();
switch (whereExpression.getOperand())
{
case Like:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().like(getRoot().get(SingularAttribute.class.cast(attribute)), value.toString()));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().like(getRoot().get(PluralAttribute.class.cast(attribute)), value.toString()));
}
}
case NotLike:
{
if (IFilterExpression.isSingularAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().notLike(getRoot().get(SingularAttribute.class.cast(attribute)), value.toString()));
}
else if (IFilterExpression.isPluralOrMapAttribute(attribute))
{
return Optional.of(getCriteriaBuilder().notLike(getRoot().get(PluralAttribute.class.cast(attribute)), value.toString()));
}
}
default:
{
return Optional.empty();
}
}
}
@SuppressWarnings("unchecked")
private Optional processWhereLists(WhereExpression whereExpression)
{
Attribute attribute = whereExpression.getExpressionAttribute();
Object value = whereExpression.getExpressionValue();
switch (whereExpression.getOperand())
{
case InList:
{
Expression