fr.ird.observe.binder.EntityDtoBinderSupport 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.binder;
/*-
* #%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.Iterables;
import fr.ird.observe.binder.data.DataEntityReferenceBinderSupport;
import fr.ird.observe.binder.referential.ReferentialEntityReferenceBinderSupport;
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.ReferentialDtoReference;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.dto.referential.ReferentialLocale;
import fr.ird.observe.entities.ObserveDataEntity;
import fr.ird.observe.entities.ObserveEntity;
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
import fr.ird.observe.spi.persistence.DataEntityDefinition;
import fr.ird.observe.spi.persistence.PersistenceModuleHelper;
import fr.ird.observe.spi.persistence.ReferentialEntityDefinition;
import org.apache.commons.collections4.CollectionUtils;
import org.nuiton.topia.persistence.TopiaDao;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
/**
* Created on 24/11/15.
*
* @author Tony Chemit - [email protected]
*/
public abstract class EntityDtoBinderSupport extends EntityBinderSupport {
protected EntityDtoBinderSupport(Class dtoType, Class entityType) {
super(dtoType, entityType);
}
public abstract void copyToEntity(ReferentialLocale referentialLocale, D dto, E entity);
public abstract void copyToDto(ReferentialLocale referentialLocale, E entity, D dto);
// -------------------------------------------------------------------------------------------------------------- //
// -- REFERENTIAL → ENTITY -------------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
protected final , EE extends ObserveReferentialEntity> LinkedHashSet toEntitySet(@Nullable Collection references) {
if (CollectionUtils.isEmpty(references)) {
return null;
}
LinkedHashSet entityList = new LinkedHashSet<>(references.size());
RR firstReference = Iterables.get(references, 0, null);
Objects.requireNonNull(firstReference);
ReferentialEntityDefinition> entityContext = PersistenceModuleHelper.fromReferentialReference(firstReference);
ReferentialEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
for (RR reference : references) {
EE entity = binder.newEntity(reference);
entityList.add(entity);
}
return entityList;
}
// -------------------------------------------------------------------------------------------------------------- //
// -- ENTITY → REFERENTIAL REFERENCE ---------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
protected final > List toReferentialReferenceList(ReferentialLocale referentialLocale, @Nullable Collection entities) {
if (CollectionUtils.isEmpty(entities)) {
return null;
}
List references = new ArrayList<>(entities.size());
EE first = entities.iterator().next();
ReferentialEntityDefinition entityContext = PersistenceModuleHelper.fromReferentialEntity(first);
ReferentialEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
for (EE entity : entities) {
RR reference = binder.toReference(referentialLocale, entity);
references.add(reference);
}
return references;
}
// -------------------------------------------------------------------------------------------------------------- //
// -- CREATE ENTITY OR DTO -------------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
public final D toDto(ReferentialLocale referentialLocale, E entity) {
D dto = newDto();
copyToDto(referentialLocale, entity, dto);
return dto;
}
protected final , EE extends ObserveReferentialEntity> EE toEntity(@Nullable RR reference) {
if (reference == null) {
return null;
}
ReferentialEntityDefinition> entityContext = PersistenceModuleHelper.fromReferentialReference(reference);
ReferentialEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
return binder.newEntity(reference);
}
protected final , EE extends ObserveDataEntity> EE toDataEntity(@Nullable RR reference) {
if (reference == null) {
return null;
}
DataEntityDefinition> entityContext = PersistenceModuleHelper.fromDataReference(reference);
DataEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
return binder.newEntity(reference);
}
protected final > RR toReferentialReference(ReferentialLocale referentialLocale, @Nullable EE entity) {
if (entity == null) {
return null;
}
ReferentialEntityDefinition entityContext = PersistenceModuleHelper.fromReferentialEntity(entity);
ReferentialEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
return binder.toReference(referentialLocale, entity);
}
protected final > RR toDataReference(ReferentialLocale referentialLocale, @Nullable EE entity) {
if (entity == null) {
return null;
}
DataEntityDefinition entityContext = PersistenceModuleHelper.fromDataEntity(entity);
DataEntityReferenceBinderSupport binder = entityContext.toEntityReferenceBinder();
return binder.toReference(referentialLocale, entity);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy