Search in sources :

Example 6 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.

the class MinCountValidatorTest method updateMinCount.

// private boolean containsMinCountError(List<ValidationResult> errors, String name) {
// for (ValidationResult result : errors) {
// ValidationRule<?> validator = result.getValidator();
// if (validator instanceof MinCountValidator) {
// MinCountValidator v = (MinCountValidator) validator;
// NodeDefinition nodeDefinition = v.getNodeDefinition();
// if (nodeDefinition.getName().equals(name)) {
// return true;
// }
// }
// }
// return false;
// }
private void updateMinCount(Entity entity, String childName) {
    NodeDefinition childDef = entity.getDefinition().getChildDefinition(childName);
    try {
        String expr = childDef.getMinCountExpression();
        int count;
        if (StringUtils.isNotBlank(expr)) {
            Number val = expressionEvaluator.evaluateNumericValue(entity, null, expr);
            count = val.intValue();
        } else {
            count = 0;
        }
        entity.setMinCount(childDef, count);
    } catch (InvalidExpressionException e) {
        throw new RuntimeException(e);
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition)

Example 7 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.

the class RecordUpdater method calculateMaxCount.

private int calculateMaxCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMaxCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return defn.isMultiple() ? Integer.MAX_VALUE : 1;
    }
    if (defn.getFixedMaxCount() != null) {
        return defn.getFixedMaxCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? Integer.MAX_VALUE : 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 8 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException 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 9 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.

the class CSVDataExportProcess method exportData.

// private String calculateOutputFileName() {
// return "data.zip";
// /*
// StringBuilder sb = new StringBuilder();
// sb.append(survey.getName());
// sb.append("_");
// sb.append(rootEntityName);
// sb.append("_");
// sb.append("csv_data");
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// String today = formatter.format(new Date());
// sb.append(today);
// sb.append(".zip");
// return sb.toString();
// */
// }
private void exportData(OutputStream outputStream, int entityDefId) throws InvalidExpressionException, IOException, RecordPersistenceException {
    Writer outputWriter = new OutputStreamWriter(outputStream, OpenForisIOUtils.UTF_8);
    CSVDataExportColumnProviderGenerator csvDataExportColumnProviderGenerator = new CSVDataExportColumnProviderGenerator(recordFilter.getSurvey(), configuration);
    DataTransformation transform = csvDataExportColumnProviderGenerator.generateDataTransformation(entityDefId);
    @SuppressWarnings("resource") ModelCsvWriter // closing modelWriter will close referenced output stream
    modelWriter = new ModelCsvWriter(outputWriter, transform);
    modelWriter.printColumnHeadings();
    CollectSurvey survey = recordFilter.getSurvey();
    Step step = recordFilter.getStepGreaterOrEqual();
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(recordFilter);
    for (CollectRecordSummary s : summaries) {
        if (status.isRunning()) {
            try {
                CollectRecord record = recordManager.load(survey, s.getId(), step, false);
                modelWriter.printData(record);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            status.incrementProcessed();
        } else {
            break;
        }
    }
    modelWriter.flush();
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) DataTransformation(org.openforis.collect.io.data.csv.DataTransformation) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) OutputStreamWriter(java.io.OutputStreamWriter) Step(org.openforis.collect.model.CollectRecord.Step) CollectSurvey(org.openforis.collect.model.CollectSurvey) ModelCsvWriter(org.openforis.collect.io.data.csv.ModelCsvWriter) ModelCsvWriter(org.openforis.collect.io.data.csv.ModelCsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) RecordPersistenceException(org.openforis.collect.persistence.RecordPersistenceException) IOException(java.io.IOException)

Example 10 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException 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)

Aggregations

InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)16 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)10 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)6 SurveyContext (org.openforis.idm.metamodel.SurveyContext)6 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3 Node (org.openforis.idm.model.Node)3 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 Record (org.openforis.idm.model.Record)2 Value (org.openforis.idm.model.Value)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 DataTransformation (org.openforis.collect.io.data.csv.DataTransformation)1 ModelCsvWriter (org.openforis.collect.io.data.csv.ModelCsvWriter)1 CollectRecord (org.openforis.collect.model.CollectRecord)1