org.nuiton.topia.templates.ApplicationContextTransformer Maven / Gradle / Ivy
The newest version!
package org.nuiton.topia.templates;
/*
* #%L
* ToPIA :: Templates
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2014 CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import org.codehaus.plexus.component.annotations.Component;
import org.nuiton.eugene.Template;
import org.nuiton.eugene.java.ObjectModelTransformerToJava;
import org.nuiton.eugene.models.object.ObjectModel;
import org.nuiton.eugene.models.object.ObjectModelClass;
import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
import org.nuiton.eugene.models.object.ObjectModelOperation;
import org.nuiton.topia.persistence.TopiaConfiguration;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaEntityEnumProvider;
import org.nuiton.topia.persistence.internal.AbstractTopiaApplicationContext;
import org.nuiton.topia.persistence.internal.AbstractTopiaPersistenceContextConstructorParameter;
import org.nuiton.topia.persistence.util.EntityOperator;
import java.util.Properties;
import java.util.Set;
/**
* To generate PersistenceHelper
*
* @author Tony Chemit - [email protected]
* @since 3.0
*/
@Component( role = Template.class, hint="org.nuiton.topia.templates.ApplicationContextTransformer")
public class ApplicationContextTransformer extends ObjectModelTransformerToJava {
protected TopiaTemplateHelper templateHelper;
protected final TopiaCoreTagValues topiaCoreTagValues;
public ApplicationContextTransformer() {
this.topiaCoreTagValues = new TopiaCoreTagValues();
}
@Override
public void transformFromModel(ObjectModel input) {
if (templateHelper == null) {
templateHelper = new TopiaTemplateHelper(model);
}
String packageName = templateHelper.getApplicationContextPackage(this, model);
String applicationContextAbstractName = templateHelper.getApplicationContextAbstractName(model);
String applicationContextConcreteName = templateHelper.getApplicationContextConcreteName(model);
boolean generateAbstract = !getResourcesHelper().isJavaFileInClassPath(packageName + "." + applicationContextAbstractName);
boolean generateConcrete = !getResourcesHelper().isJavaFileInClassPath(packageName + "." + applicationContextConcreteName);
if (generateAbstract) {
generateAbstract(packageName,
applicationContextAbstractName);
}
if (generateConcrete) {
generateImpl(packageName,
applicationContextAbstractName,
applicationContextConcreteName);
}
}
protected void generateAbstract(String packageName,
String className) {
// try to find a super class by tag-value
String superClass = topiaCoreTagValues.getApplicationContextSuperClassTagValue(model);
if (superClass == null) {
// no super-class, use default one
superClass = AbstractTopiaApplicationContext.class.getName();
} else {
//TODO check that super class instance of ApplicationPersistenceContext
}
ObjectModelClass output = createAbstractClass(className, packageName);
String persistenceContextConcreteName = templateHelper.getPersistenceContextConcreteName(model);
setSuperClass(output, superClass + "<" + persistenceContextConcreteName + ">");
// detect if there is a contract to set on abstract
String contractName = templateHelper.getApplicationContextInterfaceName(model);
boolean addPersistenceContextContract = getResourcesHelper().isJavaFileInClassPath(packageName + "." + contractName);
if (addPersistenceContextContract) {
addInterface(output, packageName + "." + contractName);
}
String modelName = model.getName();
String entityEnumName = modelName + "EntityEnum";
addImport(output, TopiaEntityEnumProvider.class);
addInterface(output, TopiaEntityEnumProvider.class.getName() + "<" + entityEnumName + ">");
boolean generateOperator =
topiaCoreTagValues.getGenerateOperatorForDAOHelperTagValue(model);
addImport(output, TopiaEntity.class);
if (generateOperator) {
addImport(output, EntityOperator.class);
}
addConstructors(output, false);
ObjectModelOperation op;
// newPersistenceContext method
addImport(output, AbstractTopiaPersistenceContextConstructorParameter.class);
op = addOperation(output, "newPersistenceContext", persistenceContextConcreteName, ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
setOperationBody(op, ""
+"\n"
+"\n"
+" "+persistenceContextConcreteName+" newContext = new "+persistenceContextConcreteName+"(\n"
+" new AbstractTopiaPersistenceContextConstructorParameter(\n"
+" getHibernateProvider(),\n"
+" getTopiaFiresSupport(),\n"
+" getTopiaIdFactory(),\n"
+" getSessionRegistry(),\n"
+" getConfiguration().getSlowQueriesThreshold()\n"
+" )\n"
+" );\n"
+" registerPersistenceContext(newContext);\n"
+" return newContext;\n"
+" "
);
// getModelVersion method
String modelVersion = model.getVersion();
op = addOperation(output, "getModelVersion", "String", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
setOperationBody(op, ""
+"\n"
+" return \""+modelVersion+"\";\n"
+" "
);
// getModelName method
op = addOperation(output, "getModelName", "String", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
setOperationBody(op, ""
+"\n"
+" return \""+modelName+"\";\n"
+" "
);
// getContractClass method
op = addOperation(output, "getContractClass", " Class", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
addParameter(op, "Class", "klass");
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getContractClass(klass);\n"
+" "
);
// getContractClasses method
op = addOperation(output, "getContractClasses", "Class extends TopiaEntity>[]", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getContractClasses();\n"
+" "
);
// getImplementationClass method
op = addOperation(output, "getImplementationClass", " Class", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
addParameter(op, "Class", "klass");
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getImplementationClass(klass);\n"
+" "
);
// getImplementationClasses method
addImport(output, Set.class);
op = addOperation(output, "getImplementationClasses", "Set>", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getImplementationClasses();\n"
+" "
);
// getContracts method
op = addOperation(output, "getContracts", entityEnumName + "[]", ObjectModelJavaModifier.PUBLIC);
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getContracts();\n"
+" "
);
if (generateOperator) {
// getOperator method
op = addOperation(output, "getOperator", " EntityOperator", ObjectModelJavaModifier.PUBLIC);
addParameter(op, "Class", "klass");
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".getOperator(klass);\n"
+" "
);
}
op = addOperation(output, "getEntityEnum", " " + entityEnumName,
ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
addParameter(op, "Class", "type");
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".valueOf(type);\n"
+" "
);
op = addOperation(output, "getEntityEnum", entityEnumName,
ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, op, Override.class);
addParameter(op, String.class, "name");
setOperationBody(op, ""
+"\n"
+" return "+entityEnumName+".valueOf(name);\n"
+" "
);
}
protected ObjectModelClass generateImpl(String packageName,
String applicationContextAbstractName,
String applicationContextConcreteName) {
ObjectModelClass output = createClass(applicationContextConcreteName, packageName);
setSuperClass(output, applicationContextAbstractName);
addConstructors(output, true);
return output;
}
protected void addConstructors(ObjectModelClass output, boolean isPublic) {
ObjectModelJavaModifier visibility = isPublic ?
ObjectModelJavaModifier.PUBLIC :
ObjectModelJavaModifier.PROTECTED;
ObjectModelOperation constructor = addConstructor(output, visibility);
addAnnotation(output, constructor, Deprecated.class);
addParameter(constructor, Properties.class, "properties");
setOperationBody(constructor, ""
+"\n"
+" super(properties);\n"
+" "
);
constructor = addConstructor(output, visibility);
addAnnotation(output, constructor, Deprecated.class);
addParameter(constructor, "java.util.Map", "configuration");
setOperationBody(constructor, ""
+"\n"
+" super(configuration);\n"
+" "
);
constructor = addConstructor(output, visibility);
addParameter(constructor, TopiaConfiguration.class, "topiaConfiguration");
setOperationBody(constructor, ""
+"\n"
+" super(topiaConfiguration);\n"
+" "
);
}
}