use of org.openforis.collect.relational.model.DataPrimaryKeyColumn in project collect by openforis.
the class JooqDatabaseExporter method insertNode.
private void insertNode(int recordId, Integer parentId, int nodeId, int nodeDefinitionId) {
DataTable table = schema.getDataTableByDefinitionId(nodeDefinitionId);
NodeDefinition nodeDef = table.getNodeDefinition();
BigInteger pkValue = getTableArtificialPK(recordId, nodeDef, nodeId);
QueryCreator queryCreator = new QueryCreator(dsl, schema.getName());
DataPrimaryKeyColumn pkColumn = table.getPrimaryKeyColumn();
org.jooq.Table<Record> jooqTable = queryCreator.getJooqTable(table);
InsertSetMoreStep<Record> insert = dsl.insertInto(jooqTable).set(field(pkColumn.getName()), pkValue);
if (parentId != null) {
Map<String, BigInteger> ancestorFKByColumnName = findAncestorFKByColumnName(schema, table, recordId, parentId);
for (Entry<String, BigInteger> entry : ancestorFKByColumnName.entrySet()) {
insert.set(field(entry.getKey()), entry.getValue());
}
}
try {
insert.execute();
} catch (DataAccessException e) {
if (JooqDaoSupport.isConstraintViolation(e)) {
LOG.warn(String.format("Duplicate node already inserted: survey = %s, node path = %s, record id = %d, parent id = %d, node id = %d", schema.getSurvey().getName(), nodeDef.getPath(), recordId, parentId, nodeId));
} else {
throw new DataAccessException("Failed to insert node into RDB", e);
}
}
}
use of org.openforis.collect.relational.model.DataPrimaryKeyColumn 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;
}
Aggregations