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.DAOAnnotationProcessor Maven / Gradle / Ivy
/*
* @(#)DAOAnnotationProcessor.java
*
* (C) Copyright 2014 by Travelers
* All Rights Reserved.
*
* This software is the confidential and proprietary information
* of the Travelers Corporation. ("Confidential Information").
* Redistribution of the source code or binary form is not permitted
* without prior authorization from Travelers.
*/
package com.wesleyhome.dao.processor;
import static com.wesleyhome.dao.processor.DAOGeneratorProcessor.DAO_NAMED_KEY;
import static com.wesleyhome.dao.processor.DAOGeneratorProcessor.ENTITY_MANANGER_INJECT_KEY;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.processing.Messager;
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JPrimitiveType;
import com.wesleyhome.dao.processor.method.CustomQueryMethodGenerator;
import com.wesleyhome.dao.processor.method.FindAllQueryMethodGenerator;
import com.wesleyhome.dao.processor.method.FindByPKQueryMethodGenerator;
import com.wesleyhome.dao.processor.method.GetCriteriaBuilderMethodGenerator;
import com.wesleyhome.dao.processor.method.GetResultListMethodGenerator;
import com.wesleyhome.dao.processor.method.GetSingleResultMethodGenerator;
import com.wesleyhome.dao.processor.method.MethodGenerator;
import com.wesleyhome.dao.processor.method.NamedQueryMethodGenerator;
import com.wesleyhome.dao.processor.method.QueryMethodGeneratorFactory;
import com.wesleyhome.dao.processor.method.TrimExpressionMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.EqualsMethodPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.comparable.IsBetweenPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.comparable.IsGreaterThanOrEqualToPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.comparable.IsGreaterThanPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.comparable.IsLessThanOrEqualToThanPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.comparable.IsLessThanPredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.date.IsAfterDatePredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.date.IsBeforeDatePredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.date.IsOnOrAfterDatePredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.date.IsOnOrBeforeDatePredicateMethodGenerator;
import com.wesleyhome.dao.processor.method.predicates.string.ContainsPatternMethod;
import com.wesleyhome.dao.processor.method.predicates.string.EndsWithPatternMethod;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionExpression;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionExpressionChar;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionExpressionExpression;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionString;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionStringChar;
import com.wesleyhome.dao.processor.method.predicates.string.ILikeExpressionStringExpression;
import com.wesleyhome.dao.processor.method.predicates.string.StartsWithPatternMethod;
import com.wesleyhome.dao.processor.model.EntityInfo;
import com.wesleyhome.dao.processor.model.QueryParameterInfo;
/**
* The DAOAnnotationProcessor
class is a
*
* @author
* @since
*/
abstract class DAOAnnotationProcessor implements ClassProcessor {
public static final String DEFAULT_BASE_PACKAGE = "com.travelers.smart.model.dao";
protected AnnotationHelper annotationHelper;
protected boolean generateNamedAnnotation;
protected boolean generateEntityManagerInject;
/**
*
*/
public DAOAnnotationProcessor() {
super();
annotationHelper = new AnnotationHelperImpl();
}
// ******************************
// * SET-OPTIONS *
// ******************************
/*
* (non-Javadoc)
*
* @see
* com.travelers.smart.dao.generator.processor.ClassProcessor#setOptions
* (java.util.Map)
*/
@Override
public void setOptions(final Map options) {
String daoNamedValue = options.get(DAO_NAMED_KEY);
String entityManagerInjectValue = options.get(ENTITY_MANANGER_INJECT_KEY);
generateNamedAnnotation = daoNamedValue == null ? true : Boolean.parseBoolean(daoNamedValue);
generateEntityManagerInject = entityManagerInjectValue == null ? true : Boolean.parseBoolean(entityManagerInjectValue);
}
// ******************************
// * VALIDATE *
// ******************************
@Override
public boolean validate(final EntityInfo entityInfo, final Messager messager) {
return DAOAnnotaionValidator.instance.validate(entityInfo, messager);
}
// ******************************
// * GENERATION *
// ******************************
/*
* (non-Javadoc)
*
* @see
* com.travelers.smart.dao.generator.processor.ClassProcessor#generate(com
* .travelers.smart.dao.generator.processor.model.GeneratedQueryInfo,
* com.sun.codemodel.JCodeModel)
*/
@Override
public JDefinedClass generate(final EntityInfo entityInfo, final JCodeModel model, final EntityInformationMap entityInfoMap) {
try {
JClass entityClassRef = model.ref(entityInfo.fullEntityClassName);
String packageName = getPackageName(entityClassRef, entityInfo);
JPackage _package = model._package(packageName);
String daoClassName = defineGeneratedClassName(entityInfo);
ClassType classType = isInterface() ? ClassType.INTERFACE : ClassType.CLASS;
JDefinedClass generatedClass = _package._class(getClassModifiers(entityInfo), daoClassName, classType);
generateFields(entityInfo, model, entityClassRef, generatedClass);
defineClass(entityInfo, model, entityClassRef, generatedClass);
JFieldVar entityManagerField = generatedClass.fields().get("entityManager");
generateMethods(entityInfo, model, generatedClass, entityClassRef, entityManagerField, entityInfoMap);
generateSerialVersionUID(model, generatedClass);
return generatedClass;
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException(e);
}
}
private void generateSerialVersionUID(final JCodeModel model, final JDefinedClass generatedClass) {
if (!generatedClass.isInterface()) {
long computeDefaultSUID = JDefinedObjectStreamClass.computeDefaultSUID(generatedClass);
// private static final long serialVersionUID = 8017106205452887140L;
JPrimitiveType type = model.LONG;
generatedClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, type, "serialVersionUID", JExpr.lit(computeDefaultSUID));
}
}
/**
* @param entityInfo
* @param model
* @param entityClassRef
* @param generatedClass
*/
protected void generateFields(final EntityInfo entityInfo, final JCodeModel model, final JClass entityClassRef,
final JDefinedClass generatedClass) {
}
/**
* @param entityInfo
* @return
*/
protected int getClassModifiers(final EntityInfo entityInfo) {
return JMod.PUBLIC;
}
/**
* @param entityInfo
* @param model
* @param generatedClass
* @param entityClassRef
* @param entityManagerField
* @param entityInfoMap
*/
private void generateMethods(final EntityInfo entityInfo, final JCodeModel model, final JDefinedClass generatedClass,
final JClass entityClassRef, final JFieldVar entityManagerField, final EntityInformationMap entityInfoMap) {
generateConstuctor(entityInfo, generatedClass, model);
generateQueryMethods(entityInfo, generatedClass, model, entityClassRef, entityManagerField, entityInfoMap);
generateCustomQueryMethods(entityInfo, generatedClass, model);
generateNamedQueryMethods(entityInfo, generatedClass, model, entityManagerField);
generateRequiredMethodImplementations(entityInfo, generatedClass, model, entityClassRef, entityManagerField);
generateProtectedMethodImplementations(entityInfo, generatedClass, model, entityClassRef, entityManagerField);
}
private void generateProtectedMethodImplementations(final EntityInfo entityInfo, final JDefinedClass generatedClass,
final JCodeModel model, final JClass entityClassRef, final JFieldVar entityManagerField) {
if (!generatedClass.isInterface()) {
List extends Class extends MethodGenerator>> generatorClasses = getProtectedGenerators();
for (Class extends MethodGenerator> generatorClass : generatorClasses) {
try {
MethodGenerator generator = generatorClass.newInstance();
generator.createMethods(model, entityClassRef, generatedClass, null, entityManagerField, true, entityInfo, null, null);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
private List extends Class extends MethodGenerator>> getProtectedGenerators() {
return Arrays.asList(GetSingleResultMethodGenerator.class, GetResultListMethodGenerator.class,
GetCriteriaBuilderMethodGenerator.class, EqualsMethodPredicateMethodGenerator.class, IsBetweenPredicateMethodGenerator.class,
IsBeforeDatePredicateMethodGenerator.class, IsAfterDatePredicateMethodGenerator.class,
IsOnOrBeforeDatePredicateMethodGenerator.class, IsOnOrAfterDatePredicateMethodGenerator.class,
IsGreaterThanPredicateMethodGenerator.class, IsLessThanPredicateMethodGenerator.class,
IsGreaterThanOrEqualToPredicateMethodGenerator.class, IsLessThanOrEqualToThanPredicateMethodGenerator.class,
ILikeExpressionExpression.class, ILikeExpressionExpressionChar.class, ILikeExpressionExpressionExpression.class,
ILikeExpressionString.class, ILikeExpressionStringChar.class, ILikeExpressionStringExpression.class,
StartsWithPatternMethod.class, EndsWithPatternMethod.class, ContainsPatternMethod.class, TrimExpressionMethodGenerator.class);
}
private void generateNamedQueryMethods(final EntityInfo entityInfo, final JDefinedClass generatedClass, final JCodeModel model,
final JFieldVar entityManagerField) {
new NamedQueryMethodGenerator().createMethods(model, generatedClass, generatedClass, null, entityManagerField, true, entityInfo,
null, null);
}
/**
* @param entityInfo
* @param generatedClass
* @param model
*/
private void generateCustomQueryMethods(final EntityInfo entityInfo, final JDefinedClass generatedClass, final JCodeModel model) {
CustomQueryMethodGenerator generator = new CustomQueryMethodGenerator();
generator.createMethods(model, generatedClass, generatedClass, null, null, true, entityInfo, null, null);
}
/**
* @param entityInfo
* @param generatedClass
* @param model
* @param entityClassRef
* @param entityManagerField
* @param entityInfoMap
*/
protected void generateQueryMethods(final EntityInfo entityInfo, final JDefinedClass generatedClass, final JCodeModel model,
final JClass entityClassRef, final JFieldVar entityManagerField, final EntityInformationMap entityInfoMap) {
List queryParameterInfoList = entityInfo.queryParameterInfoList;
for (QueryParameterInfo queryParameterInfo : queryParameterInfoList) {
generateQueryMethod(queryParameterInfo, generatedClass, model, entityClassRef, entityManagerField, entityInfo, entityInfoMap);
}
}
/**
* @param queryParameterInfo
* @param generatedClass
* @param model
* @param entityClassRef
* @param entityManagerField
* @param entityInfoMap
* @param idElements
*/
private void generateQueryMethod(final QueryParameterInfo queryParameterInfo, final JDefinedClass generatedClass,
final JCodeModel model, final JClass entityClassRef, final JFieldVar entityManagerField, final EntityInfo entityInfo,
final EntityInformationMap entityInfoMap) {
MethodGenerator generator = QueryMethodGeneratorFactory.getGenerator(queryParameterInfo.queryParameterType);
generator.createMethods(model, entityClassRef, generatedClass, queryParameterInfo.fieldElement, entityManagerField,
queryParameterInfo.generate, entityInfo, entityInfoMap, queryParameterInfo.mappingType);
}
/**
* @param entityClassRef
* @param generatedClass
*/
protected void defineClass(final EntityInfo entityInfo, final JCodeModel model, final JClass entityClassRef,
final JDefinedClass generatedClass) {
}
/**
* @param entityClassRef
* @param entityInfo
* @return
*/
protected final String getPackageName(final JClass entityClassRef, final EntityInfo entityInfo) {
String basePackage = entityClassRef._package().name();
String subPackageName = entityInfo.subPackageName;
return String.format("%s%s.dao", basePackage, subPackageName);
}
/**
* @param entityInfo
* @param generatedClass
* @param model
* @param entityClassRef
* @param entityManagerField
*/
protected void generateRequiredMethodImplementations(final EntityInfo entityInfo, final JDefinedClass generatedClass,
final JCodeModel model, final JClass entityClassRef, final JFieldVar entityManagerField) {
List extends Class extends MethodGenerator>> generatorClasses = getRequiredGenerators();
for (Class extends MethodGenerator> generatorClass : generatorClasses) {
try {
MethodGenerator generator = generatorClass.newInstance();
generator.createMethods(model, entityClassRef, generatedClass, null, entityManagerField, true, entityInfo, null, null);
} catch (InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @return
*/
private List extends Class extends MethodGenerator>> getRequiredGenerators() {
return Arrays.asList(FindAllQueryMethodGenerator.class, FindByPKQueryMethodGenerator.class);
}
/**
* @param entityInfo
* @param generatedClass
* @param model
*/
protected abstract void generateConstuctor(final EntityInfo entityInfo, final JDefinedClass generatedClass, final JCodeModel model);
/**
* @param entityInfo
* @return
*/
protected abstract String defineGeneratedClassName(final EntityInfo entityInfo);
/**
* @return
*/
protected abstract String getExtensionClass();
/**
* @return
*/
protected abstract boolean isInterface();
}