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

fr.ird.observe.toolkit.eugene.templates.dto.DtoReferenceTransformer Maven / Gradle / Ivy

The newest version!
package fr.ird.observe.toolkit.eugene.templates.dto;

/*
 * #%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%
 */




import fr.ird.observe.dto.IdHelper;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.DataDtoReferenceDefinition;
import fr.ird.observe.dto.reference.DtoReferenceAware;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceAware;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceDefinition;
import fr.ird.observe.spi.dto.DtoModuleHelper;
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.ObjectModelAttribute;
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.Arrays;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 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.dto.DtoReferenceTransformer" * @since 1.04 */ public class DtoReferenceTransformer extends ObjectModelTransformerToJava { private DtoModel dtoModel; @Override public void transformFromModel(ObjectModel model) { super.transformFromModel(model); dtoModel = new DtoModel( new EugeneCoreTagValues(), new EugeneJavaTagValues(), new BeanTransformerTagValues(), new ToolkitTagValues(), getLog(), model, getDefaultPackageName()); DtoTransformerContext context = dtoModel.referenceContext; context.report(); for (ObjectModelClass input : context.selectedClasses) { ObjectModelPackage aPackage = getPackage(input); boolean referential = IdHelper.isReferentialFromPackageName(aPackage.getName()); String referencesTagValue = dtoModel.toolkitTagValues.getReferencesTagValue(input); if (referencesTagValue == null && referential) { referencesTagValue = "code,label"; } Objects.requireNonNull(referencesTagValue); Set availableProperties = new LinkedHashSet<>(Arrays.asList(referencesTagValue.split(","))); Set properties = DtoHelper.getReferenceProperties(context, input, availableProperties); String prefix = getConstantPrefix(input); setConstantPrefix(prefix); String dtoType = context.classesNameTranslation.get(input); String className = IdHelper.cleanId(dtoType) + "Reference"; String generatedClassName = "Generated" + className; Class superClass = referential ? ReferentialDtoReference.class : DataDtoReference.class; String superClassName = String.format("%s<%s, %s>", superClass.getName(), dtoType, className); generateGeneratedClass(input, className, generatedClassName, dtoType, superClassName, properties, referential); boolean generateClass = notFoundInClassPath(input.getPackageName(), className); if (generateClass) { generateClass(input, className, generatedClassName, properties, referential); } } } private void createGetMethod(ObjectModelClass output, String attrName, String attrType, String methodPrefix) { ObjectModelOperation operation = addOperation( output, getJavaBeanMethodName(methodPrefix, attrName), attrType, ObjectModelJavaModifier.PUBLIC ); setOperationBody(operation, "" +"\n" +" return "+attrName+";\n" +" " ); } private void createProperty(ObjectModelClass output, ObjectModelAttribute attr) { String attrName = attr.getName(); String attrType = dtoModel.context.getAttributeTypeWithGeneric(attr); boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); if (booleanProperty) { createGetMethod(output, attrName, attrType, JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX); } else { createGetMethod(output, attrName, attrType, JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX); } addAttribute(output, attrName, attrType, "", ObjectModelJavaModifier.PRIVATE, ObjectModelJavaModifier.FINAL); } private void createPropertyConstant(ObjectModelClassifier output, ObjectModelAttribute attr, String prefix, Set constantNames) { String attrName = attr.getName(); String constantName = prefix + builder.getConstantName(attrName); if (!constantNames.contains(constantName)) { addConstant(output, constantName, String.class, "\"" + attrName + "\"", ObjectModelJavaModifier.PUBLIC); } } private void generateClass(ObjectModelClass input, String className, String abstractClassName, Set properties, boolean referential) { ObjectModelClass output = createClass(className, input.getPackageName()); setSuperClass(output, abstractClassName); getLog().debug("will generate " + output.getQualifiedName()); ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC); addParameter(constructor, referential ? ReferentialDtoReferenceAware.class : DtoReferenceAware.class, "dto"); StringBuilder body = new StringBuilder("\n super(dto, "); for (ObjectModelAttribute attr : properties) { addParameter(constructor, attr.getType(), attr.getName()); body.append(String.format("%s, ", attr.getName())); } String body1 = body.toString(); setOperationBody(constructor, body1.substring(0, body1.length() - 2) + ");\n"); } private void generateGeneratedClass(ObjectModelClass input, String className, String abstractClassName, String dtoType, String superClass, Set properties, boolean referential) { ObjectModelClass output = createAbstractClass(abstractClassName, input.getPackageName()); setSuperClass(output, superClass); getLog().debug("will generate " + output.getQualifiedName()); ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PROTECTED); addParameter(constructor, referential ? ReferentialDtoReferenceAware.class : DtoReferenceAware.class, "dto"); StringBuilder body = new StringBuilder(""+"\n" +" super(dto);"); String prefix = getConstantPrefix(input); setConstantPrefix(prefix); Set constantNames = addConstantsFromDependency(input, output); // Add properties constant for (ObjectModelAttribute attr : properties) { createPropertyConstant(output, attr, prefix, constantNames); } for (ObjectModelAttribute attr : properties) { createProperty(output, attr); addParameter(constructor, attr.getType(), attr.getName()); String name = attr.getName(); body.append(""+"\n" +" this."+name+" = "+name+";" ); } body.append(""+"\n" +" "); setOperationBody(constructor, body.toString()); String dtoDefinitionType = input.getName() + "DtoDefinition"; addAttribute(output, "DEFINITION", dtoDefinitionType, null, ObjectModelJavaModifier.PRIVATE, ObjectModelJavaModifier.STATIC); addImport(output, DtoModuleHelper.class); String dtoTypeSimpleName = GeneratorUtil.getSimpleName(dtoType); ObjectModelOperation definitionMethod = addOperation(output, "definition", dtoDefinitionType, ObjectModelJavaModifier.PUBLIC, ObjectModelJavaModifier.STATIC, ObjectModelJavaModifier.FINAL); setOperationBody(definitionMethod, ""+"\n" +" if (DEFINITION == null) {\n" +" DEFINITION = ("+dtoDefinitionType+") DtoModuleHelper.get().getDefinition("+dtoTypeSimpleName+".class);\n" +" }\n" +" return DEFINITION;\n" +" "); if (!input.isAbstract()) { addGetterProducerInterface(output, className, true); addGetterProducerMethods(output, className, true, "definition().getReferenceGetters()"); } Class builderMethodName = referential ? ReferentialDtoReferenceDefinition.class : DataDtoReferenceDefinition.class; addImport(output, builderMethodName); String referenceDefinitionType = String.format("%s<%s, %s>", builderMethodName.getName(), dtoType, className); ObjectModelOperation referenceDefinitionMethod = addOperation(output, "referenceDefinition", referenceDefinitionType, ObjectModelJavaModifier.PUBLIC); addAnnotation(output, referenceDefinitionMethod, Override.class); setOperationBody(referenceDefinitionMethod, ""+"\n" +" return definition().toReferenceDefinition();\n" +" " ); } 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