Search in sources :

Example 86 with Entity

use of org.openforis.idm.model.Entity in project collect by openforis.

the class CSVDataImportProcess method deleteAllParentEntities.

private void deleteAllParentEntities(CollectRecord record) {
    String parentEntitiesPath = getParentEntityDefinition().getPath();
    List<Entity> entitiesToBeDeleted = record.findNodesByPath(parentEntitiesPath);
    for (Entity entity : entitiesToBeDeleted) {
        NodeChangeSet changes = recordUpdater.deleteNode(entity);
        if (nodeChangeBatchProcessor != null) {
            nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet)

Example 87 with Entity

use of org.openforis.idm.model.Entity in project collect by openforis.

the class CSVDataImportProcess method getOrCreateParentEntity.

// @SuppressWarnings("unchecked")
// private <T extends Value> void setValueInAttribute(Attribute<?, T> attr, String strVal) {
// T val;
// if ( attr instanceof DateAttribute ) {
// val = (T) Date.parseDate(strVal);
// } else if ( attr instanceof CodeAttribute ) {
// val = (T) new Code(strVal);
// } else if ( attr instanceof CoordinateAttribute ) {
// val = (T) Coordinate.parseCoordinate(strVal);
// } else if ( attr instanceof TextAttribute ) {
// val = (T) new TextValue(strVal);
// } else {
// throw new UnsupportedOperationException("Attribute type not supported: " + attr.getClass().getName());
// }
// attr.setValue(val);
// }
private Entity getOrCreateParentEntity(CollectRecord record, DataLine line) {
    Survey survey = record.getSurvey();
    Schema schema = survey.getSchema();
    EntityDefinition parentEntityDefn = (EntityDefinition) schema.getDefinitionById(parentEntityDefinitionId);
    Entity rootEntity = record.getRootEntity();
    Entity currentParent = rootEntity;
    List<EntityDefinition> ancestorEntityDefns = parentEntityDefn.getAncestorEntityDefinitionsInReverseOrder();
    ancestorEntityDefns.add(parentEntityDefn);
    // skip the root entity
    for (int i = 1; i < ancestorEntityDefns.size(); i++) {
        EntityDefinition ancestorDefn = ancestorEntityDefns.get(i);
        String ancestorName = ancestorDefn.getName();
        EntityIdentifier<?> identifier = line.getAncestorIdentifier(ancestorDefn.getId());
        Entity childEntity;
        if (ancestorDefn.isMultiple()) {
            List<Entity> childEntities = findChildEntities(currentParent, ancestorName, identifier);
            switch(childEntities.size()) {
                case 0:
                    if (settings.isCreateAncestorEntities() || ancestorDefn == parentEntityDefn) {
                        childEntity = createChildEntity(currentParent, ancestorName, identifier, line.getColumnNamesByField(), line.getLineNumber());
                    } else {
                        status.addParsingError(createParentEntitySearchError(record, line, identifier, PARENT_ENTITY_NOT_FOUND_MESSAGE_KEY));
                        return null;
                    }
                    break;
                case 1:
                    childEntity = childEntities.get(0);
                    break;
                default:
                    status.addParsingError(createParentEntitySearchError(record, line, identifier, MULTIPLE_PARENT_ENTITY_FOUND_MESSAGE_KEY));
                    return null;
            }
        } else {
            if (currentParent.getCount(ancestorDefn) == 0) {
                Node<?> newNode = ancestorDefn.createNode();
                currentParent.add(newNode);
            }
            childEntity = (Entity) currentParent.getChild(ancestorDefn);
        }
        currentParent = childEntity;
    }
    return currentParent;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) CollectSurvey(org.openforis.collect.model.CollectSurvey) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema)

Example 88 with Entity

use of org.openforis.idm.model.Entity in project collect by openforis.

the class CSVDataImportProcess method findChildEntities.

private List<Entity> findChildEntities(Entity currentParent, String childName, EntityIdentifier<?> identifier) {
    if (identifier instanceof EntityPositionIdentifier) {
        int position = ((EntityPositionIdentifier) identifier).getPosition();
        if (currentParent.getCount(childName) >= position) {
            ArrayList<Entity> result = new ArrayList<Entity>();
            Entity child = (Entity) currentParent.getChild(childName, position - 1);
            result.add(child);
            return result;
        } else {
            return Collections.emptyList();
        }
    } else {
        EntityDefinition parentDefn = currentParent.getDefinition();
        EntityDefinition childDefn = parentDefn.getChildDefinition(childName, EntityDefinition.class);
        Value[] keyValues = ((EntityKeysIdentifier) identifier).getKeyValues();
        return currentParent.findChildEntitiesByKeys(childDefn, keyValues);
    }
}
Also used : EntityPositionIdentifier(org.openforis.collect.io.data.DataLine.EntityPositionIdentifier) Entity(org.openforis.idm.model.Entity) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) EntityKeysIdentifier(org.openforis.collect.io.data.DataLine.EntityKeysIdentifier) ArrayList(java.util.ArrayList) AbstractValue(org.openforis.idm.model.AbstractValue) Value(org.openforis.idm.model.Value)

Example 89 with Entity

use of org.openforis.idm.model.Entity in project collect by openforis.

the class CSVDataImportProcess method getOrCreateParentEntity.

private Entity getOrCreateParentEntity(Entity ancestorEntity, AttributeDefinition attrDefn) {
    EntityDefinition ancestorEntityDefn = ancestorEntity.getDefinition();
    List<EntityDefinition> attributeAncestors = attrDefn.getAncestorEntityDefinitionsInReverseOrder();
    int indexOfAncestorEntity = attributeAncestors.indexOf(ancestorEntityDefn);
    if (indexOfAncestorEntity < 0) {
        throw new IllegalArgumentException("AttributeDefinition is not among the ancestor entity descendants");
    } else if (indexOfAncestorEntity == attributeAncestors.size() - 1) {
        return ancestorEntity;
    } else {
        Entity currentParent = ancestorEntity;
        List<EntityDefinition> nearestAncestors = attributeAncestors.subList(indexOfAncestorEntity + 1, attributeAncestors.size());
        for (EntityDefinition ancestor : nearestAncestors) {
            if (currentParent.getCount(ancestor) == 0) {
                Entity newNode = (Entity) ancestor.createNode();
                currentParent.add(newNode);
                currentParent = newNode;
            } else {
                currentParent = (Entity) currentParent.getChild(ancestor);
            }
        }
        return currentParent;
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) List(java.util.List) ArrayList(java.util.ArrayList)

Example 90 with Entity

use of org.openforis.idm.model.Entity in project collect by openforis.

the class DescendantNodeFilter method accept.

@Override
public boolean accept(Node<?> node) {
    NodeDefinition nodeDef = node.getDefinition();
    if (!(nodeDef instanceof EntityDefinition) || !descendantAttributeDefinition.isDescendantOf((EntityDefinition) nodeDef)) {
        return false;
    }
    Record record = node.getRecord();
    SurveyContext surveyContext = record.getSurveyContext();
    ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
    List<Node<?>> attributes = record.findNodesByPath(descendantAttributeDefinition.getPath());
    for (Node<?> attribute : attributes) {
        try {
            Entity parentEntity = attribute.getParent();
            if (parentEntity == node && expressionEvaluator.evaluateBoolean(parentEntity, attribute, descendantAttributeCondition)) {
                return true;
            }
        } catch (InvalidExpressionException e) {
            throw new RuntimeException(e);
        }
    }
    return false;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) Record(org.openforis.idm.model.Record) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Aggregations

Entity (org.openforis.idm.model.Entity)164 Test (org.junit.Test)88 CollectRecord (org.openforis.collect.model.CollectRecord)37 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)36 Code (org.openforis.idm.model.Code)35 RealAttribute (org.openforis.idm.model.RealAttribute)25 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)23 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)19 Node (org.openforis.idm.model.Node)19 AbstractTest (org.openforis.idm.AbstractTest)18 Date (org.openforis.idm.model.Date)16 ArrayList (java.util.ArrayList)14 CodeAttribute (org.openforis.idm.model.CodeAttribute)14 Time (org.openforis.idm.model.Time)12 GregorianCalendar (java.util.GregorianCalendar)9 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)9 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)9 TextAttribute (org.openforis.idm.model.TextAttribute)9 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)8 Attribute (org.openforis.idm.model.Attribute)8