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

fr.ird.observe.spi.context.ReferentialDtoEntityContext 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.referential.ReferentialEntityDtoBinderSupport;
import fr.ird.observe.binder.referential.ReferentialEntityReferenceBinderSupport;
import fr.ird.observe.dto.constants.ReferentialLocale;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceSupport;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
import fr.ird.observe.spi.initializer.DtoFormsInitializerSupport;
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 ReferentialDtoEntityContext, E extends ObserveReferentialEntity> extends ReferentialDtoContext {

    private final Class entityType;
    private ReferentialEntityDtoBinderSupport entityBinder;
    private ReferentialEntityReferenceBinderSupport entityReferenceBinder;

    public ReferentialDtoEntityContext(Class dtoType, DtoReferencesInitializerSupport modelHelper, DtoFormsInitializerSupport formDefinitionsInitializer, EntitiesBinderInitializerSupport initializer) {
        super(dtoType, modelHelper, formDefinitionsInitializer);

        entityType = initializer.getDtoToEntityClassMapping().forReferential(dtoType);
        entityBinder = initializer.getEntityDtoReferentialBinders().get(dtoType);
        entityReferenceBinder = initializer.getEntityReferenceReferentialBinders().get(dtoType);
    }

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

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

    public , EE extends ObserveReferentialEntity> ReferentialEntityReferenceBinderSupport toEntityReferenceBinder() {
        return (ReferentialEntityReferenceBinderSupport) entityReferenceBinder;
    }

    public  D toDto(ReferentialLocale referentialLocale, 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 toEntity(ReferentialLocale referentialLocale, D dto) {
        E entity = entityBinder.newEntity();
        entityBinder.copyToEntity(referentialLocale, dto, entity);
        return (EE) entity;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy