Search in sources :

Example 11 with CodeAttribute

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

the class RecordGenerator method addSecondLevelEntities.

private void addSecondLevelEntities(CollectRecord record, RecordKey recordKey) {
    CollectSurvey survey = (CollectSurvey) record.getSurvey();
    List<AttributeDefinition> nonMeasurementKeyDefs = getNonMeasurementKeyDefs(survey);
    List<String> keyValues = recordKey.getValues(nonMeasurementKeyDefs);
    List<SamplingDesignItem> secondLevelSamplingPointItems = samplingDesignManager.loadChildItems(survey.getId(), keyValues);
    List<CodeAttributeDefinition> samplingPointDataCodeAttributeDefs = findSamplingPointCodeAttributes(survey);
    if (!secondLevelSamplingPointItems.isEmpty() && samplingPointDataCodeAttributeDefs.size() > 1) {
        int levelIndex = 1;
        for (SamplingDesignItem samplingDesignItem : secondLevelSamplingPointItems) {
            CodeAttributeDefinition levelKeyDef = samplingPointDataCodeAttributeDefs.get(levelIndex);
            EntityDefinition levelEntityDef = levelKeyDef.getParentEntityDefinition();
            Entity parentLevelEntity = record.getRootEntity();
            NodeChangeSet addEntityChangeSet = recordUpdater.addEntity(parentLevelEntity, levelEntityDef);
            Entity entity = getAddedEntity(addEntityChangeSet);
            CodeAttribute keyAttr = entity.getChild(levelKeyDef);
            recordUpdater.updateAttribute(keyAttr, new Code(samplingDesignItem.getLevelCode(levelIndex + 1)));
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Code(org.openforis.idm.model.Code) SamplingDesignItem(org.openforis.collect.model.SamplingDesignItem) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 12 with CodeAttribute

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

the class CodeListManager method loadParentItem.

protected CodeListItem loadParentItem(CodeAttribute attribute) {
    CodeList list = attribute.getDefinition().getList();
    boolean persistedSurvey = list.getSurvey().getId() != null;
    Record record = attribute.getRecord();
    ModelVersion version = record.getVersion();
    if (persistedSurvey && list.isExternal()) {
        ExternalCodeListItem item = (ExternalCodeListItem) loadItemByAttribute(attribute);
        return provider.getParentItem(item);
    } else if (persistedSurvey && list.isEmpty()) {
        PersistedCodeListItem lastParentItem = null;
        List<CodeAttribute> codeAncestors = attribute.getCodeAncestors();
        for (int i = 0; i < codeAncestors.size(); i++) {
            CodeAttribute ancestor = codeAncestors.get(i);
            Integer lastParentItemId = lastParentItem == null ? null : lastParentItem.getSystemId();
            Code code = ancestor.getValue();
            lastParentItem = codeListItemDao.loadItem(list, lastParentItemId, code.getCode(), version);
            if (lastParentItem == null) {
                break;
            }
        }
        return lastParentItem;
    } else {
        CodeAttribute codeParent = attribute.getCodeParent();
        if (codeParent == null) {
            return null;
        } else {
            return loadItemByAttribute(codeParent);
        }
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CodeList(org.openforis.idm.metamodel.CodeList) List(java.util.List) ModelVersion(org.openforis.idm.metamodel.ModelVersion) Code(org.openforis.idm.model.Code) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 13 with CodeAttribute

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

the class CodeColumnProvider method extractValues.

@Override
public List<String> extractValues(Node<?> axis) {
    List<String> values = super.extractValues(axis);
    if (hasExpandedItems) {
        List<Node<?>> attributes = extractNodes(axis);
        for (CodeListItem item : expandedItems) {
            CodeAttribute attr = findAttributeByCode(attributes, item.getCode());
            values.add(Boolean.valueOf(attr != null).toString());
            if (item.isQualifiable()) {
                values.add(attr == null ? "" : attr.getValue().getQualifier());
            }
        }
    }
    return values;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Node(org.openforis.idm.model.Node) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 14 with CodeAttribute

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

the class CodeColumnProvider method findAttributeByCode.

protected CodeAttribute findAttributeByCode(List<Node<?>> attributes, String code) {
    for (Node<?> attr : attributes) {
        CodeAttribute codeAttr = (CodeAttribute) attr;
        Code val = codeAttr.getValue();
        if (val != null && code.equals(val.getCode())) {
            return codeAttr;
        }
    }
    return null;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Code(org.openforis.idm.model.Code)

Example 15 with CodeAttribute

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

the class SpeciesManager method extractQualifierValues.

protected String[] extractQualifierValues(TaxonAttribute attr) {
    TaxonAttributeDefinition defn = attr.getDefinition();
    List<String> qualifiers = defn.getQualifiers();
    String[] qualifierValues = null;
    if (qualifiers != null && !qualifiers.isEmpty()) {
        qualifierValues = new String[qualifiers.size()];
        Entity parent = attr.getParent();
        for (int i = 0; i < qualifiers.size(); i++) {
            String qualifierExpr = qualifiers.get(i);
            try {
                ModelPathExpression expression = expressionFactory.createModelPathExpression(qualifierExpr);
                CodeAttribute code = (CodeAttribute) expression.evaluate(parent, null);
                String qualifierValue = code.getValue().getCode();
                qualifierValues[i] = qualifierValue;
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn("Error evaluating taxon qualifiers: ", e);
                }
                break;
            }
        }
    }
    return qualifierValues;
}
Also used : Entity(org.openforis.idm.model.Entity) TaxonAttributeDefinition(org.openforis.idm.metamodel.TaxonAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) ModelPathExpression(org.openforis.idm.model.expression.ModelPathExpression)

Aggregations

CodeAttribute (org.openforis.idm.model.CodeAttribute)28 Code (org.openforis.idm.model.Code)16 Test (org.junit.Test)13 Entity (org.openforis.idm.model.Entity)12 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)7 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)5 CodeList (org.openforis.idm.metamodel.CodeList)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 ModelVersion (org.openforis.idm.metamodel.ModelVersion)5 CollectRecord (org.openforis.collect.model.CollectRecord)4 CodeListItem (org.openforis.idm.metamodel.CodeListItem)4 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)4 Record (org.openforis.idm.model.Record)4 ArrayList (java.util.ArrayList)3 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 AbstractTest (org.openforis.idm.AbstractTest)3 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)3 Node (org.openforis.idm.model.Node)3 List (java.util.List)2 CSVDataImportSettings (org.openforis.collect.io.data.csv.CSVDataImportSettings)2