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

fr.ird.observe.binder.data.DataEntityDtoBinderSupport Maven / Gradle / Ivy

The newest version!
package fr.ird.observe.binder.data;

/*-
 * #%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.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import fr.ird.observe.spi.DbModelHelper;
import fr.ird.observe.binder.EntityDtoBinderSupport;
import fr.ird.observe.dto.WithComment;
import fr.ird.observe.dto.constants.ReferentialLocale;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.entities.CommentableEntity;
import fr.ird.observe.entities.ObserveDataEntity;
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.Set;
import javax.sql.rowset.serial.SerialBlob;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.topia.persistence.TopiaEntities;

/**
 * Created on 24/11/15.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class DataEntityDtoBinderSupport extends EntityDtoBinderSupport {

    private static final Log log = LogFactory.getLog(DataEntityDtoBinderSupport.class);

    protected DataEntityDtoBinderSupport(Class entityType, Class dtoType) {
        super(dtoType, entityType);
    }

    // -------------------------------------------------------------------------------------------------------------- //
    // -- ENTITY → DATA --------------------------------------------------------------------------------------------- //
    // -------------------------------------------------------------------------------------------------------------- //

    protected void copyEntityDataFieldsToDto(E entity, D dto) {
        dto.setId(entity.getTopiaId());
        dto.setLastUpdateDate(entity.getLastUpdateDate());

        if (dto instanceof WithComment && entity instanceof CommentableEntity) {
            ((WithComment) dto).setComment(((CommentableEntity) entity).getComment());
        }
    }

    protected Blob byteArrayToBlob(byte[] bytes) {
        Blob result = null;

        try {
            result = new SerialBlob(bytes);
        } catch (SQLException e) {
            if (log.isErrorEnabled()) {
                log.error("unable to create blob ", e);
            }
        }

        return result;
    }

    // -------------------------------------------------------------------------------------------------------------- //
    // -- ENTITY → DATA REFERENCE ----------------------------------------------------------------------------------- //
    // -------------------------------------------------------------------------------------------------------------- //

    protected  LinkedHashSet
toLinkedHashSetData(ReferentialLocale referentialLocale, Collection entities) { LinkedHashSet
dtoList = null; if (CollectionUtils.isNotEmpty(entities)) { EE first = entities.iterator().next(); DataEntityDtoBinderSupport binder = DbModelHelper.fromDataEntity(first).toEntityDtoBinder(); dtoList = new LinkedHashSet<>(entities.size()); for (EE entity : entities) { DD dto = binder.toDto(referentialLocale, entity); dtoList.add(dto); } } return dtoList; } protected LinkedHashSet
toLinkedHashSetData(ReferentialLocale referentialLocale, Collection entities, Class
dtoType) { LinkedHashSet
dtoList = null; if (CollectionUtils.isNotEmpty(entities)) { DataEntityDtoBinderSupport binder = DbModelHelper.fromDataDto(dtoType).toEntityBinder(); dtoList = new LinkedHashSet<>(entities.size()); for (EE entity : entities) { DD dto = binder.toDto(referentialLocale, entity); dtoList.add(dto); } } return dtoList; } protected List
toListData(ReferentialLocale referentialLocale, Collection entities) { List
dtoList = null; if (CollectionUtils.isNotEmpty(entities)) { EE first = entities.iterator().next(); DataEntityDtoBinderSupport binder = DbModelHelper.fromDataEntity(first).toEntityDtoBinder(); dtoList = new ArrayList<>(entities.size()); for (EE entity : entities) { DD dto = binder.toDto(referentialLocale, entity); dtoList.add(dto); } } return dtoList; } // -------------------------------------------------------------------------------------------------------------- // // -- DATA → ENTITY --------------------------------------------------------------------------------------------- // // -------------------------------------------------------------------------------------------------------------- // public E toEntity(ReferentialLocale referentialLocale, D data) { E entity = newEntity(); copyToEntity(referentialLocale, data, entity); return entity; } protected void copyDtoDataFieldsToEntity(D dto, E entity) { entity.setTopiaId(dto.getId()); Date lastUpdateDate = dto.getLastUpdateDate(); if (lastUpdateDate == null) { lastUpdateDate = new Date(); } entity.setLastUpdateDate(lastUpdateDate); if (dto instanceof WithComment && entity instanceof CommentableEntity) { ((CommentableEntity) entity).setComment(((WithComment) dto).getComment()); } } protected
> C toEntitySet(ReferentialLocale referentialLocale, Collection
dtoList, C entityList) { if (entityList == null) { //noinspection unchecked entityList = (C) new LinkedHashSet(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } protected
List toEntityList(ReferentialLocale referentialLocale, Collection
dtoList, List entityList) { if (entityList == null) { entityList = new LinkedList<>(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } protected
Collection toEntityCollection(ReferentialLocale referentialLocale, Collection
dtoList, Collection entityList) { if (entityList == null) { entityList = new LinkedHashSet<>(); } fillEntityCollection(referentialLocale, dtoList, entityList); return entityList; } private
> void fillEntityCollection(ReferentialLocale referentialLocale, Collection
dtoList, C entityList) { ImmutableMap entitiesById = Maps.uniqueIndex(entityList, TopiaEntities.getTopiaIdFunction()); entityList.clear(); if (CollectionUtils.isNotEmpty(dtoList)) { DD first = Iterables.get(dtoList, 0, null); DataEntityDtoBinderSupport binder = DbModelHelper.fromDataDto(first).toEntityBinder(); for (DD dto : dtoList) { EE entity = entitiesById.get(dto.getId()); if (entity == null) { // Create new entity entity = binder.toEntity(referentialLocale, dto); entityList.add(entity); } else { // Reuse existing entity binder.copyToEntity(referentialLocale, dto, entity); entityList.add(entity); } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy