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

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

The newest version!
package fr.ird.observe.spi.context;

/*-
 * #%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 fr.ird.observe.binder.data.DataEntityDtoBinderSupport;
import fr.ird.observe.binder.data.DataEntityReferenceBinderSupport;
import fr.ird.observe.dto.constants.ReferentialLocale;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.form.FormDefinition;
import fr.ird.observe.dto.reference.DataDtoReferenceSupport;
import fr.ird.observe.entities.ObserveDataEntity;
import fr.ird.observe.spi.initializer.DtoReferencesInitializerSupport;
import fr.ird.observe.spi.initializer.EntitiesBinderInitializerSupport;

/**
 * Created by tchemit on 03/09/17.
 *
 * @author Tony Chemit - [email protected]
 */
@SuppressWarnings("unchecked")
public class DataDtoEntityContext, E extends ObserveDataEntity> extends DataDtoContext {

    private final Class entityType;
    private DataEntityDtoBinderSupport entityBinder;
    private DataEntityReferenceBinderSupport entityReferenceBinder;

    public DataDtoEntityContext(Class dtoType, DtoReferencesInitializerSupport modelHelper, FormDefinition formDefinition, EntitiesBinderInitializerSupport initializer) {
        super(dtoType, modelHelper, formDefinition);

        entityType = initializer.getDtoToEntityClassMapping().forData(dtoType);
        entityBinder = initializer.getEntityDtoDataBinders().get(dtoType);
        entityReferenceBinder = initializer.getEntityReferenceDataBinders().get(dtoType);
    }

    public  Class toEntityType() {
        return (Class) entityType;
    }

    public  DataEntityDtoBinderSupport toEntityBinder() {
        return (DataEntityDtoBinderSupport) entityBinder;
    }

    public  D toDto(ReferentialLocale referentialLocale, Class dtoType, EE entity) {
        D dto = entityBinder.newDto();
        entityBinder.copyToDto(referentialLocale, (E) entity, dto);
        return dto;
    }

    public  void copyToEntity(ReferentialLocale referentialLocale, D dto, EE entity) {
        entityBinder.copyToEntity(referentialLocale, dto, (E) entity);
    }

    public , EE extends ObserveDataEntity> DataEntityReferenceBinderSupport toEntityReferenceBinder() {
        return (DataEntityReferenceBinderSupport) entityReferenceBinder;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy