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

fr.ird.observe.toolkit.eugene.templates.dto.ApplicationDataContextTransformer 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.spi.application.ApplicationDataContext;
import fr.ird.observe.spi.application.ApplicationDataContextSupport;
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.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;

/**
 * Generates implementation of {@link ApplicationDataContext}.
 *
 * @author Tony Chemit - [email protected]
 * @plexus.component role="org.nuiton.eugene.Template" role-hint="fr.ird.observe.toolkit.eugene.templates.dto.ApplicationDataContextTransformer"
 */
public class ApplicationDataContextTransformer extends ObjectModelTransformerToJava {

    @Override
    public void transformFromModel(ObjectModel model) {

        super.transformFromModel(model);

        DtoModel dtoModel = new DtoModel(new EugeneCoreTagValues(),
                new EugeneJavaTagValues(),
                new BeanTransformerTagValues(),
                new ToolkitTagValues(),
                getLog(),
                model,
                getDefaultPackageName());

        DtoTransformerContext context = dtoModel.context;
        context.report();

        ObjectModelClass output = createClass(model.getName() + "ApplicationDataContext", ApplicationDataContext.class.getPackage().getName());
        setSuperClass(output, ApplicationDataContextSupport.class);

        for (ObjectModelClass input : context.selectedClasses) {

            if (input.isAbstract()) {
                continue;
            }
            int selectedPriority = dtoModel.toolkitTagValues.getSelectedTagValue(input);
            if (selectedPriority > -1) {
                if (isVerbose()) {
                    getLog().info("Detect selected dto: " + input.getQualifiedName());
                }
                int openPriority = dtoModel.toolkitTagValues.getOpenTagValue(input);

                boolean referential = IdHelper.isReferentialFromPackageName(input.getPackageName());

                addSelectedMethods(output, input, openPriority > -1, referential);
            }
        }
    }

    private void addSelectedMethods(ObjectModelClass output, ObjectModelClass input, boolean useOpen, boolean referential) {

        String propertyName = input.getName();

        String dtoType = input.getQualifiedName() + "Dto";
        String dtoTypeSimpleName = JavaGeneratorUtil.getSimpleName(dtoType);

        addImport(output, dtoType);

        ObjectModelOperation isSelected = addOperation(output, "isSelected" + propertyName, boolean.class, ObjectModelJavaModifier.PUBLIC);
        setOperationBody(isSelected, ""+"\n"
+"        return isSelected("+dtoTypeSimpleName+".class);\n"
+"    ");
        String getSelectedIdMethodName = "getSelected" + propertyName + "Id";
        ObjectModelOperation getSelected = addOperation(output, getSelectedIdMethodName, String.class, ObjectModelJavaModifier.PUBLIC);
        setOperationBody(getSelected, ""+"\n"
+"        return getSelectedId("+dtoTypeSimpleName+".class);\n"
+"    ");
        ObjectModelOperation setSelected = addOperation(output, "setSelected" + propertyName + "Id", void.class, ObjectModelJavaModifier.PUBLIC);
        addParameter(setSelected, String.class, "selected" + propertyName + "Id");
        setOperationBody(setSelected, ""+"\n"
+"        setSelectedId("+dtoTypeSimpleName+".class, selected"+propertyName+"Id);\n"
+"    ");
        String methodName = referential ? "getReferential" : "getData";
        ObjectModelOperation getSelectedData = addOperation(output, "getCurrent" + propertyName, dtoType, ObjectModelJavaModifier.PUBLIC);
        if (referential) {
            setOperationBody(getSelectedData, ""+"\n"
+"        return getReferential("+dtoTypeSimpleName+".class, "+getSelectedIdMethodName+"());\n"
+"    ");
        } else {
            setOperationBody(getSelectedData, ""+"\n"
+"        return getData("+dtoTypeSimpleName+".class);\n"
+"    ");
        }

        if (useOpen) {

            ObjectModelOperation isOpen = addOperation(output, "isOpen" + propertyName, boolean.class, ObjectModelJavaModifier.PUBLIC);
            setOperationBody(isOpen, ""+"\n"
+"        return isOpen("+dtoTypeSimpleName+".class);\n"
+"    ");
            ObjectModelOperation isOpenId = addOperation(output, "isOpen" + propertyName + "Id", boolean.class, ObjectModelJavaModifier.PUBLIC);
            addParameter(isOpenId, String.class, "id");
            setOperationBody(isOpenId, ""+"\n"
+"        return isOpenId("+dtoTypeSimpleName+".class, id);\n"
+"    ");
            ObjectModelOperation canOpen = addOperation(output, "canOpen" + propertyName, boolean.class, ObjectModelJavaModifier.PUBLIC);
            setOperationBody(canOpen, ""+"\n"
+"        return canOpen("+dtoTypeSimpleName+".class);\n"
+"    ");
            ObjectModelOperation isSelectedOpen = addOperation(output, "isSelectedOpen" + propertyName, boolean.class, ObjectModelJavaModifier.PUBLIC);
            setOperationBody(isSelectedOpen, ""+"\n"
+"        return isSelectedOpen("+dtoTypeSimpleName+".class);\n"
+"    ");
            ObjectModelOperation getOpen = addOperation(output, "getOpen" + propertyName + "Id", String.class, ObjectModelJavaModifier.PUBLIC);
            setOperationBody(getOpen, ""+"\n"
+"        return getOpenId("+dtoTypeSimpleName+".class);\n"
+"    ");
            ObjectModelOperation setOpen = addOperation(output, "setOpen" + propertyName + "Id", void.class, ObjectModelJavaModifier.PUBLIC);
            addParameter(setOpen, String.class, "open" + propertyName + "Id");
            setOperationBody(setOpen, ""+"\n"
+"        setOpenId("+dtoTypeSimpleName+".class, open"+propertyName+"Id);\n"
+"    ");
            ObjectModelOperation close = addOperation(output, "close" + propertyName, void.class, ObjectModelJavaModifier.PUBLIC);
            setOperationBody(close, ""+"\n"
+"        close("+dtoTypeSimpleName+".class);\n"
+"    ");
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy