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

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

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

/*-
 * #%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 com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.nuiton.eugene.EugeneCoreTagValues;
import org.nuiton.eugene.LogProxy;
import org.nuiton.eugene.java.BeanTransformerTagValues;
import org.nuiton.eugene.java.EugeneJavaTagValues;
import org.nuiton.eugene.java.JavaGeneratorUtil;
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.ObjectModelPackage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

/**
 * Created by tchemit on 31/08/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class DtoTransformerContext {

    // all beans classes with their fully qualified name
    public final ImmutableMap classesNameTranslation;

    // all bean classes indexed by their fully qualified name
    public final ImmutableMap classesByFqn;

    // all bean classes fqn
    public final ImmutableList classesFqn;

    // selected bean classes
    public final ImmutableSet selectedClasses;

    // excluded bean classes
    public final ImmutableSet excludedClasses;

    // select bean classes fqn
    public final ImmutableList selectedClassesFqn;

    // selected bean classes indexed by their fully qualified name
    public final ImmutableMap selectedClassesByFqn;

    public final ObjectModelPackage defaultPackage;
    public final boolean useJava8;

    private final EugeneCoreTagValues eugeneTagValues;
    private final LogProxy log;
    public final ObjectModel model;
    public final EugeneJavaTagValues javaTemplatesTagValues;

    public DtoTransformerContext(ObjectModel model, String defaultPackageName, EugeneCoreTagValues eugeneTagValues, EugeneJavaTagValues javaTemplatesTagValues, BeanTransformerTagValues beanTagValues, boolean includeAbstract, boolean includeSkip, Predicate extraPredicate, LogProxy log) {
        this.eugeneTagValues = eugeneTagValues;
        this.log = log;
        this.model = model;
        this.javaTemplatesTagValues = javaTemplatesTagValues;
        useJava8 = javaTemplatesTagValues.isUseJava8(model);
        defaultPackage = model.getPackage(defaultPackageName);
        ImmutableMap.Builder classesNameTranslationBuilder = new ImmutableMap.Builder<>();
        ImmutableSet.Builder classesBuilder = new ImmutableSet.Builder<>();
        ImmutableSet.Builder excludedClassesBuilder = new ImmutableSet.Builder<>();

        for (ObjectModelClass aClass : model.getClasses()) {

            ObjectModelPackage aPackage = model.getPackage(aClass.getPackageName());
            if (javaTemplatesTagValues.isBean(aClass, aPackage)) {

                String classNamePrefix = beanTagValues.getClassNamePrefixTagValue(aClass, aPackage, model);
                String classNameSuffix = beanTagValues.getClassNameSuffixTagValue(aClass, aPackage, model);

                String generateName = JavaGeneratorUtil.generateName(classNamePrefix, aClass.getName(), classNameSuffix);
                classesNameTranslationBuilder.put(aClass, generateName);

                if (!includeSkip && EugeneCoreTagValues.isSkip(aClass, aPackage)) {
                    this.log.debug("Exclude by skip: " + aClass.getQualifiedName());
                    excludedClassesBuilder.add(aClass);
                    continue;
                }
                if (!includeAbstract && aClass.isAbstract()) {
                    this.log.debug("Exclude by abstract: " + aClass.getQualifiedName());
                    excludedClassesBuilder.add(aClass);
                    continue;
                }

                if (extraPredicate != null && !extraPredicate.test(aClass)) {

                    this.log.debug("Exclude by predicate: " + aClass.getQualifiedName());
                    excludedClassesBuilder.add(aClass);
                    continue;
                }
                this.log.debug("Include: " + aClass.getQualifiedName());
                classesBuilder.add(aClass);

            }

        }

        classesNameTranslation = classesNameTranslationBuilder.build();
        selectedClasses = classesBuilder.build();
        excludedClasses = excludedClassesBuilder.build();

        classesByFqn = Maps.uniqueIndex(classesNameTranslation.keySet(), ObjectModelClassifier::getQualifiedName);
        List classesFqn = new ArrayList<>(classesByFqn.keySet());
        Collections.sort(classesFqn);
        this.classesFqn = ImmutableList.copyOf(classesFqn);

        selectedClassesByFqn = Maps.uniqueIndex(selectedClasses, ObjectModelClassifier::getQualifiedName);
        List beanClassesFqn = new ArrayList<>(selectedClassesByFqn.keySet());
        Collections.sort(beanClassesFqn);
        this.selectedClassesFqn = ImmutableList.copyOf(beanClassesFqn);
    }

    public void report() {
        log.info(String.format("Found %d classes with « Bean » stereotype.", classesNameTranslation.size()));
        if (!excludedClasses.isEmpty()) {
            log.info(String.format("Skip excluded %d classes.", excludedClasses.size()));
        }
        log.info(String.format("Will apply on %d classes.", selectedClasses.size()));
    }

    public String getAttributeType(ObjectModelAttribute attr) {
        String attrType = attr.getType();
        return getAttributeType(attrType);
    }

    public String getAttributeType(String attrType) {
        if (!JavaGeneratorUtil.isPrimitiveType(attrType)) {
            boolean hasClass = model.hasClass(attrType);
            if (hasClass) {
                ObjectModelClass attributeClass = model.getClass(attrType);
                String attributeType = classesNameTranslation.get(attributeClass);
                if (attributeType != null) {
                    attrType = attributeClass.getPackageName() + "." + attributeType;
                }
            }
        }
        return attrType;
    }

    public String getAttributeTypeWithGeneric(ObjectModelAttribute attr) {
        String attrType = getAttributeType(attr);
        String generic = eugeneTagValues.getAttributeGenericTagValue(attr);
        if (generic != null) {
            attrType += "<" + getAttributeType(generic) + ">";
        }
        return attrType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy