fr.ird.observe.spi.context.DtoEntityContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-persistence Show documentation
Show all versions of common-persistence Show documentation
ObServe Toolkit Common Persistence module
package fr.ird.observe.spi.context;
/*-
* #%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.DtoToReference;
import fr.ird.observe.dto.IdDto;
import fr.ird.observe.dto.form.Form;
import fr.ird.observe.dto.reference.DtoReference;
import fr.ird.observe.dto.reference.DtoReferenceCollection;
import fr.ird.observe.dto.referential.ReferentialLocale;
import fr.ird.observe.entities.DataNotFoundException;
import fr.ird.observe.entities.Entity;
import io.ultreia.java4all.lang.Objects2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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;
/**
* Created on 08/02/19.
*
* @author Tony Chemit - [email protected]
* @since ?
*/
public interface DtoEntityContext, E extends Entity, T extends TopiaDao> {
Logger log = LogManager.getLogger(DtoEntityContext.class);
Class toDtoType();
Class toReferenceType();
Class toEntityType();
E newEntity(Date now);
D toDto(ReferentialLocale referentialLocale, Class dtoType, E entity);
E toEntity(ReferentialLocale referentialLocale, D dto);
R toReference(ReferentialLocale referentialLocale, E entity);
T getDao(TopiaPersistenceContext persistenceContext);
R loadEntityToReferenceDto(TopiaPersistenceContext persistenceContext, String id, ReferentialLocale referentialLocale);
D loadEntityToDto(TopiaPersistenceContext persistenceContext, String id, ReferentialLocale referentialLocale);
Form entityToForm(E entity, ReferentialLocale referentialLocale);
DtoReferenceCollection toReferenceSet(Collection entities, ReferentialLocale referentialLocale, Date now);
@SuppressWarnings("unchecked")
default R toReference(ReferentialLocale referentialLocale, D dto) {
return ((DtoToReference) dto).toReference(referentialLocale);
}
default D newDto() {
return Objects2.newInstance(toDtoType());
}
default E loadEntity(TopiaPersistenceContext persistenceContext, String id) {
try {
log.debug(String.format("Load entity with id: %s", id));
TopiaDao dao = getDao(persistenceContext);
return dao.forTopiaIdEquals(id).findUnique();
} catch (TopiaNoResultException e) {
throw new DataNotFoundException(toDtoType(), id);
}
}
default E loadOrCreateEntityFromDto(TopiaPersistenceContext persistenceContext, D dto) {
E entity;
if (dto.isPersisted()) {
entity = loadEntity(persistenceContext, dto.getId());
} else {
entity = newEntity(new Date());
}
return entity;
}
default boolean existsEntity(TopiaPersistenceContext persistenceContext, String id) {
return getDao(persistenceContext).forTopiaIdEquals(id).exists();
}
default E loadNullableEntity(TopiaPersistenceContext persistenceContext, String id) {
if (id == null) {
return null;
}
return loadEntity(persistenceContext, id);
}
default D toDto(ReferentialLocale referentialLocale, E entity) {
return toDto(referentialLocale, toDtoType(), entity);
}
default E toEntity(R reference) {
E entity = newEntity(reference.getCreateDate());
entity.setTopiaId(reference.getId());
return entity;
}
}