fr.ird.observe.spi.context.ReferentialDtoEntityContext 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 com.google.common.collect.ImmutableSet;
import fr.ird.observe.dto.form.Form;
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.referential.ReferentialEntity;
import io.ultreia.java4all.lang.Objects2;
import org.nuiton.topia.persistence.TopiaDao;
import org.nuiton.topia.persistence.TopiaPersistenceContext;
import java.util.Collection;
import java.util.Date;
import java.util.function.Function;
/**
* Created by tchemit on 04/09/17.
*
* @author Tony Chemit - [email protected]
*/
public class ReferentialDtoEntityContext, E extends ReferentialEntity, T extends TopiaDao> implements DtoEntityContext {
private final Function daoFunction;
private final Class dtoType;
private final Class referenceType;
private final Class entityType;
private final Class extends E> entityTypeImpl;
public static , E extends ReferentialEntity, T extends TopiaDao> ReferentialDtoEntityContext of(Class dtoType, Class referenceType, Class entityType, Class extends E> entityTypeImpl, Function daoFunction) {
return new ReferentialDtoEntityContext<>(dtoType, referenceType, entityType, entityTypeImpl, daoFunction);
}
private ReferentialDtoEntityContext(Class dtoType, Class referenceType, Class entityType, Class extends E> entityTypeImpl, Function daoFunction) {
this.dtoType = dtoType;
this.referenceType = referenceType;
this.entityType = entityType;
this.entityTypeImpl = entityTypeImpl;
this.daoFunction = daoFunction;
}
@Override
public T getDao(TopiaPersistenceContext persistenceContext) {
return daoFunction.apply(persistenceContext);
}
@Override
public R loadEntityToReferenceDto(TopiaPersistenceContext persistenceContext, String id, ReferentialLocale referentialLocale) {
E entity = loadEntity(persistenceContext, id);
return entity.toReference(referentialLocale);
}
@Override
public Form entityToForm(E entity, ReferentialLocale referentialLocale) {
D dto = entity.toDto(referentialLocale);
return Form.newFormDto(toDtoType(), dto);
}
@Override
public D loadEntityToDto(TopiaPersistenceContext persistenceContext, String id, ReferentialLocale referentialLocale) {
E entity = loadEntity(persistenceContext, id);
D dto = newDto();
entity.toDto(referentialLocale, dto);
return dto;
}
@Override
public ReferentialDtoReferenceSet toReferenceSet(Collection entities, ReferentialLocale referentialLocale, Date now) {
ImmutableSet.Builder references = ImmutableSet.builder();
for (E entity : entities) {
R reference = entity.toReference(referentialLocale);
references.add(reference);
}
return ReferentialDtoReferenceSet.of(toReferenceType(), references.build(), now);
}
@Override
public Class toEntityType() {
return entityType;
}
@Override
public Class toDtoType() {
return dtoType;
}
@Override
public Class toReferenceType() {
return referenceType;
}
@Override
public E newEntity(Date now) {
E result = Objects2.newInstance(entityTypeImpl);
result.setTopiaCreateDate(now);
return result;
}
@Override
public D toDto(ReferentialLocale referentialLocale, Class dtoType, E entity) {
D dto = newDto();
entity.toDto(referentialLocale, dto);
return dto;
}
@Override
public E toEntity(ReferentialLocale referentialLocale, D dto) {
E entity = newEntity(dto.getCreateDate());
entity.fromDto(referentialLocale, dto);
return entity;
}
@Override
public R toReference(ReferentialLocale referentialLocale, E entity) {
return entity.toReference(referentialLocale);
}
public ImmutableSet toDto(ReferentialLocale referentialLocale, Class dtoType, Collection entities) {
ImmutableSet.Builder result = ImmutableSet.builder();
for (E entity : entities) {
D dto = toDto(referentialLocale, dtoType, entity);
result.add(dto);
}
return result.build();
}
}