Search in sources :

Example 1 with AttributeDefault

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

the class CalculatedAttributeTest method createTestSurvey.

/**
 * Creates a test survey in which there is a bill with a list of items.
 * For each item there is a price, a quantity and a total
 * (calculated using the an expression or a constant).
 *
 * @return
 */
private Survey createTestSurvey() {
    SurveyContext surveyContext = new TestSurveyContext();
    Survey survey = surveyContext.createSurvey();
    Schema schema = survey.getSchema();
    EntityDefinition root = schema.createEntityDefinition();
    root.setName("bill");
    schema.addRootEntityDefinition(root);
    EntityDefinition item = schema.createEntityDefinition();
    item.setName("item");
    root.addChildDefinition(item);
    NumberAttributeDefinition qty = schema.createNumberAttributeDefinition();
    qty.setType(Type.INTEGER);
    qty.setName("qty");
    item.addChildDefinition(qty);
    NumberAttributeDefinition price = schema.createNumberAttributeDefinition();
    price.setName("price");
    item.addChildDefinition(price);
    NumberAttributeDefinition total = schema.createNumberAttributeDefinition();
    total.setName("total");
    total.setCalculated(true);
    total.addAttributeDefault(new AttributeDefault("qty * (price - (price * discount_percent div 100))"));
    item.addChildDefinition(total);
    TimeAttributeDefinition time = schema.createTimeAttributeDefinition();
    time.setName("time");
    time.addAttributeDefault(new AttributeDefault("idm:currentTime()"));
    item.addChildDefinition(time);
    TimeAttributeDefinition timeAlias = schema.createTimeAttributeDefinition();
    timeAlias.setName("time_alias");
    timeAlias.setCalculated(true);
    timeAlias.addAttributeDefault(new AttributeDefault("time"));
    item.addChildDefinition(timeAlias);
    NumberAttributeDefinition discountPercent = schema.createNumberAttributeDefinition();
    discountPercent.setType(Type.INTEGER);
    discountPercent.setName("discount_percent");
    discountPercent.setCalculated(true);
    discountPercent.addAttributeDefault(new AttributeDefault("30", "qty > 50"));
    discountPercent.addAttributeDefault(new AttributeDefault("20", "qty > 20"));
    discountPercent.addAttributeDefault(new AttributeDefault("10", "qty > 10"));
    discountPercent.addAttributeDefault(new AttributeDefault("0", "true()"));
    item.addChildDefinition(discountPercent);
    return survey;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Survey(org.openforis.idm.metamodel.Survey) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) Schema(org.openforis.idm.metamodel.Schema) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 2 with AttributeDefault

use of org.openforis.idm.metamodel.AttributeDefault 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 3 with AttributeDefault

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

the class DefaultValueTest method createTestSurvey.

/**
 * Creates a test survey in which there is a bill with a list of items.
 * For each item there is a price, a quantity and a total
 * (calculated using the an expression or a constant).
 *
 * @return
 */
protected Survey createTestSurvey() {
    SurveyContext surveyContext = new TestSurveyContext();
    Survey survey = surveyContext.createSurvey();
    Schema schema = survey.getSchema();
    EntityDefinition root = schema.createEntityDefinition();
    root.setName("bill");
    schema.addRootEntityDefinition(root);
    EntityDefinition item = schema.createEntityDefinition();
    item.setName("item");
    root.addChildDefinition(item);
    NumberAttributeDefinition qty = schema.createNumberAttributeDefinition();
    qty.setType(Type.INTEGER);
    qty.setName("qty");
    item.addChildDefinition(qty);
    NumberAttributeDefinition price = schema.createNumberAttributeDefinition();
    price.setName("price");
    item.addChildDefinition(price);
    NumberAttributeDefinition total = schema.createNumberAttributeDefinition();
    total.setName("total");
    item.addChildDefinition(total);
    {
        AttributeDefault attributeDefault = new AttributeDefault();
        attributeDefault.setExpression("qty * price");
        attributeDefault.setCondition("price > 0");
        total.addAttributeDefault(attributeDefault);
    }
    {
        AttributeDefault attributeDefault = new AttributeDefault();
        attributeDefault.setValue("0");
        attributeDefault.setCondition("price = 0");
        total.addAttributeDefault(attributeDefault);
    }
    return survey;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 4 with AttributeDefault

use of org.openforis.idm.metamodel.AttributeDefault 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 AttributeDefault

use of org.openforis.idm.metamodel.AttributeDefault 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

AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)14 Test (org.junit.Test)4 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)4 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)4 Schema (org.openforis.idm.metamodel.Schema)3 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)2 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)2 CollectSurvey (org.openforis.collect.model.CollectSurvey)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 SurveyContext (org.openforis.idm.metamodel.SurveyContext)2 Check (org.openforis.idm.metamodel.validation.Check)2 CustomCheck (org.openforis.idm.metamodel.validation.CustomCheck)2 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)2 ArrayList (java.util.ArrayList)1 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)1 ComparisonCheck (org.openforis.idm.metamodel.validation.ComparisonCheck)1