Search in sources :

Example 1 with DataAncestorFKColumn

use of org.openforis.collect.relational.model.DataAncestorFKColumn in project collect by openforis.

the class JooqDatabaseExporter method deleteEntity.

@Override
public void deleteEntity(int recordId, int entityId, int entityDefinitionId) {
    DataTable tableToDeleteFor = schema.getDataTableByDefinitionId(entityDefinitionId);
    EntityDefinition entityDefToDeleteFor = (EntityDefinition) tableToDeleteFor.getNodeDefinition();
    BigInteger pkValue = getTableArtificialPK(recordId, entityDefToDeleteFor, entityId);
    // delete data from the actual table
    batchExecutor.addDelete(tableToDeleteFor, tableToDeleteFor.getPrimaryKeyColumn(), pkValue);
    // delete data from descendant tables
    List<DataTable> descendantTables = new ArrayList<DataTable>(schema.getDescendantTablesForDefinition(entityDefinitionId));
    Collections.reverse(descendantTables);
    for (DataTable dataTable : descendantTables) {
        DataAncestorFKColumn ancestorIdColumn = dataTable.getAncestorFKColumn(entityDefinitionId);
        batchExecutor.addDelete(dataTable, ancestorIdColumn, pkValue);
    }
}
Also used : DataTable(org.openforis.collect.relational.model.DataTable) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn)

Example 2 with DataAncestorFKColumn

use of org.openforis.collect.relational.model.DataAncestorFKColumn in project collect by openforis.

the class JooqDatabaseExporter method findAncestorFKByColumnName.

private Map<String, BigInteger> findAncestorFKByColumnName(RelationalSchema schema, DataTable table, int recordId, int parentId) {
    Map<String, BigInteger> result = new HashMap<String, BigInteger>();
    DataTable parentTable = table.getParent();
    List<DataAncestorFKColumn> parentAncestorFKColumns = new ArrayList<DataAncestorFKColumn>(parentTable.getAncestorFKColumns());
    List<Field<?>> parentAncestorColumns = toFields(parentAncestorFKColumns);
    BigInteger parentPKValue = getTableArtificialPK(recordId, parentTable.getNodeDefinition(), parentId);
    DataPrimaryKeyColumn parentPKColumn = parentTable.getPrimaryKeyColumn();
    QueryCreator queryCreator = new QueryCreator(dsl, schema.getName());
    SelectConditionStep<Record> selectAncestorFKs = dsl.select(parentAncestorColumns).from(queryCreator.getJooqTable(parentTable)).where(field(parentPKColumn.getName()).eq(parentPKValue));
    Record record = selectAncestorFKs.fetchOne();
    for (int i = 0; i < parentAncestorColumns.size(); i++) {
        Field<?> parentAncestorField = parentAncestorColumns.get(i);
        DataAncestorFKColumn parentColumn = parentAncestorFKColumns.get(i);
        int ancestorDefinitionId = parentColumn.getAncestorDefinitionId();
        DataAncestorFKColumn column = table.getAncestorFKColumn(ancestorDefinitionId);
        BigInteger ancestorPK = record.getValue(parentAncestorField, BigInteger.class);
        result.put(column.getName(), ancestorPK);
    }
    result.put(table.getParentFKColumn().getName(), parentPKValue);
    return result;
}
Also used : DataTable(org.openforis.collect.relational.model.DataTable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn) Field(org.jooq.Field) BigInteger(java.math.BigInteger) CollectRecord(org.openforis.collect.model.CollectRecord) Record(org.jooq.Record) DataPrimaryKeyColumn(org.openforis.collect.relational.model.DataPrimaryKeyColumn)

Example 3 with DataAncestorFKColumn

use of org.openforis.collect.relational.model.DataAncestorFKColumn in project collect by openforis.

the class JooqRelationalSchemaCreator method createDataTableView.

private void createDataTableView(RelationalSchema schema, DataTable dataTable, Connection conn) {
    CollectDSLContext dsl = new CollectDSLContext(conn);
    List<Field<?>> fields = new ArrayList<Field<?>>();
    List<TableLike<?>> tables = new ArrayList<TableLike<?>>();
    List<Condition> conditions = new ArrayList<Condition>();
    DataTable currentTable = dataTable;
    while (currentTable != null) {
        org.jooq.Table<Record> currentJooqTable = jooqTable(schema, currentTable, !dsl.isSchemaLess());
        tables.add(currentJooqTable);
        List<Column<?>> columns = currentTable.getColumns();
        for (Column<?> column : columns) {
            if (!(column instanceof DataAncestorFKColumn)) {
                fields.add(field(name(currentJooqTable.getName(), column.getName())));
            }
        }
        // add parent table join condition
        DataTable parentTable = currentTable.getParent();
        if (parentTable != null) {
            // names are duplicate, use the table name as prefix in the join condition
            Condition parentTableJoinCondition = field(currentJooqTable.getName() + "." + currentTable.getParentFKColumn().getName()).eq(field(parentTable.getName() + "." + parentTable.getPrimaryKeyColumn().getName()));
            conditions.add(parentTableJoinCondition);
        }
        currentTable = parentTable;
    }
    Select<?> select = dsl.select(fields).from(tables).where(conditions);
    Name name;
    if (dsl.isSchemaLess()) {
        name = name(dataTable.getName() + "_view");
    } else {
        name = name(schema.getName(), dataTable.getName() + "_view");
    }
    dsl.createView(DSL.table(name), fields.toArray(new Field[fields.size()])).as(select).execute();
}
Also used : Condition(org.jooq.Condition) DataTable(org.openforis.collect.relational.model.DataTable) TableLike(org.jooq.TableLike) ArrayList(java.util.ArrayList) CollectDSLContext(org.openforis.collect.persistence.jooq.CollectDSLContext) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn) Name(org.jooq.Name) Field(org.jooq.Field) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn) Column(org.openforis.collect.relational.model.Column) CodeListCodeColumn(org.openforis.collect.relational.model.CodeListCodeColumn) Record(org.jooq.Record)

Aggregations

ArrayList (java.util.ArrayList)3 DataAncestorFKColumn (org.openforis.collect.relational.model.DataAncestorFKColumn)3 DataTable (org.openforis.collect.relational.model.DataTable)3 BigInteger (java.math.BigInteger)2 Field (org.jooq.Field)2 Record (org.jooq.Record)2 HashMap (java.util.HashMap)1 Condition (org.jooq.Condition)1 Name (org.jooq.Name)1 TableLike (org.jooq.TableLike)1 CollectRecord (org.openforis.collect.model.CollectRecord)1 CollectDSLContext (org.openforis.collect.persistence.jooq.CollectDSLContext)1 CodeListCodeColumn (org.openforis.collect.relational.model.CodeListCodeColumn)1 Column (org.openforis.collect.relational.model.Column)1 DataPrimaryKeyColumn (org.openforis.collect.relational.model.DataPrimaryKeyColumn)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1