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