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

fr.ird.observe.toolkit.eugene.templates.DtoFormTransformer 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 - 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.IdHelper;
import fr.ird.observe.dto.reference.DtoReference;
import fr.ird.observe.spi.initializer.DtoFormsInitializerSupport;
import io.ultreia.java4all.i18n.spi.builder.I18nKeySet;
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.ObjectModelAttribute;
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.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

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.DtoFormTransformer" * @since 1.04 */ public class DtoFormTransformer extends ObjectModelTransformerToJava { private final EugeneCoreTagValues coreTagValues; private final EugeneJavaTagValues javaTemplatesTagValues; private final BeanTransformerTagValues beanTagValues; private final ObserveTagValues observeTagValues; private BeanTransformerContext context; public DtoFormTransformer() { 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 -> { ObjectModelPackage aPackage = model.getPackage(input.getPackageName()); String formTagValue = observeTagValues.getFormTagValue(input, aPackage); return formTagValue != null; }, getLog()); context.report(); I18nKeySet i18nGetterFile = getConfiguration().getI18nGetterFile(); String initializerName = "DtoFormDefinitionsInitializer"; String generatedInitializerName = "Generated" + initializerName; String initializerPackageName = DtoFormsInitializerSupport.class.getPackage().getName(); ObjectModelPackage aPackage = getModel().getPackage(getDefaultPackageName()); ObjectModelPackage formPackage = getModel().getPackage(getDefaultPackageName() + ".form"); generateGeneratedInitializer(initializerPackageName, aPackage, formPackage, generatedInitializerName, i18nGetterFile); boolean generateInitializer = notFoundInClassPath(formPackage, initializerName); if (generateInitializer) { generateInitializer(initializerPackageName, generatedInitializerName, initializerName); } generateMetaInfService(getConfiguration(), initializerPackageName + "." + initializerName, DtoFormsInitializerSupport.class); } private void generateGeneratedInitializer(String initializerPackageName, ObjectModelPackage aPackage, ObjectModelPackage formPackage, String generatedInitializerName, I18nKeySet i18nGetterFile) { 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, DtoFormsInitializerSupport.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) { ObjectModelPackage thisPackage = getPackage(beanClass); String formTagValue = observeTagValues.getFormTagValue(beanClass, thisPackage).trim(); getLog().debug("FormTagValue: " + formTagValue); Objects.requireNonNull(formTagValue); String dtoType = thisPackage.getName() + "." + context.classesNameTranslation.get(beanClass); String methodName = getMethodName(useRelativeName, relativeNameExcludes, "init", null, dtoType); ObjectModelOperation operation = addOperation(output, methodName, "void", ObjectModelJavaModifier.PUBLIC); addAnnotation(output, operation, Override.class); StringBuilder bodyBuilder = new StringBuilder(); String formType; boolean referential = IdHelper.isReferentialFromPackageName(thisPackage.getName()); Map properties; if (formTagValue.equals("self")) { formType = dtoType; properties = getProperties(useRelativeName, beanClass, output); getLog().debug(String.format("form: %s, found %d properties.", formType, properties.size())); if (referential || !properties.isEmpty()) { String builderMethod = referential ? "referentialFormBuilder" : "formBuilder"; bodyBuilder.append(""+"\n" +" "+builderMethod+"("+dtoType+".class)"); for (Map.Entry entry : properties.entrySet()) { String propertyName = entry.getKey(); String referenceType = entry.getValue(); propertyName = JavaGeneratorUtil.convertVariableNameToConstantName("property" + JavaGeneratorUtil.capitalizeJavaBeanPropertyName(propertyName)); bodyBuilder.append(""+"\n" +" .addProperty("+dtoType+"."+propertyName+", "+referenceType+".class)"); } if (!properties.isEmpty()) { bodyBuilder.append(""+"\n" +" "); } bodyBuilder.append(""+" .build(formDefinitionsBuilder);\n" +" "); if (!referential) { i18nGetterFile.addKey("observe.common." + JavaGeneratorUtil.getSimpleName(dtoType) + ".title"); } } } else { ObjectModelClass aClass = getModel().getClass(packageName + formTagValue); formType = aClass.getPackageName() + "." + context.classesNameTranslation.get(aClass); } if (!useRelativeName) { addImport(output, formType); formType = JavaGeneratorUtil.getSimpleName(formType); } bodyBuilder.append(""+"\n" +" addClassMapping("+dtoType+".class, "+formType+".class);\n" +" "); setOperationBody(operation, bodyBuilder.toString()); } } 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; } private Map getProperties(boolean useRelativeName, ObjectModelClass input, ObjectModelClass output) { Collection attributes = new LinkedList<>(input.getAttributes()); attributes.addAll(input.getAllOtherAttributes()); Map properties = new TreeMap<>(); for (ObjectModelAttribute attr : attributes) { if (!attr.isNavigable()) { continue; } String type = attr.getType(); if (!type.endsWith("Reference") || !type.contains("referential")) { continue; } if (useRelativeName) { properties.put(attr.getName(), type); } else { addImport(output, type); properties.put(attr.getName(), JavaGeneratorUtil.getSimpleName(type)); } } return properties; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy