fr.ird.observe.toolkit.eugene.templates.DtoToMainDtoTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eugene-templates-extension Show documentation
Show all versions of eugene-templates-extension Show documentation
ObServe Eugene templates extension module
package fr.ird.observe.toolkit.eugene.templates;
/*
* #%L
* ObServe Toolkit :: Eugene Templates Extension
* %%
* Copyright (C) 2017 - 2019 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%
*/
import com.google.common.collect.ImmutableMap;
import fr.ird.observe.dto.IdDto;
import fr.ird.observe.dto.reference.DtoReference;
import fr.ird.observe.spi.initializer.DtoToMainDtoInitializerSupport;
import io.ultreia.java4all.i18n.I18n;
import org.nuiton.eugene.EugeneCoreTagValues;
import org.nuiton.eugene.java.BeanTransformerContext;
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.ObjectModelJavaModifier;
import org.nuiton.eugene.models.object.ObjectModelOperation;
import org.nuiton.eugene.models.object.ObjectModelPackage;
import java.util.Objects;
import static fr.ird.observe.toolkit.eugene.templates.DtoReferenceTransformer.generateMetaInfService;
/**
* Generates a reference of a bean.
*
* For example:
*
* GeneratedDtoFormDefinitionInitializer
* DtoFormDefinitionInitializer
*
*
* @author Tony Chemit - [email protected]
* @plexus.component role="org.nuiton.eugene.Template" role-hint="fr.ird.observe.toolkit.eugene.templates.DtoToMainDtoTransformer"
* @since 1.04
*/
public class DtoToMainDtoTransformer extends ObjectModelTransformerToJava {
private final EugeneCoreTagValues coreTagValues;
private final EugeneJavaTagValues javaTemplatesTagValues;
private final BeanTransformerTagValues beanTagValues;
private final ObserveTagValues observeTagValues;
private BeanTransformerContext context;
public DtoToMainDtoTransformer() {
coreTagValues = new EugeneCoreTagValues();
javaTemplatesTagValues = new EugeneJavaTagValues();
beanTagValues = new BeanTransformerTagValues();
observeTagValues = new ObserveTagValues();
}
@Override
public void transformFromModel(ObjectModel model) {
super.transformFromModel(model);
context = new BeanTransformerContext(model, coreTagValues, javaTemplatesTagValues, beanTagValues, false, false, input -> true, getLog());
context.report();
String initializerName = "DtoToMainDtoInitializer";
String generatedInitializerName = "Generated" + initializerName;
String initializerPackageName = DtoToMainDtoInitializerSupport.class.getPackage().getName();
ObjectModelPackage aPackage = getModel().getPackage(getDefaultPackageName());
generateGeneratedInitializer(initializerPackageName, aPackage, generatedInitializerName);
boolean generateInitializer = notFoundInClassPath(aPackage, initializerName);
if (generateInitializer) {
generateInitializer(initializerPackageName, generatedInitializerName, initializerName);
}
generateMetaInfService(getConfiguration(), initializerPackageName + "." + initializerName, DtoToMainDtoInitializerSupport.class);
}
private void generateGeneratedInitializer(String initializerPackageName, ObjectModelPackage aPackage, String generatedInitializerName) {
String packageName = aPackage.getName() + ".";
String modelName = getModel().getName();
ObjectModelClass output = createAbstractClass(generatedInitializerName, initializerPackageName);
addImport(output, ImmutableMap.class);
addImport(output, IdDto.class);
addImport(output, DtoReference.class);
addImport(output, I18n.class);
addImport(output, packageName + modelName + "ModelInitializer");
addImport(output, packageName + modelName + "ModelInitializerRunner");
setSuperClass(output, DtoToMainDtoInitializerSupport.class);
addInterface(output, modelName + "ModelInitializer");
ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC);
setOperationBody(constructor, ""+"\n"
+" "+modelName+"ModelInitializerRunner.init(this);\n"
+" "
);
ObjectModelOperation startMethod = addOperation(output, "start", "void", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, startMethod, Override.class);
ObjectModelOperation endMethod = addOperation(output, "end", "void", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, endMethod, Override.class);
setOperationBody(endMethod, ""+"\n"
+" super.end();\n"
+" "
);
boolean useRelativeName = context.useRelativeName;
String[] relativeNameExcludes = context.relativeNameExcludes;
for (ObjectModelClass beanClass : context.selectedClasses) {
String dtoType = beanClass.getPackageName() +"."+ context.classesNameTranslation.get(beanClass);
String mainDtoTagValue = observeTagValues.getMainDtoTagValue(beanClass);
String content;
if (Objects.equals("skip", mainDtoTagValue)) {
content="";
if (!useRelativeName) {
dtoType = JavaGeneratorUtil.getSimpleName(dtoType);
}
} else {
String mainDtoType;
if (mainDtoTagValue == null) {
// this is a main dto
mainDtoType = dtoType;
} else {
mainDtoType = getDefaultPackageName() + "." + mainDtoTagValue;
ObjectModelClass mainDtoClass = context.classesByFqn.get(mainDtoType);
mainDtoType = mainDtoClass.getPackageName() + "." + context.classesNameTranslation.get(mainDtoClass);
}
if (isVerbose()) {
getLog().info(String.format("DtoToMainDtoTagValue: %s → %s", dtoType, mainDtoType));
}
Objects.requireNonNull(mainDtoType);
if (!useRelativeName) {
addImport(output, dtoType);
addImport(output, mainDtoType);
dtoType = JavaGeneratorUtil.getSimpleName(dtoType);
mainDtoType = JavaGeneratorUtil.getSimpleName(mainDtoType);
}
content = String.format("addClassMapping(%s.class, %s.class);",dtoType, mainDtoType);
}
String methodName = getMethodName(useRelativeName, relativeNameExcludes, "init", null, dtoType);
ObjectModelOperation operation = addOperation(output, methodName, "void", ObjectModelJavaModifier.PUBLIC);
addAnnotation(output, operation, Override.class);
setOperationBody(operation, ""+"\n"
+" "+content+"\n"
+" ");
}
}
private void generateInitializer(String initializerPackageName, String generatedInitializerName, String initializerName) {
ObjectModelClass output = createClass(initializerName, initializerPackageName);
setSuperClass(output, generatedInitializerName);
getLog().debug("will generate " + output.getQualifiedName());
}
private boolean notFoundInClassPath(ObjectModelPackage input, String className) {
String fqn = input.getName() + "." + className;
boolean inClassPath = getResourcesHelper().isJavaFileInClassPath(fqn);
return !inClassPath;
}
}