Search in sources :

Example 6 with Record

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

the class RecordIndexManager method index.

protected void index(IndexWriter indexWriter, Attribute<?, ?> attr) {
    AttributeDefinition defn = attr.getDefinition();
    String indexName = defn.getAnnotation(Annotation.AUTOCOMPLETE.getQName());
    if (StringUtils.isNotBlank(indexName)) {
        try {
            Object value = attr.getValue();
            if (value != null) {
                Document doc = new Document();
                Record record = attr.getRecord();
                Integer recordId = record.getId();
                if (recordId != null) {
                    Field recordKeyField = createRecordIdField(recordId);
                    doc.add(recordKeyField);
                }
                for (org.openforis.idm.model.Field<?> field : attr.getFields()) {
                    index(doc, indexName, field);
                }
                indexWriter.addDocument(doc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Field(org.apache.lucene.document.Field) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) Record(org.openforis.idm.model.Record) CollectRecord(org.openforis.collect.model.CollectRecord) Document(org.apache.lucene.document.Document) ParseException(org.apache.lucene.queryParser.ParseException) IOException(java.io.IOException)

Example 7 with Record

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

the class RecordUpdater method recalculateDependentCalculatedAttributes.

private List<Attribute<?, ?>> recalculateDependentCalculatedAttributes(Node<?> node) {
    Record record = node.getRecord();
    List<Attribute<?, ?>> attributesToRecalculate = record.determineCalculatedAttributes(node);
    return recalculateValues(attributesToRecalculate);
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) Record(org.openforis.idm.model.Record)

Example 8 with Record

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

the class RecordUpdater method addEmptyNodes.

private void addEmptyNodes(Entity entity) {
    Record record = entity.getRecord();
    ModelVersion version = record.getVersion();
    addEmptyEnumeratedEntities(entity);
    EntityDefinition entityDefn = entity.getDefinition();
    List<NodeDefinition> childDefinitions = entityDefn.getChildDefinitionsInVersion(version);
    for (NodeDefinition childDefn : childDefinitions) {
        if (entity.getCount(childDefn) == 0) {
            if (addEmptyMultipleEntitiesWhenAddingNewEntities || !(childDefn instanceof EntityDefinition && childDefn.isMultiple())) {
                int toBeInserted = entity.getMinCount(childDefn);
                if (toBeInserted <= 0 && childDefn instanceof AttributeDefinition || !childDefn.isMultiple()) {
                    // insert at least one node
                    toBeInserted = 1;
                }
                addEmptyChildren(entity, childDefn, toBeInserted);
            }
        } else {
            entity.visitChildren(childDefn, new NodeVisitor() {

                public void visit(Node<? extends NodeDefinition> child, int idx) {
                    if (child instanceof Entity) {
                        addEmptyNodes((Entity) child);
                    }
                }
            });
        }
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 9 with Record

use of org.openforis.idm.model.Record 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++;
            }
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) CodeListService(org.openforis.idm.metamodel.CodeListService) Code(org.openforis.idm.model.Code) CodeList(org.openforis.idm.metamodel.CodeList) Survey(org.openforis.idm.metamodel.Survey) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 10 with Record

use of org.openforis.idm.model.Record 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;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) NodePointer(org.openforis.idm.model.NodePointer) CodeAttribute(org.openforis.idm.model.CodeAttribute) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue) Record(org.openforis.idm.model.Record) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Record (org.openforis.idm.model.Record)22 ModelVersion (org.openforis.idm.metamodel.ModelVersion)9 CodeAttribute (org.openforis.idm.model.CodeAttribute)7 ArrayList (java.util.ArrayList)5 CodeList (org.openforis.idm.metamodel.CodeList)5 CodeListItem (org.openforis.idm.metamodel.CodeListItem)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 Entity (org.openforis.idm.model.Entity)5 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)4 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)4 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)4 SurveyContext (org.openforis.idm.metamodel.SurveyContext)4 Attribute (org.openforis.idm.model.Attribute)4 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)4 Node (org.openforis.idm.model.Node)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 CollectRecord (org.openforis.collect.model.CollectRecord)3 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3