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