Search in sources :

Example 91 with AttributeDefinition

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;
    }
}
Also used : Survey(org.openforis.idm.metamodel.Survey) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectAnnotations(org.openforis.collect.metamodel.CollectAnnotations) Step(org.openforis.collect.model.CollectRecord.Step)

Example 92 with AttributeDefinition

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()));
    }
}
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 93 with AttributeDefinition

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);
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 94 with AttributeDefinition

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;
}
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 95 with AttributeDefinition

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;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectAnnotations(org.openforis.collect.metamodel.CollectAnnotations)

Aggregations

AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)107 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)47 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)37 ArrayList (java.util.ArrayList)33 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)27 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)21 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)20 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)18 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)18 CollectSurvey (org.openforis.collect.model.CollectSurvey)17 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)16 KeyAttributeDefinition (org.openforis.idm.metamodel.KeyAttributeDefinition)16 TextAttributeDefinition (org.openforis.idm.metamodel.TextAttributeDefinition)12 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)12 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)11 Schema (org.openforis.idm.metamodel.Schema)10 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)9 Entity (org.openforis.idm.model.Entity)9 Value (org.openforis.idm.model.Value)9 RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)8