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()));
}
}
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;
}
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;
}
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;
}
Aggregations