Search in sources :

Example 1 with FieldValueKey

use of org.openforis.collect.io.data.DataLine.FieldValueKey in project collect by openforis.

the class CSVDataImportProcess method setKeyValues.

private void setKeyValues(Entity entity, Value[] values, Map<FieldValueKey, String> colNamesByField, long row) {
    // create key attribute values by name
    Map<FieldValueKey, String> keyValuesByField = new HashMap<FieldValueKey, String>();
    EntityDefinition entityDefn = entity.getDefinition();
    List<AttributeDefinition> keyDefns = entityDefn.getKeyAttributeDefinitions();
    for (int i = 0; i < keyDefns.size(); i++) {
        AttributeDefinition keyDefn = keyDefns.get(i);
        Value keyValue = values[i];
        Map<String, Object> keyValueMap = keyValue.toMap();
        List<String> keyFieldNames = keyDefn.getKeyFieldNames();
        for (String keyFieldName : keyFieldNames) {
            Object keyValueFieldVal = keyValueMap.get(keyFieldName);
            keyValuesByField.put(new FieldValueKey(keyDefn, keyFieldName), keyValueFieldVal.toString());
        }
    }
    setValuesInAttributes(entity, keyValuesByField, colNamesByField, row);
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) HashMap(java.util.HashMap) FieldValueKey(org.openforis.collect.io.data.DataLine.FieldValueKey) AbstractValue(org.openforis.idm.model.AbstractValue) Value(org.openforis.idm.model.Value) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition)

Example 2 with FieldValueKey

use of org.openforis.collect.io.data.DataLine.FieldValueKey in project collect by openforis.

the class CSVDataImportProcess method setValuesInAttributes.

private void setValuesInAttributes(Entity ancestorEntity, Map<FieldValueKey, String> fieldValues, Map<FieldValueKey, String> colNameByField, long row) {
    Set<Entry<FieldValueKey, String>> entrySet = fieldValues.entrySet();
    // delete all multiple attributes
    for (Entry<FieldValueKey, String> entry : entrySet) {
        FieldValueKey fieldValueKey = entry.getKey();
        EntityDefinition ancestorDefn = ancestorEntity.getDefinition();
        Schema schema = ancestorDefn.getSchema();
        AttributeDefinition attrDefn = (AttributeDefinition) schema.getDefinitionById(fieldValueKey.getAttributeDefinitionId());
        Entity parentEntity = getOrCreateParentEntity(ancestorEntity, attrDefn);
        if (attrDefn.isMultiple()) {
            List<Node<?>> attributes = parentEntity.getChildren(attrDefn);
            int tot = attributes.size();
            for (int i = 0; i < tot; i++) {
                Node<?> node = attributes.get(0);
                NodeChangeSet changes = recordUpdater.deleteNode(node);
                if (nodeChangeBatchProcessor != null) {
                    nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
                }
            }
        }
    }
    // set values
    for (Entry<FieldValueKey, String> entry : entrySet) {
        FieldValueKey fieldValueKey = entry.getKey();
        String strValue = entry.getValue();
        EntityDefinition ancestorDefn = ancestorEntity.getDefinition();
        Schema schema = ancestorDefn.getSchema();
        AttributeDefinition attrDefn = (AttributeDefinition) schema.getDefinitionById(fieldValueKey.getAttributeDefinitionId());
        String fieldName = fieldValueKey.getFieldName();
        Entity parentEntity = getOrCreateParentEntity(ancestorEntity, attrDefn);
        String colName = colNameByField.get(fieldValueKey);
        int attrPos = fieldValueKey.getAttributePosition();
        setValueInField(parentEntity, attrDefn, attrPos - 1, fieldName, strValue, colName, row);
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) FieldValueKey(org.openforis.collect.io.data.DataLine.FieldValueKey) Schema(org.openforis.idm.metamodel.Schema) Node(org.openforis.idm.model.Node) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entry(java.util.Map.Entry)

Aggregations

FieldValueKey (org.openforis.collect.io.data.DataLine.FieldValueKey)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)2 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)2 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)1 Schema (org.openforis.idm.metamodel.Schema)1 AbstractValue (org.openforis.idm.model.AbstractValue)1 Entity (org.openforis.idm.model.Entity)1 Node (org.openforis.idm.model.Node)1 Value (org.openforis.idm.model.Value)1