Search in sources :

Example 11 with AttributeDefault

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

the class RecordUpdater method recalculateValue.

private Value recalculateValue(Attribute<?, ?> attribute) {
    try {
        AttributeDefinition defn = attribute.getDefinition();
        List<AttributeDefault> attributeDefaults = defn.getAttributeDefaults();
        for (AttributeDefault attributeDefault : attributeDefaults) {
            if (attributeDefault.evaluateCondition(attribute)) {
                Value value = attributeDefault.evaluate(attribute);
                return value;
            }
        }
        return null;
    } catch (InvalidExpressionException e) {
        throw new IllegalStateException(String.format("Invalid expression for calculated attribute %s", attribute.getPath()));
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 12 with AttributeDefault

use of org.openforis.idm.metamodel.AttributeDefault 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)

Example 13 with AttributeDefault

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

the class SurveyValidator method validateAttributeDefaults.

protected List<SurveyValidationResult> validateAttributeDefaults(AttributeDefinition node) {
    List<SurveyValidationResult> results = new ArrayList<SurveyValidationResult>();
    List<AttributeDefault> attributeDefaults = node.getAttributeDefaults();
    for (AttributeDefault attributeDefault : attributeDefaults) {
        validateAttributeDefault(results, node, attributeDefault);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault)

Example 14 with AttributeDefault

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

the class DependencyGraphTest method attributeDefinition.

protected AttributeDefinition attributeDefinition(EntityDefinition parent, String name, String calculatedValueExpression, String relevantExpression, String validationExpression) {
    Schema schema = survey.getSchema();
    AttributeDefinition defn = schema.createTextAttributeDefinition();
    defn.setName(name);
    if (calculatedValueExpression != null) {
        defn.setCalculated(true);
        defn.addAttributeDefault(new AttributeDefault(calculatedValueExpression));
    }
    defn.setRelevantExpression(relevantExpression);
    if (validationExpression != null) {
        defn.addCheck(new CustomCheck(validationExpression));
    }
    parent.addChildDefinition(defn);
    survey.refreshSurveyDependencies();
    return defn;
}
Also used : CustomCheck(org.openforis.idm.metamodel.validation.CustomCheck) Schema(org.openforis.idm.metamodel.Schema) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Aggregations

AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)14 Test (org.junit.Test)4 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)4 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)4 Schema (org.openforis.idm.metamodel.Schema)3 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)2 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)2 CollectSurvey (org.openforis.collect.model.CollectSurvey)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 SurveyContext (org.openforis.idm.metamodel.SurveyContext)2 Check (org.openforis.idm.metamodel.validation.Check)2 CustomCheck (org.openforis.idm.metamodel.validation.CustomCheck)2 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)2 ArrayList (java.util.ArrayList)1 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)1 ComparisonCheck (org.openforis.idm.metamodel.validation.ComparisonCheck)1