Search in sources :

Example 16 with InvalidExpressionException

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

the class RecordUpdater method performDefaultValueApply.

/**
 * Applies the first default value (if any) that is applicable to the attribute.
 * The condition of the corresponding DefaultValue will be verified.
 *
 * @param attribute
 * @return
 */
private <V extends Value> V performDefaultValueApply(Attribute<?, V> attribute) {
    AttributeDefinition attributeDefn = (AttributeDefinition) attribute.getDefinition();
    List<AttributeDefault> defaults = attributeDefn.getAttributeDefaults();
    for (AttributeDefault attributeDefault : defaults) {
        try {
            if (attributeDefault.evaluateCondition(attribute)) {
                V value = attributeDefault.evaluate(attribute);
                if (value != null) {
                    attribute.setValue(value);
                    setDefaultValueApplied(attribute, true);
                    attribute.updateSummaryInfo();
                    return value;
                }
            }
        } catch (InvalidExpressionException e) {
            throw new RuntimeException("Error applying default value for attribute " + attributeDefn.getPath());
        }
    }
    return null;
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

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