use of org.openforis.idm.metamodel.EntityDefinition in project collect by openforis.
the class DataCleansingChainIntegrationTest method testUpdateFieldChain.
@Test
public void testUpdateFieldChain() {
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");
TaxonAttributeDefinition speciesDef = (TaxonAttributeDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree/species");
query.setTitle("Find trees with unlisted species");
query.setEntityDefinition(treeDef);
query.setAttributeDefinition(speciesDef);
query.setConditions("species/@code = 'UNL'");
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.FIELD);
updateValue.setCondition("idm:blank(species/@code)");
updateValue.setFieldFixExpressions(Arrays.asList("'UNL'", "'Will not be applied'"));
step.addUpdateValue(updateValue);
}
{
DataCleansingStepValue updateValue = new DataCleansingStepValue();
updateValue.setUpdateType(UpdateType.FIELD);
updateValue.setFieldFixExpressions(Arrays.asList("'PIN'", "'Pinus Sp.'"));
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);
jobManager.start(job, false);
int finalCount = countResults(query);
assertEquals(0, finalCount);
}
use of org.openforis.idm.metamodel.EntityDefinition 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());
}
use of org.openforis.idm.metamodel.EntityDefinition in project collect by openforis.
the class QueryExecutorIntegrationTest method testSimpleQuery.
@Test
public void testSimpleQuery() {
// select region from tree where dbh > 20
DataQuery query = new DataQuery(survey);
EntityDefinition treeDef = (EntityDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree");
AttributeDefinition dbhDef = (AttributeDefinition) survey.getSchema().getDefinitionByPath("/cluster/plot/tree/dbh");
query.setEntityDefinition(treeDef);
query.setAttributeDefinition(dbhDef);
query.setConditions("dbh > 20");
final List<Node<?>> nodes = new ArrayList<Node<?>>();
DataQueryExecutorJob job = jobManager.createJob(DataQueryExecutorJob.class);
DataQueryExecutorJobInput input = new DataQueryExecutorJobInput(query, Step.ENTRY, new NodeProcessor() {
public void process(Node<?> node) {
nodes.add(node);
}
});
job.setInput(input);
jobManager.start(job, false);
assertFalse(nodes.isEmpty());
// first result
Node<?> node = nodes.get(0);
assertTrue(node instanceof Attribute);
CollectRecord record = (CollectRecord) node.getRecord();
assertEquals(Arrays.asList("10_117"), record.getRootEntityKeyValues());
}
use of org.openforis.idm.metamodel.EntityDefinition in project collect by openforis.
the class RelationalSchemaGenerator method addDataObjects.
/**
* Recursively creates and adds tables and columns
*
* @param rs
* @param parentTable
* @param defn
* @throws CollectRdbException
*/
private void addDataObjects(RelationalSchema rs, DataTable table, NodeDefinition defn, Path relativePath) throws CollectRdbException {
if (defn instanceof EntityDefinition) {
if (defn.isMultiple()) {
// Create table for multiple entity
table = createDataTable(rs, table, defn, relativePath);
rs.addTable(table);
} else {
// just keep a reference
rs.assignAncestorTable((EntityDefinition) defn);
}
// Add child tables and columns
EntityDefinition entityDefn = (EntityDefinition) defn;
for (NodeDefinition child : entityDefn.getChildDefinitions()) {
Path childPath;
if (defn.isMultiple()) {
childPath = Path.relative(child.getName());
} else {
childPath = relativePath.appendElement(child.getName());
}
addDataObjects(rs, table, child, childPath);
}
} else if (defn instanceof AttributeDefinition) {
AttributeDefinition attrDefn = (AttributeDefinition) defn;
CollectSurvey survey = (CollectSurvey) defn.getSurvey();
CollectAnnotations annotations = survey.getAnnotations();
// do not include if it's a calculated attribute and it has not to be included in data export
if (!attrDefn.isCalculated() || annotations.isIncludedInDataExport(defn)) {
if (defn.isMultiple()) {
// Create table for multiple attributes
table = createDataTable(rs, table, defn, relativePath);
rs.addTable(table);
relativePath = Path.relative(".");
}
// Add columns for attributes in entity tables or attribute tables
addDataColumns(rs, table, (AttributeDefinition) defn, relativePath);
}
}
}
use of org.openforis.idm.metamodel.EntityDefinition in project collect by openforis.
the class RelationalSchemaGenerator method addDataTables.
private void addDataTables(RelationalSchema rs) throws CollectRdbException {
Survey survey = rs.getSurvey();
Schema schema = survey.getSchema();
// Recursively create tables, columns and constraints
List<EntityDefinition> roots = schema.getRootEntityDefinitions();
for (EntityDefinition root : roots) {
Path relativePath = Path.relative(root.getName());
addDataObjects(rs, null, root, relativePath);
}
}
Aggregations