Search in sources :

Example 11 with SurveyContext

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

the class UIOptionsTest method initSurvey.

protected void initSurvey() {
    SurveyContext ctx = new CollectSurveyContext(new ExpressionFactory(), new CollectValidator());
    survey = (CollectSurvey) ctx.createSurvey();
    schema = survey.getSchema();
    populateSchema();
    initUIOptions();
}
Also used : ExpressionFactory(org.openforis.idm.model.expression.ExpressionFactory) CollectSurveyContext(org.openforis.collect.model.CollectSurveyContext) CollectValidator(org.openforis.collect.model.validation.CollectValidator) CollectSurveyContext(org.openforis.collect.model.CollectSurveyContext) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 12 with SurveyContext

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

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

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

the class DependencyGraphTest method createTestSurvey.

private void createTestSurvey() {
    SurveyContext surveyContext = new TestSurveyContext();
    survey = surveyContext.createSurvey();
    Schema schema = survey.getSchema();
    rootEntityDef = schema.createEntityDefinition();
    rootEntityDef.setName(ROOT_ENTITY_NAME);
    schema.addRootEntityDefinition(rootEntityDef);
}
Also used : Schema(org.openforis.idm.metamodel.Schema) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 15 with SurveyContext

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

the class RecordUpdater method calculateMaxCount.

private int calculateMaxCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMaxCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return defn.isMultiple() ? Integer.MAX_VALUE : 1;
    }
    if (defn.getFixedMaxCount() != null) {
        return defn.getFixedMaxCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? Integer.MAX_VALUE : value.intValue();
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating required expression", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Aggregations

SurveyContext (org.openforis.idm.metamodel.SurveyContext)26 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)9 Survey (org.openforis.idm.metamodel.Survey)8 CodeListService (org.openforis.idm.metamodel.CodeListService)6 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)6 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 CollectSurvey (org.openforis.collect.model.CollectSurvey)4 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)4 Schema (org.openforis.idm.metamodel.Schema)4 Record (org.openforis.idm.model.Record)4 ArrayList (java.util.ArrayList)3 SurveyIdmlBinder (org.openforis.idm.metamodel.xml.SurveyIdmlBinder)3 Node (org.openforis.idm.model.Node)3 InputStream (java.io.InputStream)2 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 CodeListItem (org.openforis.idm.metamodel.CodeListItem)2 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2