fr.ird.observe.spi.persistence.ReferentialEntityDefinition Maven / Gradle / Ivy
package fr.ird.observe.spi.persistence;
/*-
* #%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 com.google.common.collect.ImmutableSet;
import fr.ird.observe.binder.referential.ReferentialDtoReferenceBinder;
import fr.ird.observe.binder.referential.ReferentialEntityDtoBinderSupport;
import fr.ird.observe.binder.referential.ReferentialEntityReferenceBinderSupport;
import fr.ird.observe.dto.DataNotFoundException;
import fr.ird.observe.dto.form.Form;
import fr.ird.observe.dto.reference.DtoReferenceDefinition;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceSet;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.dto.referential.ReferentialLocale;
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
import fr.ird.observe.spi.dto.DtoModuleHelper;
import fr.ird.observe.spi.dto.ReferentialDtoDefinition;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.topia.persistence.TopiaDao;
import org.nuiton.topia.persistence.TopiaNoResultException;
import org.nuiton.topia.persistence.TopiaPersistenceContext;
import java.util.Collection;
import java.util.Date;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Created by tchemit on 03/09/17.
*
* @author Tony Chemit - [email protected]
*/
@SuppressWarnings("unchecked")
public abstract class ReferentialEntityDefinition, E extends ObserveReferentialEntity, T extends TopiaDao> {
private static final Log log = LogFactory.getLog(ReferentialEntityDefinition.class);
private final ReferentialDtoDefinition dtoContext;
private final Class entityType;
private final ReferentialEntityDtoBinderSupport entityBinder;
private final ReferentialEntityReferenceBinderSupport entityReferenceBinder;
private final Function daoFunction;
public ReferentialEntityDefinition(ReferentialEntityDtoBinderSupport entityBinder, ReferentialEntityReferenceBinderSupport entityReferenceBinder, Function daoFunction) {
this.dtoContext = DtoModuleHelper.fromReferentialDto(entityBinder.getDtoType());
this.daoFunction = daoFunction;
this.entityType = entityBinder.getEntityType();
this.entityBinder = entityBinder;
this.entityReferenceBinder = entityReferenceBinder;
}
public Class toEntityType() {
return entityType;
}
public ReferentialEntityDtoBinderSupport toEntityBinder() {
return entityBinder;
}
public ReferentialEntityReferenceBinderSupport toEntityReferenceBinder() {
return entityReferenceBinder;
}
public D toDto(ReferentialLocale referentialLocale, E entity) {
D dto = entityBinder.newDto();
entityBinder.copyToDto(referentialLocale, entity, dto);
return dto;
}
public void copyToEntity(ReferentialLocale referentialLocale, D dto, E entity) {
entityBinder.copyToEntity(referentialLocale, dto, entity);
}
public E toEntity(ReferentialLocale referentialLocale, D dto) {
E entity = entityBinder.newEntity();
entityBinder.copyToEntity(referentialLocale, dto, entity);
return entity;
}
public Class toDtoType() {
return dtoContext.toDtoType();
}
public Class toReferenceType() {
return dtoContext.toReferenceType();
}
public ReferentialDtoReferenceBinder toReferenceBinder() {
return dtoContext.toReferenceBinder();
}
public DtoReferenceDefinition toReferenceDefinition() {
return dtoContext.toReferenceDefinition();
}
public R toReference(ReferentialLocale referentialLocale, D dto) {
return dtoContext.toReference(referentialLocale, dto);
}
public T getDao(TopiaPersistenceContext persistenceContext) {
return daoFunction.apply(persistenceContext);
}
public boolean existsEntity(TopiaPersistenceContext persistenceContext, String id) {
return getDao(persistenceContext).forTopiaIdEquals(id).exists();
}
public E loadEntity(TopiaPersistenceContext persistenceContext, String id) {
log.debug("Load entity with id: " + id);
try {
TopiaDao dao = getDao(persistenceContext);
return dao.forTopiaIdEquals(id).findUnique();
} catch (TopiaNoResultException e) {
throw new DataNotFoundException(toDtoType(), id);
}
}
public R loadEntityToReference(TopiaPersistenceContext persistenceContext, ReferentialLocale referentialLocale, String id) {
E entity = loadEntity(persistenceContext, id);
ReferentialEntityReferenceBinderSupport referentialBinderFromDto = toEntityReferenceBinder();
return referentialBinderFromDto.toReference(referentialLocale, entity);
}
public Form referentialEntityToForm(ReferentialLocale referentialLocale, E entity) {
D dto = toDto(referentialLocale, entity);
return Form.newFormDto(toDtoType(), dto);
}
public E loadOrCreateEntityFromReferentialDto(TopiaPersistenceContext persistenceContext, D dto) {
E entity;
if (dto.isPersisted()) {
entity = loadEntity(persistenceContext, dto.getId());
} else {
ReferentialEntityDtoBinderSupport binder = toEntityBinder();
entity = binder.newEntity();
}
return entity;
}
public void deleteEntity(TopiaPersistenceContext persistenceContext, Iterable ids, Consumer> extraCallback) {
T dao = getDao(persistenceContext);
for (String id : ids) {
E entity = dao.forTopiaIdEquals(id).findUniqueOrNull();
if (entity == null) {
throw new DataNotFoundException(toDtoType(), id);
}
dao.delete(entity);
}
if (extraCallback != null) {
extraCallback.accept(toEntityType());
}
}
public D loadEntityToReferentialDto(TopiaPersistenceContext persistenceContext, ReferentialLocale referentialLocale, String id) {
E entity = loadEntity(persistenceContext, id);
ReferentialEntityDtoBinderSupport binder = toEntityBinder();
return binder.toDto(referentialLocale, entity);
}
public ReferentialDtoReferenceSet toReferentialReferenceSet(ReferentialLocale referentialLocale, Collection entities, Date now) {
ReferentialEntityReferenceBinderSupport binder = toEntityReferenceBinder();
ImmutableSet.Builder references = ImmutableSet.builder();
for (E entity : entities) {
R reference = binder.toReference(referentialLocale, entity);
references.add(reference);
}
return ReferentialDtoReferenceSet.of(toReferenceType(), references.build(), now);
}
public E newEntity() {
return toEntityBinder().newEntity();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy