fr.ird.observe.toolkit.eugene.templates.persistence.EntityDefinitionTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eugene-templates Show documentation
Show all versions of eugene-templates Show documentation
ObServe Eugene templates module
The newest version!
/*
* #%L
* ObServe Toolkit :: Eugene Templates
* %%
* Copyright (C) 2008 - 2017 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
package fr.ird.observe.toolkit.eugene.templates.persistence;
import fr.ird.observe.binder.DtoReferenceBinder;
import fr.ird.observe.dto.IdHelper;
import fr.ird.observe.spi.persistence.CommentableDataEntityDefinition;
import fr.ird.observe.spi.persistence.DataEntityDefinition;
import fr.ird.observe.spi.persistence.PersistenceModuleHelper;
import fr.ird.observe.spi.persistence.ReferentialEntityDefinition;
import fr.ird.observe.toolkit.eugene.templates.DtoHelper;
import fr.ird.observe.toolkit.eugene.templates.DtoModel;
import fr.ird.observe.toolkit.eugene.templates.DtoTransformerContext;
import fr.ird.observe.toolkit.eugene.templates.ToolkitTagValues;
import org.nuiton.eugene.EugeneCoreTagValues;
import org.nuiton.eugene.GeneratorUtil;
import org.nuiton.eugene.java.BeanTransformerTagValues;
import org.nuiton.eugene.java.EugeneJavaTagValues;
import org.nuiton.eugene.java.JavaGeneratorUtil;
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.ObjectModelClassifier;
import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
import org.nuiton.eugene.models.object.ObjectModelOperation;
import org.nuiton.eugene.models.object.ObjectModelPackage;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
/**
* Generates a reference of a bean.
*
* For example:
*
* GeneratedBoatReference
* BoatReference
*
*
* @author Tony Chemit - [email protected]
* @plexus.component role="org.nuiton.eugene.Template" role-hint="fr.ird.observe.toolkit.eugene.templates.persistence.EntityDefinitionTransformer"
* @since 1.04
*/
public class EntityDefinitionTransformer extends ObjectModelTransformerToJava {
@Override
public void transformFromModel(ObjectModel model) {
super.transformFromModel(model);
DtoModel dtoModel = new DtoModel(new EugeneCoreTagValues(),
new EugeneJavaTagValues(),
new BeanTransformerTagValues(),
new ToolkitTagValues(),
getLog(),
model,
getDefaultPackageName());
DtoTransformerContext context = dtoModel.context;
context.report();
List referentialDefinitionClasses = new LinkedList<>();
List dataDefinitionClasses = new LinkedList<>();
for (ObjectModelClass input : context.selectedClasses) {
if (input.isAbstract()) {
continue;
}
ObjectModelPackage aPackage = getPackage(input);
String dtoType = input.getQualifiedName() + "Dto";
String dtoTypeSimpleName = JavaGeneratorUtil.getSimpleName(dtoType);
String definitionType = input.getQualifiedName() + "EntityDefinition";
boolean referential = IdHelper.isReferentialFromPackageName(aPackage.getName());
if (referential) {
generateReferentialContextClass(input, dtoType, dtoTypeSimpleName, definitionType);
referentialDefinitionClasses.add(input);
} else {
String entityTagValue = dtoModel.toolkitTagValues.getEntityTagValue(input);
String entityType = entityTagValue != null ? entityTagValue : IdHelper.getDataEntityFromDto(dtoType);
if (!getResourcesHelper().isJavaFileInOutputDirectory(entityType)) {
// Not an entity
continue;
}
generateDataContextClass(input, dtoType, dtoTypeSimpleName, entityType, definitionType);
dataDefinitionClasses.add(input);
}
}
getLog().info(String.format("Found %d referential entities.", referentialDefinitionClasses.size()));
getLog().info(String.format("Found %d data entities.", dataDefinitionClasses.size()));
Function function = i -> i.getQualifiedName() + "EntityDefinition";
storeServiceLoaderFile(DataEntityDefinition.class, dataDefinitionClasses, function);
storeServiceLoaderFile(ReferentialEntityDefinition.class, referentialDefinitionClasses, function);
}
private void generateReferentialContextClass(ObjectModelClass input, String dtoType, String dtoTypeSimpleName, String definitionType) {
ObjectModelClass output = createClass(GeneratorUtil.getSimpleName(definitionType), input.getPackageName());
addImport(output, ReferentialEntityDefinition.class);
String entityType = IdHelper.getReferentialEntityFromDto(dtoType);
addImport(output, entityType);
String binderPackageName = DtoReferenceBinder.class.getPackage().getName();
String referenceType = dtoType.replace("Dto", "Reference");
String dtoBinderType = DtoHelper.generateBinderName(IdHelper.DTO_ROOT_PACKAGE_LENGTH - 1, binderPackageName, dtoType, dtoTypeSimpleName.replace("Dto", "") + "EntityDto", true);
getLog().debug("Found dto binder type: " + dtoBinderType);
String dtoBinderInstance = format(output, "new %s()", dtoBinderType);
String referenceBinderType = DtoHelper.generateBinderName(IdHelper.DTO_ROOT_PACKAGE_LENGTH - 1, binderPackageName, dtoType, dtoTypeSimpleName.replace("Dto", "") + "EntityReference", true);
getLog().debug("Found reference binder type: " + referenceBinderType);
String referenceBinderInstance = format(output, "new %s()", referenceBinderType);
String daoInstance = getDaoInstance(output, entityType);
String entityContextType = ReferentialEntityDefinition.class.getName() + format(output, "<%s, %s, %s, %s>", dtoType, referenceType, entityType, entityType + "TopiaDao");
setSuperClass(output, entityContextType);
addImport(output, PersistenceModuleHelper.class);
entityContextType = GeneratorUtil.getSimpleName(entityContextType);
addAttribute(output, "INSTANCE", output.getName(), null, ObjectModelJavaModifier.PRIVATE, ObjectModelJavaModifier.STATIC);
ObjectModelOperation get = addOperation(output, "get", output.getName(), ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(get, ""+"\n"
+" if (INSTANCE == null) {\n"
+" ReferentialEntityDefinition context = PersistenceModuleHelper.fromReferentialDto("+dtoTypeSimpleName+".class);\n"
+" INSTANCE = ("+output.getName()+") context;\n"
+" }\n"
+" return INSTANCE;\n"
+" ");
ObjectModelOperation dtoBinder = addOperation(output, "dtoBinder", dtoBinderType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(dtoBinder, ""+"\n"
+" return ("+JavaGeneratorUtil.getSimpleName(dtoBinderType)+") get().toEntityBinder();\n"
+" ");
ObjectModelOperation referenceBinder = addOperation(output, "referenceBinder", referenceBinderType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(referenceBinder, ""+"\n"
+" return ("+JavaGeneratorUtil.getSimpleName(referenceBinderType)+") get().toEntityReferenceBinder();\n"
+" ");
ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC);
String body = format(output, "super(%s, %s, %s);", dtoBinderInstance, referenceBinderInstance, daoInstance);
setOperationBody(constructor, ""+"\n"
+" "+body+"\n"
+" ");
}
private void generateDataContextClass(ObjectModelClass input, String dtoType, String dtoTypeSimpleName, String entityType, String definitionType) {
String binderPackageName = DtoReferenceBinder.class.getPackage().getName();
String referenceType = dtoType.replace("Dto", "Reference");
String dtoBinderType = DtoHelper.generateBinderName(IdHelper.DTO_ROOT_PACKAGE_LENGTH - 1, binderPackageName, dtoType, dtoTypeSimpleName.replace("Dto", "") + "EntityDto", false);
boolean dtoBinderFound = getResourcesHelper().isFullyQualifiedNameInClassPath(dtoBinderType, ".java");
String referenceBinderType = DtoHelper.generateBinderName(IdHelper.DTO_ROOT_PACKAGE_LENGTH - 1, binderPackageName, dtoType, dtoTypeSimpleName.replace("Dto", "") + "EntityReference", false);
boolean referenceBinderFound = getResourcesHelper().isFullyQualifiedNameInClassPath(referenceBinderType, ".java");
if (!referenceBinderFound && !dtoBinderFound) {
return;
}
Class parentClass = DataEntityDefinition.class;
for (ObjectModelClass objectModelClass : input.getSuperclasses()) {
if ("Openable".equals(objectModelClass.getName()) || "Commentable".equals(objectModelClass.getName())) {
parentClass = CommentableDataEntityDefinition.class;
break;
}
}
ObjectModelClass output = createClass(GeneratorUtil.getSimpleName(definitionType), input.getPackageName());
addImport(output, parentClass);
addImport(output, PersistenceModuleHelper.class);
addImport(output, DataEntityDefinition.class);
String dtoBinderInstance;
if (dtoBinderFound) {
getLog().debug("Found dto binder type: " + dtoBinderType);
dtoBinderInstance = format(output, "new %s()", dtoBinderType);
} else {
dtoBinderInstance = null;
}
String daoInstance = getDaoInstance(output, entityType);
String referenceBinderInstance;
String entityContextType;
String getterType;
if (referenceBinderFound) {
getLog().debug("Found reference binder type: " + referenceBinderType);
referenceBinderInstance = format(output, "new %s()", referenceBinderType);
entityContextType = parentClass.getName() + format(output, "<%s, %s, %s, %s>", dtoType, referenceType, entityType, entityType + "TopiaDao");
getterType = output.getName();
} else {
referenceBinderInstance = null;
entityContextType = parentClass.getName();
getterType = parentClass.getName() + format(output, "<%s, ?, %s, %s>", dtoType, entityType, entityType + "TopiaDao");
}
setSuperClass(output, entityContextType);
entityContextType = GeneratorUtil.getSimpleName(entityContextType);
addAttribute(output, "INSTANCE", getterType, null, ObjectModelJavaModifier.PRIVATE, ObjectModelJavaModifier.STATIC);
ObjectModelOperation get = addOperation(output, "get", getterType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(get, ""+"\n"
+" if (INSTANCE == null) {\n"
+" DataEntityDefinition context = PersistenceModuleHelper.fromDataDto("+dtoTypeSimpleName+".class);\n"
+" INSTANCE = ("+output.getName()+") context;\n"
+" }\n"
+" return INSTANCE;\n"
+" ");
ObjectModelOperation dtoBinder = addOperation(output, "dtoBinder", dtoBinderType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(dtoBinder, ""+"\n"
+" return ("+JavaGeneratorUtil.getSimpleName(dtoBinderType)+") get().toEntityBinder();\n"
+" ");
if (referenceBinderFound) {
ObjectModelOperation referenceBinder = addOperation(output, "referenceBinder", referenceBinderType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL);
setOperationBody(referenceBinder, ""+"\n"
+" return ("+JavaGeneratorUtil.getSimpleName(referenceBinderType)+") get().toEntityReferenceBinder();\n"
+" ");
}
ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC);
String body = format(output, "super(%s, %s, %s);", dtoBinderInstance, referenceBinderInstance, daoInstance);
setOperationBody(constructor, ""+"\n"
+" "+body+"\n"
+" ");
}
private String getDaoInstance(ObjectModelClass output, String entityType) {
//FIXME Configure this
String packageName = "fr.ird.observe.persistence";
//FIXME Use Topia template helper
String persistenceContextInterfaceName = packageName + "." + model.getName() + "TopiaPersistenceContext";
return format(output, "t -> ((%s) t).get%sDao()", persistenceContextInterfaceName, entityType);
}
private String format(ObjectModelClass output, String format, String... types) {
for (int i = 0; i < types.length; i++) {
String type = types[i];
if (type == null || type.equals("?") || type.contains("(") || type.contains("<")) {
continue;
}
addImport(output, type);
types[i] = JavaGeneratorUtil.getSimpleName(type);
}
return String.format(format, (Object[]) types);
}
}