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

fr.ird.observe.toolkit.maven.plugin.server.definition.EntityDefinitionTemplate Maven / Gradle / Ivy

package fr.ird.observe.toolkit.maven.plugin.server.definition;

/*-
 * #%L
 * ObServe Toolkit :: Maven plugin
 * %%
 * Copyright (C) 2017 - 2022 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.gson.Gson;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.data.EditableDto;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.entities.Entity;
import fr.ird.observe.entities.data.DataEntity;
import fr.ird.observe.entities.referential.ReferentialEntity;
import fr.ird.observe.spi.PersistenceBusinessProject;
import fr.ird.observe.spi.context.DataDtoEntityContext;
import fr.ird.observe.spi.context.DtoEntityContext;
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
import fr.ird.observe.spi.doc.EntityToDescription;
import fr.ird.observe.spi.module.BusinessDataPackage;
import fr.ird.observe.spi.module.BusinessModule;
import fr.ird.observe.spi.module.BusinessProject;
import fr.ird.observe.spi.module.BusinessProjectVisitor;
import fr.ird.observe.spi.module.BusinessReferentialPackage;
import fr.ird.observe.spi.module.BusinessSubModule;
import org.apache.maven.plugin.logging.Log;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.Map;

/**
 * Created on 22/11/2021.
 *
 * @author Tony Chemit - [email protected]
 * @since 5.0.55
 */
public class EntityDefinitionTemplate {

    private final Log log;
    private final Path targetDirectory;
    private final Gson gson;
    private final Date now;
    boolean verbose;
    int count = 0;

    public EntityDefinitionTemplate(Log log, Path targetDirectory, Gson gson) {
        this.log = log;
        this.targetDirectory = targetDirectory;
        this.gson = gson;
        this.now = new Date();
    }

    public void generate(BusinessProject businessProject) throws IOException {
        count = 0;
        BusinessProjectVisitor visitor = new BusinessProjectVisitor() {

            @Override
            public void enterSubModuleDataType(BusinessModule module, BusinessSubModule subModule, BusinessDataPackage dataPackage, Class dtoType) {
                Class mainDtoType = businessProject.getMapping().getMainDtoType(dtoType);
                if (mainDtoType != dtoType && !(EditableDto.class.isAssignableFrom(dtoType))) {
                    return;
                }
                if (dtoType.getSimpleName().equals("ProgramDto")) {
                    //FIXME
                    return;
                }
                Path path = BusinessProject.getDtoVariablePath(targetDirectory, module, subModule, dtoType);
                DataDtoEntityContext spi = PersistenceBusinessProject.fromDataDto(dtoType);
                try {
                    generateDefinition(path, spi);
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }

            @Override
            public void enterSubModuleReferentialType(BusinessModule module, BusinessSubModule subModule, BusinessReferentialPackage referentialPackage, Class dtoType) {
                Path path;
                if (subModule.getName().equals(module.getName())) {
                    path = BusinessProject.getDtoVariablePath(targetDirectory, module, dtoType);
                } else {
                    path = BusinessProject.getDtoVariablePath(targetDirectory, module, subModule, dtoType);
                }
                ReferentialDtoEntityContext spi = PersistenceBusinessProject.fromReferentialDto(dtoType);
                try {
                    generateDefinition(path, spi);
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
        businessProject.accept(visitor);
        log.info(String.format("Generate %d definition(s).", count));
    }

    protected void generateDefinition(Path path, DtoEntityContext spi) throws IOException {
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }
        if (verbose) {
            log.info("generate definition at: " + path);
        }
        count++;
        Entity newEntity = spi.newEntity(now);
        Map description = EntityToDescription.generate(newEntity);
        description.remove(Entity.PROPERTY_LAST_UPDATE_DATE);
        Path descriptionPath = path.resolve(DtoEntityContext.DEFINITION_CLASSIFIER);
        String json = gson.toJson(description);
        Files.write(descriptionPath, json.getBytes(StandardCharsets.UTF_8));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy