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

fr.ird.observe.binder.EntityBinderSupport Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package fr.ird.observe.binder;

/*-
 * #%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.dto.IdDto;
import fr.ird.observe.dto.IdHelper;
import org.nuiton.topia.persistence.TopiaEntity;

/**
 * Created by tchemit on 30/08/17.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class EntityBinderSupport {

    private final Class entityType;
    private final Class entityImplType;
    private final Class dtoType;
    private final String key;
    protected BinderProvider binderFactory;

    EntityBinderSupport(Class dtoType, Class entityType) {
        this.entityType = entityType;
        this.dtoType = dtoType;
        this.key = IdHelper.cleanId(dtoType);
        try {
            //noinspection unchecked
            this.entityImplType = (Class) Class.forName(entityType.getName() + "Impl");
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }

    public final Class getEntityType() {
        return entityType;
    }

    public final Class getEntityImplType() {
        return entityImplType;
    }

    public final Class getDtoType() {
        return dtoType;
    }

    public final String getKey() {
        return key;
    }

    protected final void setBinderFactory(BinderProvider binderFactory) {
        this.binderFactory = binderFactory;
    }

    protected final E newEntity() {
        try {
            return entityImplType.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("What ever");
        }
    }

    protected final D newDto() {
        try {
            return dtoType.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("What ever");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy