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

com.github.molcikas.photon.blueprints.TableBlueprint Maven / Gradle / Ivy

package com.github.molcikas.photon.blueprints;

import com.github.molcikas.photon.converters.Converter;
import com.github.molcikas.photon.query.PopulatedEntity;

import java.util.*;
import java.util.stream.Collectors;

public class TableBlueprint
{
    private List columns;
    private ColumnBlueprint primaryKeyColumn;
    private ColumnBlueprint foreignKeyToParentColumn;
    private boolean isPrimaryKeyMappedToField;

    private String tableName;
    private JoinType joinType;
    private String orderBySql;
    private TableBlueprint parentTableBlueprint;
    private String parentTableName;
    private Class entityClass;

    private String selectSql;
    private String selectWhereSql;
    private String selectByIdSql;
    private String updateSql;
    private String insertSql;
    private String insertWithPrimaryKeySql;
    private String deleteSql;
    private String deleteChildrenExceptSql;
    private String selectOrphansSql;
    private Map deleteOrphansSql;

    TableBlueprint(
        TableBlueprint parentTableBlueprint,
        String parentTableName,
        JoinType joinType,
        Class entityClass,
        List columns,
        ColumnBlueprint primaryKeyColumn,
        ColumnBlueprint foreignKeyToParentColumn,
        boolean isPrimaryKeyMappedToField,
        String tableName,
        String orderBySql)
    {
        this.parentTableBlueprint = parentTableBlueprint;
        this.parentTableName = parentTableName;
        this.joinType = joinType;
        this.entityClass = entityClass;
        this.columns = columns;
        this.primaryKeyColumn = primaryKeyColumn;
        this.foreignKeyToParentColumn = foreignKeyToParentColumn;
        this.isPrimaryKeyMappedToField = isPrimaryKeyMappedToField;
        this.tableName = tableName;
        this.orderBySql = orderBySql;
        this.deleteOrphansSql = new HashMap<>();
    }

    public JoinType getJoinType()
    {
        return joinType;
    }

    public Class getEntityClass()
    {
        return entityClass;
    }

    public List getColumns()
    {
        return Collections.unmodifiableList(columns);
    }

    public ColumnBlueprint getPrimaryKeyColumn()
    {
        return primaryKeyColumn;
    }

    public String getPrimaryKeyColumnName()
    {
        return primaryKeyColumn != null ? primaryKeyColumn.getColumnName() : null;
    }

    public ColumnBlueprint getForeignKeyToParentColumn()
    {
        return foreignKeyToParentColumn;
    }

    public String getForeignKeyToParentColumnNameQualified()
    {
        return foreignKeyToParentColumn != null ? foreignKeyToParentColumn.getColumnNameQualified() : null;
    }

    public boolean isPrimaryKeyMappedToField()
    {
        return isPrimaryKeyMappedToField;
    }

    public String getTableName()
    {
        return tableName;
    }

    public String getOrderBySql()
    {
        return orderBySql;
    }

    public TableBlueprint getParentTableBlueprint()
    {
        return parentTableBlueprint;
    }

    public String getParentTableName()
    {
        return parentTableName;
    }

    public String getPrimaryKeyColumnNameQualified()
    {
        return primaryKeyColumn != null ? primaryKeyColumn.getColumnNameQualified() : null;
    }

    public Optional getColumn(String columnName)
    {
        return columns
            .stream()
            .filter(c -> c.getColumnName().equals(columnName))
            .findFirst();
    }

    public List getColumnsForInsertStatement(boolean alwaysIncludePrimaryKey)
    {
        if(alwaysIncludePrimaryKey)
        {
            return Collections.unmodifiableList(columns);
        }
        return columns
            .stream()
            .filter(c -> !c.isPrimaryKeyColumn() || (c.isPrimaryKeyColumn() && !c.isAutoIncrementColumn()))
            .collect(Collectors.toList());
    }

    public boolean shouldInsertUsingPrimaryKeySql(PopulatedEntity populatedEntity)
    {
        if (!getPrimaryKeyColumn().isAutoIncrementColumn())
        {
            return false;
        }
        Object primaryKey = populatedEntity.getPrimaryKeyValue();
        if (primaryKey == null || !Number.class.isAssignableFrom(primaryKey.getClass()))
        {
            return false;
        }
        return ((Number) primaryKey).longValue() != 0L;
    }

    public boolean isApplicableForEntityClass(Class entityClass)
    {
        return this.entityClass == null || entityClass.isAssignableFrom(this.entityClass);
    }

    public Converter getPrimaryKeyColumnSerializer()
    {
        return primaryKeyColumn.getCustomSerializer();
    }

    public String getSelectSql()
    {
        return selectSql;
    }

    public String getSelectWhereSql()
    {
        return selectWhereSql;
    }

    public String getSelectByIdSql()
    {
        return selectByIdSql;
    }

    public String getUpdateSql()
    {
        return updateSql;
    }

    public String getInsertSql()
    {
        return insertSql;
    }

    public String getInsertWithPrimaryKeySql()
    {
        return insertWithPrimaryKeySql;
    }

    public String getDeleteSql()
    {
        return deleteSql;
    }

    public String getDeleteChildrenExceptSql()
    {
        return deleteChildrenExceptSql;
    }

    public String getSelectOrphansSql()
    {
        return selectOrphansSql;
    }

    public String getDeleteOrphansSql(int level)
    {
        return deleteOrphansSql.get(level);
    }

    public void setSelectSql(String selectSql)
    {
        this.selectSql = selectSql;
    }

    public void setSelectWhereSql(String selectWhereSql)
    {
        this.selectWhereSql = selectWhereSql;
    }

    public void setSelectByIdSql(String selectByIdSql)
    {
        this.selectByIdSql = selectByIdSql;
    }

    public void setUpdateSql(String updateSql)
    {
        this.updateSql = updateSql;
    }

    public void setInsertSql(String insertSql)
    {
        this.insertSql = insertSql;
    }

    public void setInsertWithPrimaryKeySql(String insertWithPrimaryKeySql)
    {
        this.insertWithPrimaryKeySql = insertWithPrimaryKeySql;
    }

    public void setDeleteSql(String deleteSql)
    {
        this.deleteSql = deleteSql;
    }

    public void setDeleteChildrenExceptSql(String deleteChildrenExceptSql)
    {
        this.deleteChildrenExceptSql = deleteChildrenExceptSql;
    }

    public void setSelectOrphansSql(String selectOrphansSql)
    {
        this.selectOrphansSql = selectOrphansSql;
    }

    public void setDeleteOrphansSql(String deleteOrphanSql, int parentLevelsUpForOrphanIds)
    {
        deleteOrphansSql.put(parentLevelsUpForOrphanIds, deleteOrphanSql);
    }

    public void setParentTableBlueprint(TableBlueprint parentTableBlueprint)
    {
        this.parentTableBlueprint = parentTableBlueprint;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy