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

com.wizarius.orm.database.EntityInitializer Maven / Gradle / Ivy

The newest version!
package com.wizarius.orm.database;

import com.wizarius.orm.database.actions.AbstractDBDelete;
import com.wizarius.orm.database.actions.AbstractDBInsert;
import com.wizarius.orm.database.actions.AbstractDBSelect;
import com.wizarius.orm.database.actions.AbstractDBUpdate;
import com.wizarius.orm.database.annotations.DBField;
import com.wizarius.orm.database.annotations.DBJoinModel;
import com.wizarius.orm.database.annotations.DBModel;
import com.wizarius.orm.database.data.UniqueTableDictionary;
import com.wizarius.orm.database.exceptions.DBException;
import com.wizarius.orm.database.interfaces.DBEntity;

import java.lang.reflect.Field;

/**
 * Created by UNKNOWN on 12.11.17.
 */
public abstract class EntityInitializer {
    private final UniqueTableDictionary uniqueTableDictionary = new UniqueTableDictionary();
    protected final DBManateeParsedFieldsList fieldsMap;
    protected final DBConnectionPool connectionPool;
    /**
     * Abstract database functions
     */
    @Deprecated
    public AbstractDBSelect select;
    @Deprecated
    public AbstractDBInsert insert;
    @Deprecated
    public AbstractDBUpdate update;
    @Deprecated
    public AbstractDBDelete delete;

    public EntityInitializer(Class clazz, DBConnectionPool pool) throws DBException {
        this.connectionPool = pool;
        this.fieldsMap = parseEntity(clazz);
    }

    /**
     * Parse entity
     *
     * @param clazz entity clazz
     * @return parsed fields list
     * @throws DBException
     */
    private DBManateeParsedFieldsList parseEntity(Class clazz) throws DBException {
        DBEntity dbEntity;
        try {
            dbEntity = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DBException(" Unable to create new instance ", e);
        }
        if (!dbEntity.getClass().isAnnotationPresent(DBModel.class)) {
            throw new DBException(" DBModel annotation didn't found");
        }
        String tableName = dbEntity.getClass().getAnnotation(DBModel.class).tableName();

        DBManateeParsedFieldsList fieldsList = new DBManateeParsedFieldsList(
                tableName,
                clazz,
                uniqueTableDictionary.getNext()
        );
        for (Field field : dbEntity.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(DBField.class)) {
                String fieldName = field.getName();
                DBField dbFieldAnnotation = field.getAnnotation(DBField.class);
                String dbFieldName = dbFieldAnnotation.fieldName();
                boolean isAutoIncrement = dbFieldAnnotation.isAutoIncrement();
                fieldsList.add(new DBManateeParsedField(
                        tableName,
                        fieldName,
                        dbFieldName,
                        field.getType(),
                        field,
                        javaTypeToManateeType(field),
                        isAutoIncrement)
                );
            }

            //if join model
            if (field.isAnnotationPresent(DBJoinModel.class)) {
                String fieldName = field.getName();
                String parentField = field.getAnnotation(DBJoinModel.class).currentDBFieldName();
                String childField = field.getAnnotation(DBJoinModel.class).insideDBFieldName();
                //load array
                if (field.getType().isArray()) {
                    Class arrayField = field.getType().getComponentType();
                    DBManateeParsedFieldsList dbManateeParsedFieldsList = parseEntity((Class) arrayField);
                    DBManateeParsedField parsedField = new DBManateeParsedField(
                            tableName,
                            fieldName,
                            null,
                            field.getType(),
                            field,
                            javaTypeToManateeType(field),
                            false);
                    fieldsList.add(
                            parsedField
                    );
                    parsedField.setJoinField(new DBManateeJoinField(parentField, childField, true, dbManateeParsedFieldsList));
                } else {
                    Class fieldClass = field.getType();
                    DBManateeParsedFieldsList dbManateeParsedFieldsList = parseEntity((Class) fieldClass);
                    DBManateeParsedField parsedField = new DBManateeParsedField(
                            tableName,
                            fieldName,
                            "",
                            field.getType(),
                            field,
                            ManateeDBSupportedTypes.JOIN_OBJECT
                            , false);
                    fieldsList.add(
                            parsedField
                    );
                    parsedField.setJoinField(new DBManateeJoinField(parentField, childField, false, dbManateeParsedFieldsList));
                }
            }

        }
        return fieldsList;
    }

    /**
     * Get manatee type by java type
     *
     * @param field java field refraction
     * @return manatee db type
     */
    private ManateeDBSupportedTypes javaTypeToManateeType(Field field) {
        if (field.getType().isEnum()) {
            return ManateeDBSupportedTypes.ENUM;
        } else if (field.getAnnotation(DBField.class).isCustomType()) {
            return ManateeDBSupportedTypes.CUSTOM_TYPE;
        }
        switch (field.getType().getSimpleName()) {
            case "int": {
                return ManateeDBSupportedTypes.INTEGER;
            }
            case "BigDecimal": {
                return ManateeDBSupportedTypes.BIGDECIMAL;
            }
            case "int[]": {
                return ManateeDBSupportedTypes.INTEGER_ARRAY;
            }
            case "String": {
                return ManateeDBSupportedTypes.STRING;
            }
            case "String[]": {
                return ManateeDBSupportedTypes.STRING_ARRAY;
            }
            case "byte": {
                return ManateeDBSupportedTypes.BYTE;
            }
            case "byte[]": {
                return ManateeDBSupportedTypes.BYTE_ARRAY;
            }
            case "float": {
                return ManateeDBSupportedTypes.FLOAT;
            }
            case "float[]": {
                return ManateeDBSupportedTypes.FLOAT_ARRAY;
            }
            case "long": {
                return ManateeDBSupportedTypes.LONG;
            }
            case "long[]": {
                return ManateeDBSupportedTypes.LONG_ARRAY;
            }
            case "char": {
                return ManateeDBSupportedTypes.CHAR;
            }
            case "char[]": {
                return ManateeDBSupportedTypes.CHAR_ARRAY;
            }
            case "double": {
                return ManateeDBSupportedTypes.DOUBLE;
            }
            case "double[]": {
                return ManateeDBSupportedTypes.DOUBLE_ARRAY;
            }
            case "boolean": {
                return ManateeDBSupportedTypes.BOOLEAN;
            }
        }
        return ManateeDBSupportedTypes.UNKNOWN;
    }

    public abstract AbstractDBSelect getSelectQuery() throws DBException;

    public abstract AbstractDBInsert getInsertQuery() throws DBException;

    public abstract AbstractDBUpdate getUpdateQuery() throws DBException;

    public abstract AbstractDBDelete getDeleteQuery() throws DBException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy