use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.
the class RecordUpdater method isDefaultValueToBeApplied.
private boolean isDefaultValueToBeApplied(Attribute<?, ?> attr) {
Survey survey = attr.getSurvey();
if (survey instanceof CollectSurvey) {
CollectAnnotations annotations = ((CollectSurvey) survey).getAnnotations();
Step recordStep = ((CollectRecord) attr.getRecord()).getStep();
AttributeDefinition def = attr.getDefinition();
Step stepToApplyDefaultValue = annotations.getPhaseToApplyDefaultValue(def);
return recordStep.compareTo(stepToApplyDefaultValue) >= 0;
} else {
return false;
}
}
use of org.openforis.idm.metamodel.AttributeDefinition 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.AttributeDefinition in project collect by openforis.
the class RecordUpdater method addAttribute.
/**
* Adds a new attribute to a record.
* This attribute can be immediately populated with a value or with a FieldSymbol, and remarks.
* You cannot specify both value and symbol.
*
* @param parentEntity Parent entity of the attribute
* @param attributeName Name of the attribute definition
* @param value Value to set on the attribute
* @param symbol FieldSymbol to set on each field of the attribute
* @param remarks Remarks to set on each field of the attribute
* @return Changes applied to the record
*/
public NodeChangeSet addAttribute(Entity parentEntity, String attributeName, Value value, FieldSymbol symbol, String remarks) {
EntityDefinition parentEntityDefn = parentEntity.getDefinition();
AttributeDefinition attributeDef = (AttributeDefinition) parentEntityDefn.getChildDefinition(attributeName);
return addAttribute(parentEntity, attributeDef, value, symbol, remarks);
}
use of org.openforis.idm.metamodel.AttributeDefinition 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.AttributeDefinition in project collect by openforis.
the class RecordUpdater method addEmptyChildren.
private int addEmptyChildren(Entity entity, NodeDefinition childDefn, int toBeInserted) {
CollectSurvey survey = (CollectSurvey) entity.getSurvey();
CollectAnnotations annotations = survey.getAnnotations();
int count = 0;
if (!childDefn.isMultiple() || annotations.isAutoGenerateMinItems(childDefn) || survey.getTarget() == SurveyTarget.COLLECT_EARTH) {
while (count < toBeInserted) {
if (childDefn instanceof AttributeDefinition) {
Node<?> createdNode = childDefn.createNode();
entity.add(createdNode);
} else if (childDefn instanceof EntityDefinition) {
Entity childEntity = performEntityAdd(entity, (EntityDefinition) childDefn);
addEmptyNodes(childEntity);
}
count++;
}
}
return count;
}
Aggregations