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

fr.ird.observe.spi.context.DataDtoEntityContext Maven / Gradle / Ivy

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.data.DataDto;
import fr.ird.observe.dto.form.Form;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.DataDtoReferenceSet;
import fr.ird.observe.dto.referential.ReferentialLocale;
import fr.ird.observe.entities.data.DataEntity;
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]
 */
@SuppressWarnings("rawtypes")
public class DataDtoEntityContext, E extends DataEntity, T extends TopiaDao> implements DtoEntityContext {

    private final Class dtoType;
    private final Class entityType;
    private final Class entityTypeImpl;
    private final Class referenceType;
    private final Function daoFunction;

    private DataDtoEntityContext(Class dtoType, Class referenceType, Class entityType, Class entityTypeImpl, Function daoFunction) {
        this.dtoType = dtoType;
        this.referenceType = referenceType;
        this.entityType = entityType;
        this.entityTypeImpl = entityTypeImpl;
        this.daoFunction = daoFunction;
    }

    public static , E extends DataEntity, T extends TopiaDao> DataDtoEntityContext of(Class dtoType, Class referenceType, Class entityType, Class entityImplType, Function daoFunction) {
        return new DataDtoEntityContext<>(dtoType, referenceType, entityType, entityImplType, 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 toReference(referentialLocale, entity);
    }

    @Override
    public Form entityToForm(E entity, ReferentialLocale referentialLocale) {
        D dto = toDto(referentialLocale, toDtoType(), entity);
        return Form.newFormDto(toDtoType(), dto);
    }

    @Override
    public D loadEntityToDto(TopiaPersistenceContext persistenceContext, String id, ReferentialLocale referentialLocale) {
        E entity = loadEntity(persistenceContext, id);
        return toDto(referentialLocale, entity);

    }

    public DataDtoReferenceSet toReferenceSet(Collection entities, ReferentialLocale referentialLocale) {
        ImmutableSet.Builder references = ImmutableSet.builder();
        for (E entity : entities) {
            R reference = toReference(referentialLocale, entity);
            references.add(reference);
        }
        return DataDtoReferenceSet.of(toReferenceType(), references.build());
    }

    @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 DataDtoReferenceSet toReferenceSet(Collection entities, ReferentialLocale referentialLocale, Date now) {
        ImmutableSet.Builder references = ImmutableSet.builder();
        for (E entity : entities) {
            R reference = toReference(referentialLocale, entity);
            references.add(reference);
        }
        return DataDtoReferenceSet.of(toReferenceType(), references.build());
    }

    @Override
    public D toDto(ReferentialLocale referentialLocale, Class dtoType, E entity) {
        D dto = Objects2.newInstance(dtoType);
        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) {
        @SuppressWarnings("unchecked") R reference = (R) entity.toReference(referentialLocale);
        return reference;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy