Search in sources :

Example 1 with TextValue

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);
}
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 2 with TextValue

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());
}
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 3 with TextValue

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());
}
Also used : TextValue(org.openforis.idm.model.TextValue) Test(org.junit.Test)

Example 4 with TextValue

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());
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) CodeListService(org.openforis.idm.metamodel.CodeListService) Time(org.openforis.idm.model.Time) Code(org.openforis.idm.model.Code) Date(org.openforis.idm.model.Date) NumericRange(org.openforis.idm.model.NumericRange) CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Coordinate(org.openforis.idm.model.Coordinate) NumberValue(org.openforis.idm.model.NumberValue) TextValue(org.openforis.idm.model.TextValue) BooleanValue(org.openforis.idm.model.BooleanValue) File(org.openforis.idm.model.File) NumberFormat(java.text.NumberFormat)

Example 5 with TextValue

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

TextValue (org.openforis.idm.model.TextValue)5 Test (org.junit.Test)4 Attribute (org.openforis.idm.model.Attribute)3 Entity (org.openforis.idm.model.Entity)3 TextAttribute (org.openforis.idm.model.TextAttribute)3 NumberFormat (java.text.NumberFormat)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 CodeList (org.openforis.idm.metamodel.CodeList)1 CodeListService (org.openforis.idm.metamodel.CodeListService)1 BooleanValue (org.openforis.idm.model.BooleanValue)1 Code (org.openforis.idm.model.Code)1 Coordinate (org.openforis.idm.model.Coordinate)1 Date (org.openforis.idm.model.Date)1 File (org.openforis.idm.model.File)1 IntegerRange (org.openforis.idm.model.IntegerRange)1 NumberValue (org.openforis.idm.model.NumberValue)1 NumericRange (org.openforis.idm.model.NumericRange)1 Time (org.openforis.idm.model.Time)1