All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.entities.EntityHelper Maven / Gradle / Ivy

package fr.ird.observe.entities;

/*-
 * #%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.ImmutableMap;
import com.google.common.collect.Maps;
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.data.DataEntity;
import fr.ird.observe.entities.referential.ReferentialEntity;
import fr.ird.observe.spi.DbModelHelper;
import fr.ird.observe.spi.context.DataDtoEntityContext;
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
import org.apache.commons.collections4.CollectionUtils;
import org.nuiton.topia.persistence.TopiaEntities;

import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Some useful methods on entities (mainly to transform them to/from dto).
 *
 * @author Tony Chemit - [email protected]
 * @since 4
 */
public class EntityHelper {

    public static  void toDto(Entity entity, D dto) {
        dto.setId(entity.getTopiaId());
        dto.setVersion(entity.getTopiaVersion());
        dto.setCreateDate(entity.getTopiaCreateDate());
        dto.setLastUpdateDate(entity.getLastUpdateDate());
    }

    public static  void fromDto(Entity entity, D dto) {
        entity.setTopiaId(dto.getId());
        entity.setTopiaVersion(dto.getVersion());
        entity.setTopiaCreateDate(dto.getCreateDate());
        Date lastUpdateDate = dto.getLastUpdateDate();
        if (lastUpdateDate == null) {
            lastUpdateDate = new Date();
        }
        entity.setLastUpdateDate(lastUpdateDate);
    }

    public static 
, EE extends ReferentialEntity> EE toReferentialEntity(RR reference) { EE entity = null; if (reference != null) { ReferentialDtoEntityContext entityContext = DbModelHelper.fromReferentialDto(reference.getDtoType()); entity = entityContext.toEntity(reference); } return entity; } public static
, EE extends ReferentialEntity> LinkedHashSet toReferentialEntitySet(Collection references) { LinkedHashSet entityList = null; if (CollectionUtils.isNotEmpty(references)) { entityList = new LinkedHashSet<>(references.size()); RR firstReference = Objects.requireNonNull(references.iterator().next()); Class
dtoType = firstReference.getDtoType(); ReferentialDtoEntityContext entityContext = DbModelHelper.fromReferentialDto(dtoType); for (RR reference : references) { EE entity = entityContext.toEntity(reference); entityList.add(entity); } } return entityList; } public static , DD extends ReferentialDto, RR extends ReferentialDtoReference> List toReferentialReferenceList(ReferentialLocale referentialLocale, Collection entities) { List references = null; if (CollectionUtils.isNotEmpty(entities)) { references = new ArrayList<>(entities.size()); for (EE entity : entities) { RR reference = entity.toReference(referentialLocale); references.add(reference); } } return references; } public static Blob byteArrayToBlob(byte[] bytes) { try { return new SerialBlob(bytes); } catch (SQLException e) { throw new RuntimeException("unable to create blob ", e); } } public static void toDataDto(DataEntity entity, D dto) { toDto(entity, dto); dto.setHomeId(entity.getHomeId()); } public static void fromDataDto(DataEntity entity, D dto) { EntityHelper.fromDto(entity, dto); entity.setHomeId(dto.getHomeId()); } protected static
, EE extends DataEntity, C extends Collection> void fillEntityCollection(ReferentialLocale referentialLocale, Collection
dtoList, C entityList) { ImmutableMap entitiesById = Maps.uniqueIndex(entityList, TopiaEntities.getTopiaIdFunction()); entityList.clear(); if (CollectionUtils.isNotEmpty(dtoList)) { DD first = Objects.requireNonNull(dtoList.iterator().next()); DataDtoEntityContext dtoEntityContext = DbModelHelper.fromDataDto(first); for (DD dto : dtoList) { EE entity = entitiesById.get(dto.getId()); if (entity == null) { // Create new entity entity = dtoEntityContext.newEntity(dto.getCreateDate()); } entity.fromDto(referentialLocale, dto); entityList.add(entity); } } } public static
> LinkedHashSet
toDtoLinkedHashSet(ReferentialLocale referentialLocale, Collection entities, Class
dtoType) { LinkedHashSet
dtoList = null; if (entities!=null) { @SuppressWarnings("unchecked") DataDtoEntityContext dtoEntityContext = (DataDtoEntityContext) DbModelHelper.fromDataDtoWeak(dtoType); dtoList = new LinkedHashSet<>(entities.size()); for (EE entity : entities) { DD dto = dtoEntityContext.toDto(referentialLocale, dtoType, entity); dtoList.add(dto); } } return dtoList; } public static
, EE extends DataEntity, C extends Set> C toDataEntitySet(ReferentialLocale referentialLocale, Collection
dtoList, C entityList) { if (entityList == null) { //noinspection unchecked entityList = (C) new LinkedHashSet(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } public static
, EE extends DataEntity> List toDataEntityList(ReferentialLocale referentialLocale, Collection
dtoList, List entityList) { if (entityList == null) { entityList = new LinkedList<>(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } public static
, EE extends DataEntity> Collection toDataEntityCollection(ReferentialLocale referentialLocale, Collection
dtoList, Collection entityList) { if (entityList == null) { entityList = new LinkedHashSet<>(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } public static
, EE extends DataEntity> EE toDataEntity(RR reference) { EE entity = null; if (reference != null) { DataDtoEntityContext context = DbModelHelper.fromDataDto(reference.getDtoType()); entity = context.toEntity(reference); } return entity; } public static
, EE extends DataEntity> List
toDataDtoList(ReferentialLocale referentialLocale, Collection entities) { List
dtoList = null; if (entities != null) { dtoList = new ArrayList<>(entities.size()); for (EE entity : entities) { DD dto = entity.toDto(referentialLocale); dtoList.add(dto); } } return dtoList; } public static void toReferentialDto(ReferentialEntity entity, D dto) { toDto(entity, dto); dto.setStatus(entity.getStatus()); dto.setNeedComment(entity.isNeedComment()); dto.setCode(entity.getCode()); dto.setUri(entity.getUri()); dto.setHomeId(entity.getHomeId()); } public static void fromReferentialDto(ReferentialEntity entity, D dto) { fromDto(entity, dto); entity.setStatus(dto.getStatus()); entity.setNeedComment(dto.isNeedComment()); entity.setCode(dto.getCode()); entity.setUri(dto.getUri()); entity.setHomeId(dto.getHomeId()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy