Search in sources :

Example 16 with SurveyContext

use of org.openforis.idm.metamodel.SurveyContext in project collect by openforis.

the class RecordUpdater method calculateMinCount.

private int calculateMinCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMinCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return 0;
    }
    if (defn.getFixedMinCount() != null) {
        return defn.getFixedMinCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? 0 : value.intValue();
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating required expression", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 17 with SurveyContext

use of org.openforis.idm.metamodel.SurveyContext 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;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) Record(org.openforis.idm.model.Record) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 18 with SurveyContext

use of org.openforis.idm.metamodel.SurveyContext in project collect by openforis.

the class CodeColumnProvider method getCodeListService.

private CodeListService getCodeListService() {
    SurveyContext context = attributeDefinition.getSurvey().getContext();
    CodeListService codeListService = context.getCodeListService();
    return codeListService;
}
Also used : CodeListService(org.openforis.idm.metamodel.CodeListService) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 19 with SurveyContext

use of org.openforis.idm.metamodel.SurveyContext in project collect by openforis.

the class EnumerableEntityColumnProvider method createProviders.

private static List<ColumnProvider> createProviders(CSVDataExportParameters config, EntityDefinition defn) {
    List<ColumnProvider> providers = new ArrayList<ColumnProvider>();
    List<AttributeDefinition> keyDefs = defn.getKeyAttributeDefinitions();
    CodeAttributeDefinition keyDef = (CodeAttributeDefinition) keyDefs.get(0);
    CodeList codeList = keyDef.getList();
    SurveyContext context = defn.getSurvey().getContext();
    CodeListService codeListService = context.getCodeListService();
    List<CodeListItem> items = codeListService.loadRootItems(codeList);
    for (CodeListItem item : items) {
        String code = item.getCode();
        String keyName = keyDef.getName();
        EnumeratedCodeItemColumnProvider p = new EnumeratedCodeItemColumnProvider(config, defn, keyName, code);
        providers.add(p);
    }
    return providers;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) SurveyContext(org.openforis.idm.metamodel.SurveyContext) CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 20 with SurveyContext

use of org.openforis.idm.metamodel.SurveyContext in project collect by openforis.

the class ValidationMessageBuilder method getComparisonCheckMessageArg.

protected String getComparisonCheckMessageArg(Attribute<?, ?> attribute, String expression, Locale locale) {
    if (StringUtils.isNotBlank(expression)) {
        String result = expression;
        Survey survey = attribute.getSurvey();
        Schema schema = survey.getSchema();
        SurveyContext surveyContext = survey.getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        try {
            Entity parentEntity = attribute.getParent();
            EntityDefinition parentDefinition = parentEntity.getDefinition();
            Set<String> referencedPaths = expressionEvaluator.determineReferencedPaths(expression);
            for (String path : referencedPaths) {
                String absolutePath = parentDefinition.getPath() + PATH_SEPARATOR + path;
                NodeDefinition nodeDefinition = schema.getDefinitionByPath(absolutePath);
                String label = getPrettyLabelText(nodeDefinition, locale);
                result = result.replaceAll(nodeDefinition.getName(), label);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    } else {
        return expression;
    }
}
Also used : Entity(org.openforis.idm.model.Entity) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Aggregations

SurveyContext (org.openforis.idm.metamodel.SurveyContext)26 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)9 Survey (org.openforis.idm.metamodel.Survey)8 CodeListService (org.openforis.idm.metamodel.CodeListService)6 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)6 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 CollectSurvey (org.openforis.collect.model.CollectSurvey)4 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)4 Schema (org.openforis.idm.metamodel.Schema)4 Record (org.openforis.idm.model.Record)4 ArrayList (java.util.ArrayList)3 SurveyIdmlBinder (org.openforis.idm.metamodel.xml.SurveyIdmlBinder)3 Node (org.openforis.idm.model.Node)3 InputStream (java.io.InputStream)2 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 CodeListItem (org.openforis.idm.metamodel.CodeListItem)2 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2