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