All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.wesleyhome.dao.processor.method.NamedQueryMethodGenerator Maven / Gradle / Ivy
package com.wesleyhome.dao.processor.method;
import java.util.List;
import javax.lang.model.element.Element;
import javax.persistence.TypedQuery;
import org.apache.commons.lang3.StringUtils;
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.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
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;
import com.wesleyhome.dao.processor.model.NamedQueryInformation;
import com.wesleyhome.dao.processor.model.ParamInfo;
public class NamedQueryMethodGenerator 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, final MappingType mappingType) {
List namedQueryInfoList = entityInfo.namedQueryInfoList;
for (NamedQueryInformation namedQueryInformation : namedQueryInfoList) {
if (generate(_class, generate)) {
JMethod method = createQueryMethod(model, entityJClass, _class, fieldElement, namedQueryInformation);
if (!_class.isInterface()) {
createMethodBody(method, entityJClass, model, fieldElement, entityManagerField, namedQueryInformation);
}
}
}
}
protected boolean generate(final JDefinedClass _class, final boolean generate) {
return _class.isInterface() || generate;
}
private JMethod createQueryMethod(final JCodeModel model, final JClass entityJClass, final JDefinedClass _class,
final Element fieldElement, final NamedQueryInformation namedQueryInformation) {
String methodName = getMethodName(namedQueryInformation.namedQueryName);
List paramInfoList = namedQueryInformation.paramInfoList;
JType returnType = CodeGenerationHelper.getReturnType(model, namedQueryInformation.returnInfo);
int queryMethodModifiers = _class.isInterface() ? JMod.NONE : JMod.PUBLIC;
JMethod method = _class.method(queryMethodModifiers, returnType, methodName);
CodeGenerationHelper.applyParameters(model, paramInfoList, method);
return method;
}
private String getMethodName(final String namedQueryName) {
return String.format("run%sNamedQuery", StringUtils.capitalize(namedQueryName));
}
private void createMethodBody(final JMethod method, final JClass entityJClass, final JCodeModel model, final Element fieldElement,
final JFieldVar entityManagerField, final NamedQueryInformation namedQueryInformation) {
method.annotate(Override.class);
JBlock body = method.body();
JClass returnType = (JClass) method.type();
JClass namedQueryClass = model.ref(TypedQuery.class).narrow(returnType);
JInvocation invoke = JExpr._this().invoke("createNamedQuery").arg(namedQueryInformation.namedQueryName)
.arg(returnType.staticInvoke("class"));
JVar namedQueryVar = body.decl(namedQueryClass, "namedQuery", invoke);
List params = method.params();
for (JVar param : params) {
body.add(namedQueryVar.invoke("setParameter").arg(param.name()).arg(param));
}
body._return(namedQueryVar.invoke("getResultList"));
}
}