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

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

The 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 fr.ird.observe.entities.ObserveEntity;

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

    private final Class entityType;
    private final Class entityImplType;
    private final Class dtoType;
    private final String key;

    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);
        }
    }

    @Override
    public final Class getEntityType() {
        return entityType;
    }

    @Override
    public final Class getEntityImplType() {
        return entityImplType;
    }

    @Override
    public final Class getDtoType() {
        return dtoType;
    }

    public final String getKey() {
        return key;
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy