use of org.openforis.idm.model.Attribute in project collect by openforis.
the class RecordUpdater method deleteNode.
/**
* Deletes a node from the record.
*
* @param node
* @return
*/
public NodeChangeSet deleteNode(Node<?> node) {
Record record = node.getRecord();
NodeChangeMap changeMap = new NodeChangeMap();
Set<Node<?>> nodesToBeDeleted = node.getDescendantsAndSelf();
Set<NodePointer> pointersToBeDeleted = nodesToPointers(nodesToBeDeleted);
NodePointer nodePointer = new NodePointer(node);
List<NodePointer> ancestorPointers = getAncestorPointers(node);
Entity parentEntity = node.getParent();
// calculated attributes
List<Attribute<?, ?>> dependentCalculatedAttributes = record.determineCalculatedAttributes(nodesToBeDeleted);
dependentCalculatedAttributes.removeAll(nodesToBeDeleted);
if (validateAfterUpdate) {
// relevance
List<NodePointer> relevanceDependenciesToDeleted = record.determineRelevanceDependentNodes(nodesToBeDeleted);
// min/max
Collection<NodePointer> preDeletionMinMaxDependenciesToCheck = new HashSet<NodePointer>(pointersToBeDeleted);
preDeletionMinMaxDependenciesToCheck.addAll(getAncestorsAndSelfPointers(node));
Collection<NodePointer> minCountDependenciesToDelete = record.determineMinCountDependentNodes(preDeletionMinMaxDependenciesToCheck);
Collection<NodePointer> maxCountDependenciesToDelete = record.determineMaxCountDependentNodes(preDeletionMinMaxDependenciesToCheck);
// validation
Set<Attribute<?, ?>> validationDependenciesToDeleted = record.determineValidationDependentNodes(nodesToBeDeleted);
validationDependenciesToDeleted.removeAll(nodesToBeDeleted);
List<Integer> ancestorIds = node.getAncestorIds();
performNodeDeletion(node);
changeMap.addNodeDeleteChange(record.getId(), ((CollectRecord) record).getStep(), ancestorIds, node);
// calculated attributes
List<Attribute<?, ?>> updatedCalculatedAttributes = recalculateValues(dependentCalculatedAttributes);
changeMap.addValueChanges(updatedCalculatedAttributes);
// relevance
Set<NodePointer> pointersToRecalculateRelevanceFor = new HashSet<NodePointer>();
pointersToRecalculateRelevanceFor.addAll(record.determineRelevanceDependentNodes(updatedCalculatedAttributes));
pointersToRecalculateRelevanceFor.addAll(relevanceDependenciesToDeleted);
if (parentEntity != null) {
pointersToRecalculateRelevanceFor.addAll(record.determineRelevanceDependentNodePointers(Arrays.asList(new NodePointer(parentEntity, node.getDefinition()))));
}
List<NodePointer> relevanceToUpdate = record.determineRelevanceDependentNodePointers(pointersToRecalculateRelevanceFor);
// check relevance update with detached entity in node pointer
RelevanceUpdater relevanceUpdater = new RelevanceUpdater(relevanceToUpdate);
Set<NodePointer> updatedRelevancePointers = relevanceUpdater.update();
changeMap.addRelevanceChanges(updatedRelevancePointers);
// min count
Collection<NodePointer> pointersToCheckMinCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
pointersToCheckMinCountFor.addAll(minCountDependenciesToDelete);
pointersToCheckMinCountFor.addAll(nodesToPointers(updatedCalculatedAttributes));
Collection<NodePointer> minCountPointersToUpdate = record.determineMinCountDependentNodes(pointersToCheckMinCountFor);
Collection<NodePointer> updatedMinCountPointers = updateMinCount(minCountPointersToUpdate);
changeMap.addMinCountChanges(updatedMinCountPointers);
// max count
Collection<NodePointer> pointersToCheckMaxCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
pointersToCheckMaxCountFor.addAll(maxCountDependenciesToDelete);
pointersToCheckMaxCountFor.addAll(nodesToPointers(updatedCalculatedAttributes));
Collection<NodePointer> maxCountPointersToUpdate = record.determineMaxCountDependentNodes(pointersToCheckMaxCountFor);
Collection<NodePointer> updatedMaxCountPointers = updateMaxCount(maxCountPointersToUpdate);
changeMap.addMinCountChanges(updatedMaxCountPointers);
Set<NodePointer> updatedCardinalityPointers = new HashSet<NodePointer>(updatedMinCountPointers);
updatedCardinalityPointers.addAll(updatedMaxCountPointers);
// validate cardinality
Set<NodePointer> pointersToValidateCardinalityFor = new HashSet<NodePointer>(updatedMinCountPointers.size() + updatedMaxCountPointers.size());
pointersToValidateCardinalityFor.addAll(updatedMinCountPointers);
pointersToValidateCardinalityFor.addAll(updatedMaxCountPointers);
// validate cardinality on ancestor node pointers because we are considering empty nodes as missing nodes
pointersToValidateCardinalityFor.add(nodePointer);
pointersToValidateCardinalityFor.addAll(ancestorPointers);
validateCardinality(record, pointersToValidateCardinalityFor, changeMap);
// validate attributes
Set<Node<?>> nodesToCheckValidationFor = new HashSet<Node<?>>(validationDependenciesToDeleted);
nodesToCheckValidationFor.addAll(updatedCalculatedAttributes);
nodesToCheckValidationFor.addAll(pointersToNodes(updatedRelevancePointers));
nodesToCheckValidationFor.addAll(pointersToNodes(updatedCardinalityPointers));
Set<Attribute<?, ?>> attributesToRevalidate = record.determineValidationDependentNodes(nodesToCheckValidationFor);
validateAttributes(record, attributesToRevalidate, changeMap);
} else {
performNodeDeletion(node);
// calculated attributes
List<Attribute<?, ?>> updatedCalculatedAttributes = recalculateValues(dependentCalculatedAttributes);
changeMap.addValueChanges(updatedCalculatedAttributes);
}
return changeMap;
}
use of org.openforis.idm.model.Attribute in project collect by openforis.
the class RecordValidationReportGenerator method extractAttributeValidationResultItem.
protected List<RecordValidationReportItem> extractAttributeValidationResultItem(Locale locale, Integer attrId, ValidationResultFlag level, boolean includeConfirmedErrors) {
List<RecordValidationReportItem> items = new ArrayList<RecordValidationReportItem>();
Attribute<?, ?> attr = (Attribute<?, ?>) record.getNodeByInternalId(attrId);
ValidationResults validationResults = validationCache.getAttributeValidationResults(attrId);
List<ValidationResult> failed = validationResults.getFailed();
if (CollectionUtils.isNotEmpty(failed)) {
String path = getPath(attr);
String prettyFormatPath = messageBuilder.getPrettyFormatPath(attr, locale);
for (ValidationResult validationResult : failed) {
ValidationResultFlag flag = validationResult.getFlag();
if (isInLevel(flag, level) || flag == ValidationResultFlag.WARNING && includeConfirmedErrors && record.isErrorConfirmed(attr)) {
String message = messageBuilder.getValidationMessage(attr, validationResult, locale);
RecordValidationReportItem recordValidationItem = new RecordValidationReportItem(attrId, path, prettyFormatPath, flag, message);
items.add(recordValidationItem);
}
}
}
return items;
}
use of org.openforis.idm.model.Attribute in project collect by openforis.
the class RecordUpdaterTest method testRemoveEntity.
@Test
public void testRemoveEntity() {
record(rootEntityDef(entityDef("time_study", attributeDef("start_time")).multiple().required()), entity("time_study", attribute("start_time", "start first")), entity("time_study", attribute("start_time", "start second")));
Entity timeStudy1 = entityByPath("/root/time_study[1]");
updater.deleteNode(timeStudy1);
Entity rootEntity = record.getRootEntity();
assertEquals(1, rootEntity.getCount("time_study"));
Entity timeStudy2 = (Entity) rootEntity.getChild("time_study", 0);
Attribute<?, ?> startTime2 = (Attribute<?, ?>) timeStudy2.getChild("start_time");
assertEquals(new TextValue("start second"), startTime2.getValue());
}
Aggregations