All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.toolkit.eugene.templates.EntityReferenceTransformer Maven / Gradle / Ivy

There is a newer version: 4.21
Show newest version
package fr.ird.observe.toolkit.eugene.templates;

/*
 * #%L
 * ObServe Toolkit :: Eugene Templates Extension
 * %%
 * Copyright (C) 2017 - 2018 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 com.google.common.collect.ImmutableSet;
import fr.ird.observe.binder.DtoReferenceBinder;
import fr.ird.observe.binder.data.DataDtoReferenceBinder;
import fr.ird.observe.binder.referential.ReferentialDtoReferenceBinder;
import fr.ird.observe.dto.IdDto;
import fr.ird.observe.dto.IdHelper;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.DtoReference;
import fr.ird.observe.dto.reference.DtoReferenceDefinition;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.spi.initializer.EntitiesBinderInitializerSupport;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
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.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 static fr.ird.observe.toolkit.eugene.templates.DtoReferenceTransformer.generateMetaInfService;

/**
 * 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.EntityReferenceTransformer" * @since 1.04 */ public class EntityReferenceTransformer extends ObjectModelTransformerToJava { private final EugeneJavaTagValues javaTemplatesTagValues; private final ObserveTagValues observeTagValues; private final BeanTransformerTagValues beanTagValues; private BeanTransformerContext context; public EntityReferenceTransformer() { javaTemplatesTagValues = new EugeneJavaTagValues(); beanTagValues = new BeanTransformerTagValues(); observeTagValues = new ObserveTagValues(); } @Override public void transformFromModel(ObjectModel model) { super.transformFromModel(model); ImmutableSet.Builder withReferencesBuilder = ImmutableSet.builder(); ImmutableSet.Builder withFormBuilder = ImmutableSet.builder(); context = new BeanTransformerContext(model, javaTemplatesTagValues, beanTagValues, false, false, input -> { ObjectModelPackage aPackage = model.getPackage(input.getPackageName()); boolean referential = IdHelper.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 initializerName = "EntityBindersInitializer"; String generatedInitializerName = "Generated" + initializerName; String initializerPackageName = EntitiesBinderInitializerSupport.class.getPackage().getName(); ObjectModelPackage aPackage = getModel().getPackage(getDefaultPackageName()); String binderPackageName = DtoReferenceBinder.class.getPackage().getName(); generateGeneratedInitializer(initializerPackageName, aPackage.getName(), binderPackageName, generatedInitializerName, withReferenceFqns, withFormFqns); boolean generateInitializer = notFoundInClassPath(binderPackageName, initializerName); if (generateInitializer) { generateInitializer(initializerPackageName, generatedInitializerName, initializerName); } generateMetaInfService(getConfiguration(), initializerPackageName + "." + initializerName, EntitiesBinderInitializerSupport.class); } private void generateGeneratedInitializer(String initializerPackageName, String defaultPackageName, String binderPackageName, String generatedInitializerName, ImmutableSet withReferenceFqns, ImmutableSet withFormFqns) { int defaultPackageNameLength = defaultPackageName.length(); String modelName = getModel().getName(); ObjectModelClass output = createAbstractClass(generatedInitializerName, initializerPackageName); setSuperClass(output, EntitiesBinderInitializerSupport.class); addImport(output, Map.class); addImport(output, ImmutableMap.class); addImport(output, ImmutableSet.class); addImport(output, Set.class); addImport(output, LinkedHashSet.class); addImport(output, TreeMap.class); addImport(output, ReferentialDto.class); addImport(output, DataDto.class); addImport(output, IdDto.class); addImport(output, DtoReference.class); addImport(output, ReferentialDtoReference.class); addImport(output, DataDtoReference.class); addImport(output, DtoReferenceDefinition.class); addImport(output, ReferentialDtoReferenceBinder.class); addImport(output, DataDtoReferenceBinder.class); addImport(output, defaultPackageName + "." + modelName + "ModelInitializer"); addImport(output, defaultPackageName + "." + modelName + "ModelInitializerRunner"); 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" +" " ); for (String fqn : context.selectedClassesFqn) { ObjectModelClass beanClass = context.selectedClassesByFqn.get(fqn); if (BeanTransformer.skipForInitializer(getModel().getPackage(beanClass), beanClass)) { continue; } String dtoName = context.classesNameTranslation.get(beanClass); ObjectModelOperation operation = addOperation(output, "init" + dtoName, "void", ObjectModelJavaModifier.PUBLIC); addAnnotation(output, operation, Override.class); if (context.selectedClasses.contains(beanClass)) { boolean referential = IdHelper.isReferentialFromPackageName(beanClass.getPackageName()); StringBuilder body = new StringBuilder(); boolean withReference = withReferenceFqns.contains(fqn); boolean withForm = withFormFqns.contains(fqn); if (withForm) { // add dto binder String binderMethodName = referential ? "registerDtoReferentialBinder" : "registerDtoDataBinder"; String binderName = BinderHelper.generateBinderName(defaultPackageNameLength, binderPackageName, fqn, beanClass.getName() + "EntityDto", referential); addImport(output, binderName); binderName = JavaGeneratorUtil.getSimpleName(binderName); body.append("" +"\n" +" "+binderMethodName+"(new "+binderName+"());"); } if (withReference) { // add reference binder addImport(output, fqn + "Reference"); String binderMethodName = referential ? "registerReferenceReferentialBinder" : "registerReferenceDataBinder"; String binderName = BinderHelper.generateBinderName(defaultPackageNameLength, binderPackageName, fqn, beanClass.getName() + "EntityReference", referential); addImport(output, binderName); binderName = JavaGeneratorUtil.getSimpleName(binderName); body.append("" +"\n" +" "+binderMethodName+"(new "+binderName+"());"); } setOperationBody(operation, body.toString()+"\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(String input, String className) { String fqn = input + "." + className; boolean inClassPath = getResourcesHelper().isJavaFileInClassPath(fqn); return !inClassPath; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy