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-db Show documentation
Show all versions of common-db Show documentation
ObServe Toolkit Common Db module
The newest version!
package fr.ird.observe.binder;
/*-
* #%L
* ObServe Toolkit :: Common Db
* %%
* 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.spi.DbModelHelper;
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.constants.ReferentialLocale;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.reference.DataDtoReferenceSupport;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceSupport;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.entities.ObserveDataEntity;
import fr.ird.observe.entities.ObserveEntity;
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import org.apache.commons.collections4.CollectionUtils;
/**
* 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 REFERENCE → ENTITY ---------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
// -- REFERENTIAL → ENTITY -------------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
protected final , EE extends ObserveReferentialEntity> LinkedHashSet toEntitySet(Collection references) {
LinkedHashSet entityList = null;
if (CollectionUtils.isNotEmpty(references)) {
entityList = new LinkedHashSet<>(references.size());
RR firstReference = Iterables.get(references, 0, null);
Objects.requireNonNull(firstReference);
Class dtoType = firstReference.getDtoType();
ReferentialEntityReferenceBinderSupport binder = DbModelHelper.fromReferentialDto(dtoType).toEntityReferenceBinder();
for (RR reference : references) {
EE entity = binder.toEntity(reference);
entityList.add(entity);
}
}
return entityList;
}
// -------------------------------------------------------------------------------------------------------------- //
// -- ENTITY → REFERENTIAL REFERENCE ---------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------------------------------------- //
protected final > List toReferentialReferenceList(ReferentialLocale referentialLocale, Collection entities) {
List references = null;
if (CollectionUtils.isNotEmpty(entities)) {
references = new ArrayList<>(entities.size());
EE first = entities.iterator().next();
ReferentialEntityReferenceBinderSupport binder = DbModelHelper.fromReferentialEntity(first).toEntityReferenceBinder();
for (EE entity : entities) {
RR reference = binder.toReference(referentialLocale, entity);
references.add(reference);
}
}
return references;
}
// -------------------------------------------------------------------------------------------------------------- //
// -- CREATE ENTITY OU 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(RR reference) {
if (reference == null) {
return null;
}
ReferentialEntityReferenceBinderSupport binder = DbModelHelper.fromReferentialReference(reference).toEntityReferenceBinder();
return binder.toEntity(reference);
}
protected final , EE extends ObserveDataEntity> EE toDataEntity(RR reference) {
DataEntityReferenceBinderSupport binder = DbModelHelper.fromDataReference(reference).toEntityReferenceBinder();
return binder.toEntity(reference);
}
protected final > RR toReferentialReference(ReferentialLocale referentialLocale, EE entity) {
RR reference = null;
if (entity != null) {
ReferentialEntityReferenceBinderSupport binder = DbModelHelper.fromReferentialEntity(entity).toEntityReferenceBinder();
reference = binder.toReference(referentialLocale, entity);
}
return reference;
}
protected final > RR toDataReference(ReferentialLocale referentialLocale, EE entity) {
RR reference = null;
if (entity != null) {
DataEntityReferenceBinderSupport binder = DbModelHelper.fromDataEntity(entity).toEntityReferenceBinder();
reference = binder.toReference(referentialLocale, entity);
}
return reference;
}
}