use of org.openforis.idm.model.CodeAttribute in project collect by openforis.
the class RecordUpdater method addEmptyEnumeratedEntities.
private void addEmptyEnumeratedEntities(Entity parentEntity, EntityDefinition enumerableEntityDefn) {
Record record = parentEntity.getRecord();
ModelVersion version = record.getVersion();
CodeAttributeDefinition enumeratingCodeDefn = enumerableEntityDefn.getEnumeratingKeyCodeAttribute(version);
if (enumeratingCodeDefn != null) {
CodeList list = enumeratingCodeDefn.getList();
Survey survey = record.getSurvey();
CodeListService codeListService = survey.getContext().getCodeListService();
List<CodeListItem> items = codeListService.loadRootItems(list);
int i = 0;
for (CodeListItem item : items) {
if (version == null || version.isApplicable(item)) {
String code = item.getCode();
Entity enumeratedEntity = parentEntity.getEnumeratedEntity(enumerableEntityDefn, enumeratingCodeDefn, code);
if (enumeratedEntity == null) {
Entity addedEntity = performEntityAdd(parentEntity, enumerableEntityDefn, i);
addEmptyNodes(addedEntity);
// set the value of the key CodeAttribute
CodeAttribute addedCode = (CodeAttribute) addedEntity.getChild(enumeratingCodeDefn, 0);
addedCode.setValue(new Code(code));
addedCode.updateSummaryInfo();
} else if (enumeratedEntity.getIndex() != i) {
parentEntity.move(enumerableEntityDefn, enumeratedEntity.getIndex(), i);
}
i++;
}
}
}
}
use of org.openforis.idm.model.CodeAttribute in project collect by openforis.
the class RecordUpdater method afterAttributeInsertOrUpdate.
private NodeChangeSet afterAttributeInsertOrUpdate(NodeChangeMap changeMap, Attribute<?, ?> attribute) {
attribute.updateSummaryInfo();
Record record = attribute.getRecord();
NodePointer attributeNodePointer = new NodePointer(attribute);
List<Attribute<?, ?>> updatedAttributes = new ArrayList<Attribute<?, ?>>();
// calculated attributes
List<Attribute<?, ?>> updatedCalculatedAttributes = recalculateDependentCalculatedAttributes(attribute);
updatedAttributes.addAll(updatedCalculatedAttributes);
changeMap.addValueChanges(updatedCalculatedAttributes);
// dependent code attributes
if (attribute instanceof CodeAttribute && clearDependentCodeAttributes) {
Set<CodeAttribute> updatedCodeAttributes = clearDependentCodeAttributes(attribute);
updatedAttributes.addAll(updatedCodeAttributes);
changeMap.addValueChanges(updatedCodeAttributes);
}
if (validateAfterUpdate) {
// relevance
Collection<Node<?>> nodesToCheckRelevanceFor = new ArrayList<Node<?>>(updatedAttributes);
nodesToCheckRelevanceFor.add(attribute);
List<NodePointer> relevanceToUpdate = record.determineRelevanceDependentNodes(nodesToCheckRelevanceFor);
RelevanceUpdater relevanceUpdater = new RelevanceUpdater(relevanceToUpdate);
Set<NodePointer> updatedRelevancePointers = relevanceUpdater.update();
Set<Node<?>> updatedRelevanceNodes = pointersToNodes(updatedRelevancePointers);
// apply default values to relevant nodes (if not applied yet)
for (Node<?> updatedRelevanceNode : updatedRelevanceNodes) {
if (updatedRelevanceNode instanceof Attribute) {
Attribute<?, ?> updatedRelevanceAttr = (Attribute<?, ?>) updatedRelevanceNode;
if (!updatedRelevanceAttr.getDefinition().isCalculated()) {
if (updatedRelevanceAttr.isEmpty() || isDefaultValueApplied(updatedRelevanceAttr)) {
Value appliedValue = applyInitialValue(updatedRelevanceAttr);
if (appliedValue != null) {
updatedAttributes.add((Attribute<?, ?>) updatedRelevanceNode);
}
}
}
}
}
changeMap.addRelevanceChanges(updatedRelevancePointers);
if (clearNotRelevantAttributes) {
Set<Attribute<?, ?>> noMoreRelevantAttributes = retainNotRelevantAttributes(updatedRelevanceNodes);
Set<Attribute<?, ?>> clearedAttributes = clearUserSpecifiedAttributes(noMoreRelevantAttributes);
updatedAttributes.addAll(clearedAttributes);
changeMap.addValueChanges(clearedAttributes);
}
// min count
Collection<NodePointer> pointersToCheckMinCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
pointersToCheckMinCountFor.add(attributeNodePointer);
pointersToCheckMinCountFor.addAll(nodesToPointers(updatedAttributes));
Collection<NodePointer> minCountPointersToUpdate = record.determineMinCountDependentNodes(pointersToCheckMinCountFor);
Collection<NodePointer> updatedMinCountPointers = updateMinCount(minCountPointersToUpdate);
changeMap.addMinCountChanges(updatedMinCountPointers);
// max count
Collection<NodePointer> pointersToCheckMaxCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
pointersToCheckMaxCountFor.add(attributeNodePointer);
pointersToCheckMaxCountFor.addAll(nodesToPointers(updatedAttributes));
Collection<NodePointer> maxCountPointersToUpdate = record.determineMaxCountDependentNodes(pointersToCheckMaxCountFor);
Collection<NodePointer> updatedMaxCountPointers = updateMaxCount(maxCountPointersToUpdate);
changeMap.addMaxCountChanges(updatedMaxCountPointers);
Set<NodePointer> updatedCardinalityPointers = new HashSet<NodePointer>(updatedMinCountPointers);
updatedCardinalityPointers.addAll(updatedMaxCountPointers);
// validate cardinality
List<NodePointer> ancestorsAndSelfPointers = getAncestorsAndSelfPointers(attribute);
Set<NodePointer> pointersToValidateCardinalityFor = new HashSet<NodePointer>(updatedMinCountPointers.size() + updatedMaxCountPointers.size() + updatedRelevancePointers.size() + ancestorsAndSelfPointers.size());
pointersToValidateCardinalityFor.addAll(nodesToPointers(updatedAttributes));
pointersToValidateCardinalityFor.addAll(updatedMinCountPointers);
pointersToValidateCardinalityFor.addAll(updatedMaxCountPointers);
pointersToValidateCardinalityFor.addAll(updatedRelevancePointers);
// validate cardinality on ancestor node pointers because we are considering empty nodes as missing nodes
pointersToValidateCardinalityFor.addAll(ancestorsAndSelfPointers);
validateCardinality(record, pointersToValidateCardinalityFor, changeMap);
// validate attributes
Set<Node<?>> nodesToCheckValidationFor = new HashSet<Node<?>>(updatedAttributes);
nodesToCheckValidationFor.add(attribute);
nodesToCheckValidationFor.addAll(updatedRelevanceNodes);
nodesToCheckValidationFor.addAll(pointersToNodes(updatedCardinalityPointers));
Set<Attribute<?, ?>> attributesToRevalidate = record.determineValidationDependentNodes(nodesToCheckValidationFor);
validateAttributes(record, attributesToRevalidate, changeMap);
}
return changeMap;
}
use of org.openforis.idm.model.CodeAttribute in project collect by openforis.
the class CodeListItemProxy method setSelectedItems.
public static void setSelectedItems(List<CodeListItemProxy> proxies, List<Node<? extends NodeDefinition>> codes) {
for (CodeListItemProxy proxy : proxies) {
// if code in attributes, set selected and qualifier in proxy
for (Node<? extends NodeDefinition> node : codes) {
CodeAttribute code = (CodeAttribute) node;
Code codeVal = code.getValue();
if (codeVal != null && codeVal.getCode() != null && codeVal.getCode().equals(proxy.getCode())) {
proxy.setSelected(Boolean.TRUE);
proxy.setQualifier(codeVal.getQualifier());
}
}
}
}
use of org.openforis.idm.model.CodeAttribute in project collect by openforis.
the class CSVDataImportProcessIntegrationTest method nestedEntityTest.
@Test
public void nestedEntityTest() throws Exception {
CollectRecord record = createTestRecord(survey, "10_114");
recordDao.insert(record);
EntityDefinition clusterDefn = survey.getSchema().getRootEntityDefinition("cluster");
EntityDefinition plotDefn = (EntityDefinition) clusterDefn.getChildDefinition("plot");
CSVDataImportProcess process = importCSVFile(VALID_NESTED_ENTITY_TEST_CSV, plotDefn.getId());
ReferenceDataImportStatus<ParsingError> status = process.getStatus();
assertTrue(status.isComplete());
assertTrue(status.getSkippedRows().isEmpty());
assertEquals(3, status.getProcessed());
CollectRecord reloadedRecord = recordDao.load(survey, record.getId(), Step.ENTRY);
Entity reloadedCluster = reloadedRecord.getRootEntity();
{
Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "1", "A").get(0);
CodeAttribute landUse = (CodeAttribute) plot.getChild("land_use");
assertEquals("2", landUse.getValue().getCode());
}
{
Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "2", "B").get(0);
CodeAttribute landUse = (CodeAttribute) plot.getChild("land_use");
assertEquals("3", landUse.getValue().getCode());
}
}
use of org.openforis.idm.model.CodeAttribute in project collect by openforis.
the class CollectRecordIntegrationTest method testUpdateValue.
@Test
public void testUpdateValue() throws Exception {
CollectSurvey survey = loadSurvey();
CollectRecord record = createTestRecord(survey);
Entity cluster = record.getRootEntity();
CodeAttribute region = (CodeAttribute) cluster.getChild("region", 0);
{
NodeChangeSet nodeChangeSet = recordUpdater.updateAttribute(region, FieldSymbol.BLANK_ON_FORM);
assertTrue(region.isEmpty());
assertEquals(FieldSymbol.BLANK_ON_FORM, FieldSymbol.valueOf(region.getCodeField().getSymbol()));
assertEquals(FieldSymbol.BLANK_ON_FORM, FieldSymbol.valueOf(region.getQualifierField().getSymbol()));
NodeChange<?> regionChange = nodeChangeSet.getChange(region);
assertTrue(regionChange instanceof AttributeChange);
assertEquals(region, regionChange.getNode());
Map<Integer, Object> updatedFieldValues = ((AttributeChange) regionChange).getUpdatedFieldValues();
Map<Integer, Object> expectedValues = new HashMap<Integer, Object>();
expectedValues.put(Integer.valueOf(0), null);
expectedValues.put(Integer.valueOf(1), null);
assertEquals(expectedValues, updatedFieldValues);
}
{
NodeChangeSet nodeChangeSet = recordUpdater.updateAttribute(region, new Code("AAA"));
assertFalse(region.isEmpty());
assertEquals(null, FieldSymbol.valueOf(region.getCodeField().getSymbol()));
assertEquals(null, FieldSymbol.valueOf(region.getQualifierField().getSymbol()));
NodeChange<?> regionChange = nodeChangeSet.getChange(region);
assertTrue(regionChange instanceof AttributeChange);
assertEquals(region, regionChange.getNode());
Map<Integer, Object> updatedFieldValues = ((AttributeChange) regionChange).getUpdatedFieldValues();
Map<Integer, Object> expectedValues = new HashMap<Integer, Object>();
expectedValues.put(Integer.valueOf(0), "AAA");
expectedValues.put(Integer.valueOf(1), null);
assertEquals(expectedValues, updatedFieldValues);
}
}
Aggregations