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

fr.ird.observe.spi.DbModelHelper Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.spi;

/*-
 * #%L
 * ObServe Toolkit :: Common Persistence
 * %%
 * Copyright (C) 2017 - 2020 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.IdDto;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.DtoReference;
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.context.DataDtoEntityContext;
import fr.ird.observe.spi.context.DtoEntityContext;
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
import fr.ird.observe.spi.mapping.DtoToEntityContextMapping;
import fr.ird.observe.spi.mapping.EntityToDtoClassMapping;
import io.ultreia.java4all.application.context.spi.GenerateApplicationComponent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaEntityEnum;

import java.util.Objects;

/**
 * Created by tchemit on 29/08/17.
 *
 * @author Tony Chemit - [email protected]
 */
@SuppressWarnings("unchecked")
@GenerateApplicationComponent(name = "Persistence Model Helper",
        dependencies = {
                DtoToEntityContextMapping.class,
                EntityToDtoClassMapping.class,
        })
public class DbModelHelper {

    private static final Logger log = LogManager.getLogger(DtoModelHelper.class);
    private static final DbModelHelper INSTANCE = DbModelHelperApplicationComponent.value();

    private final DtoToEntityContextMapping dtoToEntityContextMapping;
    private final EntityToDtoClassMapping entityToDtoClassMapping;

    public DbModelHelper(DtoToEntityContextMapping dtoToEntityContextMapping, EntityToDtoClassMapping entityToDtoClassMapping) {
        log.info("Persistence model helper initialization  (" + this + ").");
        this.dtoToEntityContextMapping = Objects.requireNonNull(dtoToEntityContextMapping);
        this.entityToDtoClassMapping = Objects.requireNonNull(entityToDtoClassMapping);
        log.info("Persistence model helper is initialized (" + this + ").");
    }

    // -----------------------
    // -- fromDto
    // -----------------------

    public static , E extends Entity> DtoEntityContext fromDto(D dtoType) {
        return fromDto((Class) dtoType.getClass());
    }

    public static , E extends Entity> DtoEntityContext fromDto(Class dtoType) {
        return getDtoToEntityContextMapping().getContext(dtoType);
    }

    // -----------------------
    // -- fromReferentialDto
    // -----------------------

    @SuppressWarnings("rawtypes")
    public static , E extends ReferentialEntity> ReferentialDtoEntityContext fromReferentialDto(D dtoType) {
        return referential0((Class) dtoType.getClass());
    }

    @SuppressWarnings("rawtypes")
    public static > ReferentialDtoEntityContext fromReferentialDtoWeak(D dtoType) {
        return referentialWeak0((Class) dtoType.getClass());
    }

    public static , E extends ReferentialEntity> ReferentialDtoEntityContext fromReferentialDto(Class dtoType) {
        return referential0(dtoType);
    }

    public static > ReferentialDtoEntityContext fromReferentialDtoWeak(Class dtoType) {
        return referentialWeak0(dtoType);
    }

    // -----------------------
    // -- fromDataDto
    // -----------------------

    @SuppressWarnings("rawtypes")
    public static , E extends DataEntity> DataDtoEntityContext fromDataDto(D dto) {
        return data0((Class) dto.getClass());
    }

    public static , E extends DataEntity> DataDtoEntityContext fromDataDto(Class dtoType) {
        return data0(dtoType);
    }

    public static > DataDtoEntityContext fromDataDtoWeak(Class dtoType) {
        return dataWeak0(dtoType);
    }

    // -----------------------
    // -- fromEntity
    // -----------------------

    public static , E extends Entity> DtoEntityContext fromEntity(Class entityType) {
        return getDtoToEntityContextMapping().getContext(getDtoType(entityType));
    }

    // -----------------------
    // -- fromReferentialEntity
    // -----------------------

    public static , E extends ReferentialEntity> ReferentialDtoEntityContext fromReferentialEntity(TopiaEntityEnum entityType) {
        return referential0(getDtoType(entityType.getContract()));
    }

    public static , E extends ReferentialEntity> ReferentialDtoEntityContext fromReferentialEntity(E entity) {
        return referential0(getDtoType(getClass(entity)));
    }

    public static , E extends ReferentialEntity> ReferentialDtoEntityContext fromReferentialEntity(Class entityType) {
        return referential0(getDtoType(entityType));
    }

    // -----------------------
    // -- fromDataEntity
    // -----------------------

    public static , E extends DataEntity> DataDtoEntityContext fromDataEntity(Class entityType) {
        return data0(getDtoType(entityType));
    }

    @SuppressWarnings("rawtypes")
    private static , E extends DataEntity> DataDtoEntityContext data0(Class dtoType) {
        return (DataDtoEntityContext) getDtoToEntityContextMapping().getContext(dtoType);
    }

    protected static DtoToEntityContextMapping getDtoToEntityContextMapping() {
        return INSTANCE.dtoToEntityContextMapping;
    }

    protected static EntityToDtoClassMapping getEntityToDtoClassMapping() {
        return INSTANCE.entityToDtoClassMapping;
    }

    @SuppressWarnings("rawtypes")
    private static > DataDtoEntityContext dataWeak0(Class dtoType) {
        return (DataDtoEntityContext) getDtoToEntityContextMapping().getContext(dtoType);
    }

    @SuppressWarnings("rawtypes")
    private static , E extends ReferentialEntity> ReferentialDtoEntityContext referential0(Class dtoType) {
        return (ReferentialDtoEntityContext) getDtoToEntityContextMapping().getContext(dtoType);
    }

    @SuppressWarnings("rawtypes")
    private static > ReferentialDtoEntityContext referentialWeak0(Class dtoType) {
        return (ReferentialDtoEntityContext) getDtoToEntityContextMapping().getContext(dtoType);
    }

    private static  Class getDtoType(Class entityType) {
        return getEntityToDtoClassMapping().get(entityType);
    }

    private static  Class getClass(Object entity) {
        if (entity.getClass().getName().contains("net.bytebuddy")) {
            return (Class) entity.getClass().getInterfaces()[0];
        }
        if (entity.getClass().getName().contains("HibernateProxy")) {
            return (Class) entity.getClass().getInterfaces()[0];
        }
        return (Class) entity.getClass();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy