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

com.wizarius.orm.database.entityreader.WizEntityManager Maven / Gradle / Ivy

There is a newer version: 0.0.27.6
Show newest version
package com.wizarius.orm.database.entityreader;

import com.wizarius.orm.database.DBException;
import com.wizarius.orm.database.actions.WizDBDelete;
import com.wizarius.orm.database.actions.WizDBInsert;
import com.wizarius.orm.database.actions.WizDBSelect;
import com.wizarius.orm.database.actions.WizDBUpdate;
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.connection.DBConnectionPool;
import com.wizarius.orm.database.data.UniqueTableDictionary;

import java.lang.reflect.Field;

/**
 * Created by Vladyslav Shyshkin on 12.11.17.
 */
public class WizEntityManager {
    /**
     * Connections pool
     */
    private final DBConnectionPool pool;

    /**
     * Database connection pool
     *
     * @param pool database connection pool
     */
    public WizEntityManager(DBConnectionPool pool) {
        this.pool = pool;
    }

    /**
     * Get connection type by java type
     *
     * @param field java field refraction
     * @return connection type
     */
    private static DBSupportedTypes javaTypeToDBType(Field field) {
        return javaTypeToDBType(field.getType());
    }

    /**
     * Get connection type by java class
     *
     * @param clazz class instance
     * @return connection type
     */
    public static DBSupportedTypes javaTypeToDBType(Class clazz) {
        if (clazz.isEnum()) {
            return DBSupportedTypes.ENUM;
        }
        switch (clazz.getSimpleName()) {
            case "Integer":
            case "int": {
                return DBSupportedTypes.INTEGER;
            }
            case "int[]": {
                return DBSupportedTypes.INTEGER_ARRAY;
            }
            case "Short":
            case "short": {
                return DBSupportedTypes.SHORT;
            }
            case "short[]": {
                return DBSupportedTypes.SHORT_ARRAY;
            }
            case "String": {
                return DBSupportedTypes.STRING;
            }
            case "String[]": {
                return DBSupportedTypes.STRING_ARRAY;
            }
            case "Byte":
            case "byte": {
                return DBSupportedTypes.BYTE;
            }
            case "byte[]": {
                return DBSupportedTypes.BYTE_ARRAY;
            }
            case "Float":
            case "float": {
                return DBSupportedTypes.FLOAT;
            }
            case "float[]": {
                return DBSupportedTypes.FLOAT_ARRAY;
            }
            case "Long":
            case "long": {
                return DBSupportedTypes.LONG;
            }
            case "long[]": {
                return DBSupportedTypes.LONG_ARRAY;
            }
            case "Character":
            case "char": {
                return DBSupportedTypes.CHAR;
            }
            case "char[]": {
                return DBSupportedTypes.CHAR_ARRAY;
            }
            case "Double":
            case "double": {
                return DBSupportedTypes.DOUBLE;
            }
            case "double[]": {
                return DBSupportedTypes.DOUBLE_ARRAY;
            }
            case "boolean[]": {
                return DBSupportedTypes.BOOLEAN_ARRAY;
            }
            case "Boolean":
            case "boolean": {
                return DBSupportedTypes.BOOLEAN;
            }
            case "BigDecimal": {
                return DBSupportedTypes.BIGDECIMAL;
            }
            case "BigDecimal[]": {
                return DBSupportedTypes.BIGDECIMAL_ARRAY;
            }
            case "Date": {
                return DBSupportedTypes.DATE;
            }
            case "Date[]": {
                return DBSupportedTypes.DATE_ARRAY;
            }
            case "Timestamp": {
                return DBSupportedTypes.TIMESTAMP;
            }
            case "Timestamp[]": {
                return DBSupportedTypes.TIMESTAMP_ARRAY;
            }
        }
        if (clazz.isArray() && clazz.getComponentType().isEnum()) {
            return DBSupportedTypes.ENUM_ARRAY;
        }
        return DBSupportedTypes.UNKNOWN;
    }

    /**
     * Parse entity
     *
     * @param clazz entity clazz
     * @return parsed fields list
     * @throws DBException on parse entity exception
     */
    public DBParsedFieldsList parseEntity(Class clazz) throws DBException {
        return parseEntity(clazz, new UniqueTableDictionary());
    }

    /**
     * Parse entity
     *
     * @param clazz                 clazz name
     * @param uniqueTableDictionary unique table dictionary
     * @return parsed entities
     * @throws DBException on unable to parse
     */
    public DBParsedFieldsList parseEntity(Class clazz, UniqueTableDictionary uniqueTableDictionary) throws DBException {
        Object 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();
        DBParsedFieldsList fieldsList = new DBParsedFieldsList(
                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 DBParsedField(
                        tableName,
                        fieldName,
                        dbFieldName,
                        field.getType(),
                        field,
                        javaTypeToDBType(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();
                Class fieldClass = field.getType();
                DBParsedFieldsList parsedFields = parseEntity(fieldClass, uniqueTableDictionary);
                DBParsedField parsedField = new DBParsedField(
                        tableName,
                        fieldName,
                        "",
                        field.getType(),
                        field,
                        DBSupportedTypes.JOIN_OBJECT,
                        false);
                fieldsList.add(parsedField);
                parsedField.setJoinField(new DBJoinField(parentField, childField, parsedFields));
            }
        }
        return fieldsList;
    }

    /**
     * Returns select query
     *
     * @param clazz entity class
     * @param    generic class
     * @return db select query
     * @throws DBException on unable to parse entity
     */
    public  WizDBSelect getSelectQuery(Class clazz) throws DBException {
        DBParsedFieldsList fields = parseEntity(clazz);
        return new WizDBSelect<>(pool, fields);
    }

    /**
     * Returns insert query
     *
     * @param clazz entity class
     * @param    generic class
     * @return db insert query
     * @throws DBException on unable to parse entity
     */
    public  WizDBInsert getInsertQuery(Class clazz) throws DBException {
        DBParsedFieldsList fields = parseEntity(clazz);
        return new WizDBInsert<>(pool, fields);
    }

    /**
     * Returns update query
     *
     * @param clazz entity class
     * @param    generic class
     * @return db update query
     * @throws DBException on unable to parse entity
     */
    public  WizDBUpdate getUpdateQuery(Class clazz) throws DBException {
        DBParsedFieldsList fields = parseEntity(clazz);
        return new WizDBUpdate<>(pool, fields);
    }

    /**
     * Returns update query
     *
     * @param clazz entity class
     * @param    generic class
     * @return db update query
     * @throws DBException on unable to parse entity
     */
    public  WizDBDelete getDeleteQuery(Class clazz) throws DBException {
        DBParsedFieldsList fields = parseEntity(clazz);
        return new WizDBDelete<>(pool, fields);
    }

    /**
     * Returns entity query builder
     *
     * @param clazz entity class
     * @param    entity
     * @return entity queries
     * @throws DBException on unable to parse entity
     */
    public  EntityQuery getEntityQuery(Class clazz) throws DBException {
        DBParsedFieldsList fields = parseEntity(clazz);
        return new EntityQuery<>(pool, fields);
    }

    /**
     * Returns database connection pool
     *
     * @return connection pool
     */
    public DBConnectionPool getPool() {
        return pool;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy