Search in sources :

Example 1 with NumberAttributeDefinition

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

the class DataCleansingChainIntegrationTest method testSimpleChain.

@Test
public void testSimpleChain() {
    DataCleansingChain chain = new DataCleansingChain(survey);
    chain.setTitle("Test chain");
    chain.setDescription("This is just a test");
    DataQuery query = new DataQuery(survey);
    EntityDefinition treeDef = (EntityDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree");
    NumberAttributeDefinition dbhDef = (NumberAttributeDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree/dbh");
    query.setTitle("Find trees with invalid DBH");
    query.setEntityDefinition(treeDef);
    query.setAttributeDefinition(dbhDef);
    query.setConditions("dbh > 20");
    dataQueryManager.save(query, user);
    int initialCount = countResults(query);
    assertEquals(1, initialCount);
    DataCleansingStep step = new DataCleansingStep(survey);
    step.setTitle("Step 1");
    step.setDescription("This is the step 1");
    step.setQuery(query);
    DataCleansingStepValue updateValue = new DataCleansingStepValue();
    updateValue.setUpdateType(UpdateType.ATTRIBUTE);
    // set dbh = 20
    updateValue.setFixExpression("20");
    step.addUpdateValue(updateValue);
    stepManager.save(step, user);
    chain.addStep(step);
    chainManager.save(chain, user);
    DataCleansingChainExecutorJob job = jobManager.createJob(DataCleansingChainExecutorJob.class);
    job.setSurvey(survey);
    job.setChain(chain);
    job.setRecordStep(Step.ENTRY);
    job.setActiveUser(user);
    jobManager.start(job, false);
    int finalCount = countResults(query);
    assertEquals(0, finalCount);
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 2 with NumberAttributeDefinition

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

the class DataReportGeneratorIntegrationTest method testSimpleErrorReport.

@Test
public void testSimpleErrorReport() {
    EntityDefinition treeDef = (EntityDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree");
    NumberAttributeDefinition dbhDef = (NumberAttributeDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree/dbh");
    DataQuery query = dataQuery().title("Find trees with invalid DBH").entity(treeDef).attribute(dbhDef).conditions("dbh > 20").type(invalidAttributeErrorType).severity(ErrorSeverity.ERROR).build();
    dataQueryManager.save(query, adminUser);
    DataQueryGroup queryGroup = new DataQueryGroup(survey);
    queryGroup.setTitle("Simple query group");
    queryGroup.addQuery(query);
    dataQueryGroupManager.save(queryGroup, adminUser);
    DataReportGeneratorJob job = jobManager.createJob(DataReportGeneratorJob.class);
    job.setQueryGroup(queryGroup);
    job.setRecordStep(Step.ENTRY);
    job.setActiveUser(adminUser);
    jobManager.start(job, false);
    DataReport report = job.getReport();
    DataReport reloadedReport = dataReportManager.loadById(survey, report.getId());
    List<DataReportItem> items = dataReportManager.loadItems(reloadedReport, 0, 100);
    assertFalse(items.isEmpty());
    assertEquals(1, items.size());
    DataReportItem item = items.get(0);
    CollectRecord record = recordManager.load(survey, item.getRecordId());
    assertEquals(Arrays.asList("10_117"), record.getRootEntityKeyValues());
    assertEquals(new RealValue(30.0d, dbhDef.getDefaultUnit()), item.extractAttributeValue());
}
Also used : RealValue(org.openforis.idm.model.RealValue) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CollectRecord(org.openforis.collect.model.CollectRecord) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) Test(org.junit.Test)

Example 3 with NumberAttributeDefinition

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

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

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

the class AttributeValueUtils method extractNormalizedValue.

private static Object extractNormalizedValue(NumberAttribute<?, ?> attr) {
    NumberAttributeDefinition defn = attr.getDefinition();
    List<Unit> units = defn.getUnits();
    if (units != null && units.size() > 1) {
        Unit unit = attr.getUnit();
        Unit defaultUnit = defn.getDefaultUnit();
        if (unit != null && defaultUnit != null) {
            double normalizedValue = getNormalizedValue(attr.getValue(), defaultUnit).doubleValue();
            return normalizedValue;
        }
    }
    NumberValue<?> value = attr.getValue();
    return value.getValue();
}
Also used : NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) Unit(org.openforis.idm.metamodel.Unit)

Aggregations

NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)10 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)7 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)3 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)2 Schema (org.openforis.idm.metamodel.Schema)2 Survey (org.openforis.idm.metamodel.Survey)2 SurveyContext (org.openforis.idm.metamodel.SurveyContext)2 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)2 Unit (org.openforis.idm.metamodel.Unit)2 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)1 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)1 CollectRecord (org.openforis.collect.model.CollectRecord)1