use of org.openforis.idm.model.TextValue 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.TextValue 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());
}
use of org.openforis.idm.model.TextValue in project collect by openforis.
the class RecordUpdaterTest method testThisVariableCanReturnNodes.
@Test
public void testThisVariableCanReturnNodes() {
record(rootEntityDef(entityDef("tree", attributeDef("tree_count").calculated("idm:position($this/parent())")).multiple()));
updater.addEntity(record.getRootEntity(), "tree");
Attribute<?, ?> treeCount = attributeByPath("/root/tree[1]/tree_count");
assertEquals(new TextValue("1"), treeCount.getValue());
}
use of org.openforis.idm.model.TextValue in project collect by openforis.
the class CSVValueFormatter method format.
public String format(AttributeDefinition defn, Value value) {
if (value == null) {
return "";
} else if (value instanceof BooleanValue) {
return ((BooleanValue) value).getValue().toString();
} else if (value instanceof Code) {
CodeListService codeListService = defn.getSurvey().getContext().getCodeListService();
CodeList list = ((CodeAttributeDefinition) defn).getList();
if (codeListService.hasQualifiableItems(list)) {
return String.format("%s: %s", ((Code) value).getCode(), ((Code) value).getQualifier());
} else {
return ((Code) value).getCode();
}
} else if (value instanceof Coordinate) {
return value.toString();
} else if (value instanceof Date) {
Date date = (Date) value;
return String.format("%d/%d/%d", ((Date) value).getDay(), ((Date) value).getMonth(), date.getYear());
} else if (value instanceof File) {
return ((File) value).getFilename();
} else if (value instanceof NumberValue) {
Number val = ((NumberValue<?>) value).getValue();
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
String formattedVal = numberFormat.format(val);
return formattedVal;
} else if (value instanceof NumericRange) {
Number from = ((NumericRange<?>) value).getFrom();
Number to = ((NumericRange<?>) value).getFrom();
String format;
if (value instanceof IntegerRange) {
format = "%d-%d";
} else {
format = "%f-%f";
}
String formattedValue = String.format(Locale.ENGLISH, format, from, to);
return formattedValue;
} else if (value instanceof TextValue) {
return ((TextValue) value).getValue();
} else if (value instanceof Time) {
Time time = (Time) value;
return String.format("%d:%d", time.getHour(), time.getMinute());
} else
throw new IllegalArgumentException("Unsupported attribute value type: " + value.getClass().getName());
}
use of org.openforis.idm.model.TextValue in project collect by openforis.
the class RecordUpdaterTest method testRemoveEntity.
@Test
public void testRemoveEntity() {
record(rootEntityDef(entityDef("time_study", attributeDef("start_time")).multiple().required()), entity("time_study", attribute("start_time", "start first")), entity("time_study", attribute("start_time", "start second")));
Entity timeStudy1 = entityByPath("/root/time_study[1]");
updater.deleteNode(timeStudy1);
Entity rootEntity = record.getRootEntity();
assertEquals(1, rootEntity.getCount("time_study"));
Entity timeStudy2 = (Entity) rootEntity.getChild("time_study", 0);
Attribute<?, ?> startTime2 = (Attribute<?, ?>) timeStudy2.getChild("start_time");
assertEquals(new TextValue("start second"), startTime2.getValue());
}
Aggregations