Search in sources :

Example 1 with NumericAttributeDefinition

use of org.openforis.idm.metamodel.NumericAttributeDefinition in project collect by openforis.

the class NumericAttributeDefinitionPR method onStartDefinition.

@Override
protected void onStartDefinition() throws XmlParseException, XmlPullParserException, IOException {
    super.onStartDefinition();
    String typeStr = getAttribute(TYPE, false);
    NumericAttributeDefinition defn = (NumericAttributeDefinition) getDefinition();
    try {
        Type type = typeStr == null ? Type.REAL : Type.valueOf(typeStr.toUpperCase());
        defn.setType(type);
    } catch (IllegalArgumentException e) {
        throw new XmlParseException(getParser(), "unknown type " + typeStr);
    }
}
Also used : Type(org.openforis.idm.metamodel.NumericAttributeDefinition.Type) XmlParseException(org.openforis.idm.metamodel.xml.XmlParseException) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 2 with NumericAttributeDefinition

use of org.openforis.idm.metamodel.NumericAttributeDefinition in project collect by openforis.

the class DefaultValueTest method addItem.

protected Entity addItem(Entity rootEntity, Integer qtyValue, Double priceValue) {
    EntityDefinition rootEntityDefn = rootEntity.getDefinition();
    EntityDefinition itemDefn = (EntityDefinition) rootEntityDefn.getChildDefinition("item");
    Entity item = (Entity) itemDefn.createNode();
    if (qtyValue != null) {
        NodeDefinition qtyDefn = itemDefn.getChildDefinition("qty");
        IntegerAttribute qty = (IntegerAttribute) qtyDefn.createNode();
        qty.setValue(new IntegerValue(qtyValue, null));
        qty.updateSummaryInfo();
        item.add(qty);
    }
    if (priceValue != null) {
        NumericAttributeDefinition priceDefn = (NumericAttributeDefinition) itemDefn.getChildDefinition("price");
        RealAttribute price = (RealAttribute) priceDefn.createNode();
        price.setValue(new RealValue(priceValue, null));
        price.updateSummaryInfo();
        item.add(price);
    }
    NumericAttributeDefinition totalDefn = (NumericAttributeDefinition) itemDefn.getChildDefinition("total");
    RealAttribute total = (RealAttribute) totalDefn.createNode();
    item.add(total);
    rootEntity.add(item);
    return item;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 3 with NumericAttributeDefinition

use of org.openforis.idm.metamodel.NumericAttributeDefinition in project collect by openforis.

the class DefaultValueTest method testConditionRespected.

@Test
public void testConditionRespected() throws InvalidExpressionException {
    NumericAttributeDefinition totalDefn = (NumericAttributeDefinition) schema.getDefinitionByPath("bill/item/total");
    List<AttributeDefault> attributeDefaults = totalDefn.getAttributeDefaults();
    Entity rootEntity = record.getRootEntity();
    Entity item = addItem(rootEntity, 10, 0d);
    RealAttribute total = (RealAttribute) item.getChild("total", 0);
    AttributeDefault constantAttributeDefault = attributeDefaults.get(1);
    RealValue calculatedTotal = constantAttributeDefault.evaluate(total);
    assertEquals(new RealValue(0d, null), calculatedTotal);
}
Also used : AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) Test(org.junit.Test)

Example 4 with NumericAttributeDefinition

use of org.openforis.idm.metamodel.NumericAttributeDefinition in project collect by openforis.

the class DefaultValueTest method testNullValue.

@Test
public void testNullValue() throws InvalidExpressionException {
    NumericAttributeDefinition totalDefn = (NumericAttributeDefinition) schema.getDefinitionByPath("bill/item/total");
    List<AttributeDefault> attributeDefaults = totalDefn.getAttributeDefaults();
    AttributeDefault exprAttributeDefault = attributeDefaults.get(0);
    Entity rootEntity = record.getRootEntity();
    Entity item = addItem(rootEntity, null, 5.5d);
    RealAttribute total = (RealAttribute) item.getChild("total", 0);
    RealValue calculatedTotal = exprAttributeDefault.evaluate(total);
    assertEquals(0d, calculatedTotal.getValue(), 0);
}
Also used : AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) Test(org.junit.Test)

Example 5 with NumericAttributeDefinition

use of org.openforis.idm.metamodel.NumericAttributeDefinition in project collect by openforis.

the class DefaultValueTest method testConditionNotRespected.

@Test
public void testConditionNotRespected() throws InvalidExpressionException {
    NumericAttributeDefinition totalDefn = (NumericAttributeDefinition) schema.getDefinitionByPath("bill/item/total");
    List<AttributeDefault> attributeDefaults = totalDefn.getAttributeDefaults();
    AttributeDefault exprAttributeDefault = attributeDefaults.get(0);
    Entity rootEntity = record.getRootEntity();
    Entity item = addItem(rootEntity, 10, 0d);
    RealAttribute total = (RealAttribute) item.getChild("total", 0);
    boolean condition = exprAttributeDefault.evaluateCondition(total);
    assertFalse(condition);
}
Also used : AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) Test(org.junit.Test)

Aggregations

NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)13 Test (org.junit.Test)4 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)4 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)4 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3 ArrayList (java.util.ArrayList)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 Schema (org.openforis.idm.metamodel.Schema)2 List (java.util.List)1 Stack (java.util.Stack)1 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)1 CodeTable (org.openforis.collect.relational.model.CodeTable)1 DataColumn (org.openforis.collect.relational.model.DataColumn)1 DataTable (org.openforis.collect.relational.model.DataTable)1 CodeListItem (org.openforis.idm.metamodel.CodeListItem)1 CodeListLevel (org.openforis.idm.metamodel.CodeListLevel)1 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)1