
fr.ird.observe.spi.PersistenceModelHelper Maven / Gradle / Ivy
package fr.ird.observe.spi;
/*-
* #%L
* ObServe Toolkit :: Common Persistence
* %%
* 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.data.DataDto;
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.ObserveDataEntity;
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
import fr.ird.observe.spi.context.DataDtoEntityContext;
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
import fr.ird.observe.spi.map.ImmutableDtoMap;
import fr.ird.observe.spi.map.ImmutableEntityMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.topia.persistence.TopiaDao;
import org.nuiton.topia.persistence.TopiaEntityEnum;
/**
* Created by tchemit on 29/08/17.
*
* @author Tony Chemit - [email protected]
*/
@SuppressWarnings("unchecked")
public class PersistenceModelHelper {
/** Logger. */
private static final Log log = LogFactory.getLog(DtoModelHelper.class);
private static final PersistenceModelHelper INSTANCE = new PersistenceModelHelper();
private final ImmutableDtoMap referentialDtoContext;
private final ImmutableEntityMap referentialEntityContext;
private final ImmutableDtoMap dataDtoContext;
private final ImmutableEntityMap dataEntityContext;
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialDto(D dtoType) {
return fromReferentialDto((Class) dtoType.getClass());
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialDto(Class dtoType) {
return INSTANCE.referentialDtoContext.get(dtoType);
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialReference(R reference) {
return INSTANCE.referentialDtoContext.get(reference.getClass());
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialReference(Class referenceType) {
return INSTANCE.referentialDtoContext.get(referenceType);
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialEntity(TopiaEntityEnum entityType) {
return INSTANCE.referentialEntityContext.get(entityType.getContract());
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialEntity(E entity) {
//tricks to get entity name (if using bytebuddy then no package, but we can still obtain entity type by the toString method)
return INSTANCE.referentialEntityContext.get(entity.getClass().getPackage() == null ? entity.toString().replaceFirst("@.+", "") : entity.getClass().getName());
}
public static , E extends ObserveReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext fromReferentialEntity(Class entityType) {
return INSTANCE.referentialEntityContext.get(entityType);
}
public static , E extends ObserveDataEntity, T extends TopiaDao> DataDtoEntityContext fromDataDto(D dto) {
return fromDataDto((Class) dto.getClass());
}
public static , E extends ObserveDataEntity, T extends TopiaDao> DataDtoEntityContext fromDataDto(Class dtoType) {
return INSTANCE.dataDtoContext.get(dtoType);
}
public static , E extends ObserveDataEntity, T extends TopiaDao> DataDtoEntityContext fromDataReference(R reference) {
return INSTANCE.dataDtoContext.get(reference.getClass());
}
static , E extends ObserveDataEntity, T extends TopiaDao> DataDtoEntityContext fromDataReference(Class referenceType) {
return INSTANCE.dataDtoContext.get(referenceType);
}
public static , E extends ObserveDataEntity, T extends TopiaDao> DataDtoEntityContext fromDataEntity(E entity) {
//tricks to get entity name (if using bytebuddy then no package, but we can still obtain entity type by the toString method)
return INSTANCE.dataEntityContext.get(entity.getClass().getPackage() == null ? entity.toString().replaceFirst("@.+", "") : entity.getClass().getName());
}
private PersistenceModelHelper() {
log.info("Persistence model helper initialization (" + this + ").");
PersistenceModelHelperInitializerSupport initializer = PersistenceModelHelperInitializerSupport.get();
log.info("Load " + initializer.getReferentialEntityContexts().size() + " entity to dto referential contexts.");
log.info("Load " + initializer.getDataEntityContexts().size() + " entity to dto data contexts.");
log.info("Load " + initializer.getReferentialDtoContexts().size() + " dto to entity referential contexts.");
log.info("Load " + initializer.getDataDtoContexts().size() + " dto to entity data contexts.");
this.referentialDtoContext = initializer.getReferentialDtoContexts();
this.dataDtoContext = initializer.getDataDtoContexts();
this.referentialEntityContext = initializer.getReferentialEntityContexts();
this.dataEntityContext = initializer.getDataEntityContexts();
log.info("Persistence model helper is initialized (" + this + ").");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy