fr.ird.observe.toolkit.eugene.templates.EntitiesSpiTransformer 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.ImmutableSet;
import fr.ird.observe.entities.ObserveEntity;
import fr.ird.observe.spi.context.DataDtoEntityPersistenceContext;
import fr.ird.observe.spi.context.ReferentialDtoEntityPersistenceContext;
import io.ultreia.java4all.lang.Strings;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.eugene.EugeneCoreTagValues;
import org.nuiton.eugene.java.BeanTransformer;
import org.nuiton.eugene.java.BeanTransformerContext;
import org.nuiton.eugene.java.BeanTransformerTagValues;
import org.nuiton.eugene.java.EugeneJavaTagValues;
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.ObjectModelInterface;
import org.nuiton.eugene.models.object.ObjectModelPackage;
import org.nuiton.topia.templates.TopiaTemplateHelper;
import java.util.Objects;
/**
* Generates Entities (with DataEntityPersistenceContext for each dto to entity.
*
* @author Tony Chemit - [email protected]
* @plexus.component role="org.nuiton.eugene.Template" role-hint="fr.ird.observe.toolkit.eugene.templates.EntitiesSpiTransformer"
*/
public class EntitiesSpiTransformer extends ObjectModelTransformerToJava {
private final EugeneCoreTagValues coreTagValues;
private final EugeneJavaTagValues javaTemplatesTagValues;
private final ObserveTagValues observeTagValues;
private final BeanTransformerTagValues beanTagValues;
private TopiaTemplateHelper templateHelper;
private BeanTransformerContext context;
public EntitiesSpiTransformer() {
coreTagValues = new EugeneCoreTagValues();
javaTemplatesTagValues = new EugeneJavaTagValues();
beanTagValues = new BeanTransformerTagValues();
observeTagValues = new ObserveTagValues();
}
@Override
public void transformFromModel(ObjectModel model) {
if (templateHelper == null) {
templateHelper = new TopiaTemplateHelper(model);
}
super.transformFromModel(model);
ImmutableSet.Builder withReferencesBuilder = ImmutableSet.builder();
ImmutableSet.Builder withFormBuilder = ImmutableSet.builder();
context = new BeanTransformerContext(model, coreTagValues, javaTemplatesTagValues, beanTagValues, false, false, input -> {
ObjectModelPackage aPackage = model.getPackage(input.getPackageName());
boolean referential = BinderHelper.isReferentialFromPackageName(aPackage.getName());
String referencesTagValue = observeTagValues.getReferencesTagValue(input);
String formTagValue = observeTagValues.getFormTagValue(input, getModel().getPackage(input.getPackageName())).trim();
boolean withReference = referencesTagValue != null || referential;
boolean withForm = StringUtils.isNotEmpty(formTagValue);
boolean keep = false;
if (withForm) {
withFormBuilder.add(input.getQualifiedName());
keep = true;
}
if (withReference) {
withReferencesBuilder.add(input.getQualifiedName());
keep = true;
}
return keep;
}, getLog());
context.report();
ImmutableSet withReferenceFqns = withReferencesBuilder.build();
ImmutableSet withFormFqns = withFormBuilder.build();
String entitiesName = model.getName() + "EntitiesSpi";
String generatedEntitiesName = "Generated" + entitiesName;
String entitiesPackageName = ObserveEntity.class.getPackage().getName();
generateGeneratedEntities(entitiesPackageName, generatedEntitiesName, withReferenceFqns, withFormFqns);
boolean generateEntities = notFoundInClassPath(entitiesPackageName, entitiesName);
if (generateEntities) {
generateEntities(entitiesPackageName, generatedEntitiesName, entitiesName);
}
}
private void generateGeneratedEntities(String entitiesPackageName, String generatedInitializerName, ImmutableSet withReferenceFqns, ImmutableSet withFormFqns) {
String modelName = getModel().getName();
ObjectModelInterface output = createInterface(generatedInitializerName, entitiesPackageName);
addImport(output, DataDtoEntityPersistenceContext.class);
addImport(output, ReferentialDtoEntityPersistenceContext.class);
String persistenceContext = modelName + "TopiaPersistenceContext";
addImport(output, entitiesPackageName + "." + persistenceContext);
String[] relativeNameExcludes = context.relativeNameExcludes;
String entityEnumName = templateHelper.getEntityEnumName(model);
addImport(output, entitiesPackageName + "." +entityEnumName);
for (String fqn : context.selectedClassesFqn) {
ObjectModelClass beanClass = context.selectedClassesByFqn.get(fqn);
ObjectModelPackage thisPackage = getModel().getPackage(beanClass);
if (BeanTransformer.skipForInitializer(thisPackage, beanClass)) {
continue;
}
String entityPackage;
String daoName;
String daoMethodName;
String dtoName = thisPackage.getName() + "." + context.classesNameTranslation.get(beanClass);
if (context.selectedClasses.contains(beanClass)) {
String mainDtoTagValue = observeTagValues.getMainDtoTagValue(beanClass);
if (Objects.equals("skip", mainDtoTagValue)) {
continue;
}
String entityName;
if (mainDtoTagValue == null) {
// this is a main dto
entityPackage = getEntityPackage(entitiesPackageName, beanClass);
entityName = entityPackage + "." + beanClass.getName();
daoMethodName = Strings.getRelativeCamelCaseName(getDefaultPackageName(), BinderHelper.getRelativeDtoPackage(beanClass.getQualifiedName() + "Dao"), relativeNameExcludes);
} else {
String mainDtoType = getDefaultPackageName() + "." + mainDtoTagValue;
ObjectModelClass mainDtoClass = context.classesByFqn.get(mainDtoType);
entityPackage = getEntityPackage(entitiesPackageName, mainDtoClass);
entityName = entityPackage + "." + mainDtoClass.getName();
daoMethodName = Strings.getRelativeCamelCaseName(getDefaultPackageName(), BinderHelper.getRelativeDtoPackage(mainDtoClass.getQualifiedName() + "Dao"), relativeNameExcludes);
}
boolean referential = BinderHelper.isReferentialFromPackageName(beanClass.getPackageName());
String constantType = referential ? ReferentialDtoEntityPersistenceContext.class.getSimpleName() : DataDtoEntityPersistenceContext.class.getSimpleName();
String constantValue = referential ? "ReferentialDtoEntityPersistenceContext.of(%s.class, %s, t -> ((%s) t).get%s())" : "DataDtoEntityPersistenceContext.of(%s.class, t -> ((%s) t).get%s())";
boolean withReference = withReferenceFqns.contains(fqn);
boolean withForm = withFormFqns.contains(fqn);
daoName = entityName + "TopiaDao";
String relativeName = BinderHelper.getRelativeDtoPackage(beanClass.getPackageName()).substring(1) + beanClass.getName();
String yo = Strings.getRelativeCamelCaseName(getDefaultPackageName(), beanClass.getQualifiedName(), relativeNameExcludes);
String constantName = getConstantName(relativeName.replaceAll("\\.", "_") + "_spi");
if (withReference) {
String referenceName = BinderHelper.cleanId(dtoName) + "Reference";
constantType += String.format("<%s, %s, %s, %s>", dtoName, referenceName, entityName, daoName);
} else if (withForm) {
constantType += String.format("<%s, ?, %s, %s>", dtoName, entityName, daoName);
} else {
continue;
}
if (referential) {
String literalName = String.format("%s.valueOf(%s.class)", entityEnumName, entityName);
constantValue = String.format(constantValue, dtoName, literalName, persistenceContext, daoMethodName);
} else {
constantValue = String.format(constantValue, dtoName, persistenceContext, daoMethodName);
}
addAttribute(output, constantName, constantType, "\n " + constantValue);
}
}
}
private void generateEntities(String entitiesPackageName, String generatedEntitiesName, String entitiesName) {
ObjectModelInterface output = createInterface(entitiesName, entitiesPackageName);
addInterface(output, generatedEntitiesName);
// setSuperClass(output, generatedEntitiesName);
getLog().debug("will generate " + output.getQualifiedName());
}
private boolean notFoundInClassPath(String input, String className) {
String fqn = input + "." + className;
boolean inClassPath = getResourcesHelper().isJavaFileInClassPath(fqn);
return !inClassPath;
}
private String getEntityPackage(String entitiesPackageName, ObjectModelClass entityClass) {
return entitiesPackageName + BinderHelper.getRelativeDtoPackage(entityClass.getPackageName());
}
}