Search in sources :

Example 1 with DataPrimaryKeyColumn

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);
        }
    }
}
Also used : DataTable(org.openforis.collect.relational.model.DataTable) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) BigInteger(java.math.BigInteger) CollectRecord(org.openforis.collect.model.CollectRecord) Record(org.jooq.Record) DataPrimaryKeyColumn(org.openforis.collect.relational.model.DataPrimaryKeyColumn) DataAccessException(org.jooq.exception.DataAccessException)

Example 2 with DataPrimaryKeyColumn

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

Aggregations

BigInteger (java.math.BigInteger)2 Record (org.jooq.Record)2 CollectRecord (org.openforis.collect.model.CollectRecord)2 DataPrimaryKeyColumn (org.openforis.collect.relational.model.DataPrimaryKeyColumn)2 DataTable (org.openforis.collect.relational.model.DataTable)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Field (org.jooq.Field)1 DataAccessException (org.jooq.exception.DataAccessException)1 DataAncestorFKColumn (org.openforis.collect.relational.model.DataAncestorFKColumn)1 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)1