com.wesleyhome.dao.processor.method.TrimExpressionMethodGenerator Maven / Gradle / Ivy
package com.wesleyhome.dao.processor.method;
import javax.lang.model.element.Element;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;
import com.wesleyhome.dao.processor.EntityInformationMap;
import com.wesleyhome.dao.processor.model.EntityInfo;
import com.wesleyhome.dao.processor.model.MappingType;
public class TrimExpressionMethodGenerator implements MethodGenerator {
@Override
public void createMethods(final JCodeModel model, final JClass entityJClass, final JDefinedClass _class, final Element fieldElement,
final JFieldVar entityManagerField, final boolean generate, final EntityInfo entityInfo, final EntityInformationMap entityInfoMap, MappingType mappingType) {
JClass returnType = model.ref(Expression.class).narrow(String.class);
JMethod method = _class.method(JMod.PROTECTED,returnType, "trim");
JVar param = method.param(returnType, "x");
JBlock body = method.body();
JVar cb = body.decl(model.ref(CriteriaBuilder.class), "criteriaBuilder", JExpr._this().invoke("getCriteriaBuilder"));
body._return(getTrimInvocation(model, cb, param));
}
private JInvocation getTrimInvocation(final JCodeModel model, final JVar cb, final JVar param) {
JInvocation ltrimFunction = cb.invoke("function").arg("ltrim");
JInvocation rtrimFunction = cb.invoke("function").arg("rtrim");
JFieldRef stringClassRef = model.ref(String.class).staticRef("class");
return ltrimFunction.arg(stringClassRef).arg(rtrimFunction.arg(stringClassRef).arg(param));
}
}