Search in sources :

Example 6 with Attribute

use of org.openforis.idm.model.Attribute in project collect by openforis.

the class NodeBuilder method createAttribute.

private Attribute<?, ?> createAttribute(Entity parent) {
    AttributeDefinition def = (AttributeDefinition) parent.getDefinition().getChildDefinition(name);
    @SuppressWarnings("unchecked") Attribute<?, Value> attr = (Attribute<?, Value>) def.createNode();
    if (value != null) {
        if (value instanceof Value) {
            attr.setValue((Value) value);
        } else {
            attr.setValue(def.<Value>createValue(value.toString()));
        }
        attr.updateSummaryInfo();
    }
    return attr;
}
Also used : Attribute(org.openforis.idm.model.Attribute) Value(org.openforis.idm.model.Value) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Example 7 with Attribute

use of org.openforis.idm.model.Attribute in project collect by openforis.

the class DataPersister method insertRow.

private int insertRow(CollectRecord record, Node<? extends NodeDefinition> node, Integer parentId, int idx) {
    Integer defnId = node.getDefinition().getId();
    if (defnId == null) {
        throw new IllegalArgumentException("Null schema object definition id");
    }
    int dataRowId = jooqFactory.nextval(DATA_ID_SEQ).intValue();
    InsertSetMoreStep<?> insert = jooqFactory.insertInto(DATA).set(DATA.ID, dataRowId).set(DATA.DEFINITION_ID, defnId).set(DATA.RECORD_ID, record.getId()).set(DATA.IDX, idx + 1).set(DATA.PARENT_ID, parentId);
    NodeDefinition defn = node.getDefinition();
    Class<? extends NodeDefinition> defnClass = defn.getClass();
    if (node instanceof Attribute) {
        Attribute<?, ?> attr = (Attribute<?, ?>) node;
        if (attr.getRemarks() != null) {
            insert.set(DATA.REMARKS, attr.getRemarks());
        }
        if (attr.getSymbol() != null) {
            insert.set(DATA.SYMBOL, attr.getSymbol().toString());
        }
    }
    NodeMapper mapper = NodeMapper.getInstance(defnClass);
    mapper.setFields(node, insert);
    insert.execute();
    return dataRowId;
}
Also used : Attribute(org.openforis.idm.model.Attribute) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition)

Example 8 with Attribute

use of org.openforis.idm.model.Attribute in project collect by openforis.

the class UniquenessCheck method evaluate.

@Override
public ValidationResultFlag evaluate(final Attribute<?, ?> attribute) {
    try {
        SurveyContext recordContext = attribute.getRecord().getSurveyContext();
        ExpressionEvaluator expressionEvaluator = recordContext.getExpressionEvaluator();
        Node<?> duplicateNode = expressionEvaluator.findNode(attribute.getParent(), attribute, expression, new Predicate<Node<?>>() {

            public boolean evaluate(Node<?> node) {
                if (node instanceof Attribute && node != attribute) {
                    Value value = ((Attribute<?, ?>) node).getValue();
                    if (value != null && value.equals(attribute.getValue())) {
                        return true;
                    }
                }
                return false;
            }
        });
        boolean unique = duplicateNode == null;
        return ValidationResultFlag.valueOf(unique, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating uniqueness check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) Attribute(org.openforis.idm.model.Attribute) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) Value(org.openforis.idm.model.Value) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 9 with Attribute

use of org.openforis.idm.model.Attribute in project collect by openforis.

the class RecordUpdaterTest method testRemoveEntityWithCalculatedAttribute.

@Test
public void testRemoveEntityWithCalculatedAttribute() {
    record(rootEntityDef(entityDef("plot_details", attributeDef("dbh_sum").calculated("sum(parent()/tree/dbh)"), attributeDef("tree_health").relevant("dbh_sum > 0").required()), entityDef("tree", attributeDef("dbh")).multiple()), entity("plot_details", attribute("dbh_sum"), attribute("tree_health")), entity("tree", attribute("dbh", "1")));
    Entity plotDetails = entityByPath("/root/plot_details");
    Entity tree1 = entityByPath("/root/tree[1]");
    NodeChangeSet changeSet = updater.deleteNode(tree1);
    Attribute<?, ?> dbhSum = (Attribute<?, ?>) plotDetails.getChild("dbh_sum");
    assertEquals(new TextValue("0.0"), dbhSum.getValue());
    NodeChange<?> dbhSumChange = changeSet.getChange(dbhSum);
    assertNotNull(dbhSumChange);
}
Also used : Entity(org.openforis.idm.model.Entity) Attribute(org.openforis.idm.model.Attribute) TextAttribute(org.openforis.idm.model.TextAttribute) TextValue(org.openforis.idm.model.TextValue) Test(org.junit.Test)

Example 10 with Attribute

use of org.openforis.idm.model.Attribute in project collect by openforis.

the class RecordUpdaterTest method testRemoveEntityUpdatesCalculatedPosition.

@Test
public void testRemoveEntityUpdatesCalculatedPosition() {
    record(rootEntityDef(entityDef("tree", attributeDef("tree_num").calculated("idm:position()")).multiple()), entity("tree"), entity("tree"), entity("tree"));
    Entity tree1 = entityByPath("/root/tree[1]");
    Entity tree2 = entityByPath("/root/tree[2]");
    Entity tree3 = entityByPath("/root/tree[3]");
    updater.deleteNode(tree2);
    Attribute<?, ?> treeNum1 = (Attribute<?, ?>) tree1.getChild("tree_num");
    assertEquals(new TextValue("1"), treeNum1.getValue());
    Attribute<?, ?> treeNum3 = (Attribute<?, ?>) tree3.getChild("tree_num");
    assertEquals(new TextValue("2"), treeNum3.getValue());
}
Also used : Entity(org.openforis.idm.model.Entity) Attribute(org.openforis.idm.model.Attribute) TextAttribute(org.openforis.idm.model.TextAttribute) TextValue(org.openforis.idm.model.TextValue) Test(org.junit.Test)

Aggregations

Attribute (org.openforis.idm.model.Attribute)23 ArrayList (java.util.ArrayList)8 Entity (org.openforis.idm.model.Entity)8 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)7 CodeAttribute (org.openforis.idm.model.CodeAttribute)7 Value (org.openforis.idm.model.Value)6 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)5 Node (org.openforis.idm.model.Node)5 Test (org.junit.Test)4 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)4 Record (org.openforis.idm.model.Record)4 TextAttribute (org.openforis.idm.model.TextAttribute)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 BooleanValue (org.openforis.idm.model.BooleanValue)3 NodePointer (org.openforis.idm.model.NodePointer)3 NodeVisitor (org.openforis.idm.model.NodeVisitor)3 TextValue (org.openforis.idm.model.TextValue)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 FileAttribute (org.openforis.idm.model.FileAttribute)2