Search in sources :

Example 1 with TimeAttributeDefinition

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

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

the class NodeDefinitionProxy method fromList.

static List<NodeDefinitionProxy> fromList(EntityDefinitionProxy parent, List<? extends NodeDefinition> list) {
    List<NodeDefinitionProxy> proxies = new ArrayList<NodeDefinitionProxy>();
    if (list != null) {
        for (NodeDefinition n : list) {
            NodeDefinitionProxy p = null;
            CollectSurvey survey = (CollectSurvey) n.getSurvey();
            UIOptions uiOptions = survey.getUIOptions();
            boolean hidden = uiOptions.isHidden(n);
            if (!hidden) {
                if (n instanceof AttributeDefinition) {
                    if (n instanceof BooleanAttributeDefinition) {
                        p = new BooleanAttributeDefinitionProxy(parent, (BooleanAttributeDefinition) n);
                    } else if (n instanceof CodeAttributeDefinition) {
                        p = new CodeAttributeDefinitionProxy(parent, (CodeAttributeDefinition) n);
                    } else if (n instanceof CoordinateAttributeDefinition) {
                        p = new CoordinateAttributeDefinitionProxy(parent, (CoordinateAttributeDefinition) n);
                    } else if (n instanceof DateAttributeDefinition) {
                        p = new DateAttributeDefinitionProxy(parent, (DateAttributeDefinition) n);
                    } else if (n instanceof FileAttributeDefinition) {
                        p = new FileAttributeDefinitionProxy(parent, (FileAttributeDefinition) n);
                    } else if (n instanceof NumberAttributeDefinition) {
                        p = new NumberAttributeDefinitionProxy(parent, (NumberAttributeDefinition) n);
                    } else if (n instanceof RangeAttributeDefinition) {
                        p = new RangeAttributeDefinitionProxy(parent, (RangeAttributeDefinition) n);
                    } else if (n instanceof TaxonAttributeDefinition) {
                        p = new TaxonAttributeDefinitionProxy(parent, (TaxonAttributeDefinition) n);
                    } else if (n instanceof TextAttributeDefinition) {
                        p = new TextAttributeDefinitionProxy(parent, (TextAttributeDefinition) n);
                    } else if (n instanceof TimeAttributeDefinition) {
                        p = new TimeAttributeDefinitionProxy(parent, (TimeAttributeDefinition) n);
                    } else {
                        throw new RuntimeException("AttributeDefinition not supported: " + n.getClass().getSimpleName());
                    }
                } else if (n instanceof EntityDefinition) {
                    p = new EntityDefinitionProxy(parent, (EntityDefinition) n);
                }
                if (p != null) {
                    proxies.add(p);
                }
            }
        }
    }
    return proxies;
}
Also used : TaxonAttributeDefinition(org.openforis.idm.metamodel.TaxonAttributeDefinition) UIOptions(org.openforis.collect.metamodel.ui.UIOptions) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) FileAttributeDefinition(org.openforis.idm.metamodel.FileAttributeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) TextAttributeDefinition(org.openforis.idm.metamodel.TextAttributeDefinition) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) TaxonAttributeDefinition(org.openforis.idm.metamodel.TaxonAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) FileAttributeDefinition(org.openforis.idm.metamodel.FileAttributeDefinition) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) TextAttributeDefinition(org.openforis.idm.metamodel.TextAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition)

Example 3 with TimeAttributeDefinition

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

the class Mondrian4SchemaGenerator method createDimensionAttributes.

private List<Attribute> createDimensionAttributes(DataTable dataTable, AttributeDefinition attrDefn) {
    List<Attribute> attributes = new ArrayList<Attribute>();
    if (attrDefn instanceof CodeAttributeDefinition && !((CodeAttributeDefinition) attrDefn).getList().isExternal()) {
        CodeAttributeDefinition codeAttrDefn = (CodeAttributeDefinition) attrDefn;
        CodeTable codeListTable = rdbSchema.getCodeListTable(codeAttrDefn);
        String codeListTableName = codeListTable.getName();
        Attribute attribute = new Attribute();
        FieldDefinition<String> codeFieldDef = codeAttrDefn.getCodeFieldDefinition();
        attribute.name = codeFieldDef.getName();
        attribute.caption = getAttributeCaption(codeFieldDef);
        attribute.table = codeListTableName;
        attribute.keyColumn = CodeListTables.getCodeColumnName(rdbConfig, codeListTableName);
        attribute.nameColumn = CodeListTables.getLabelColumnName(rdbConfig, codeListTableName);
        attributes.add(attribute);
    } else if (attrDefn.hasMainField()) {
        attributes.addAll(createAttributesForFields(dataTable, attrDefn));
    } else if (attrDefn instanceof DateAttributeDefinition || attrDefn instanceof TimeAttributeDefinition) {
        List<DataColumn> dataColumns = dataTable.getDataColumns(attrDefn);
        DataColumn dataColumn = dataColumns.get(0);
        Attribute attribute = new Attribute();
        attribute.name = attrDefn.getName();
        attribute.caption = getDimensionCaption(attrDefn);
        attribute.keyColumn = dataColumn.getName();
        attributes.add(attribute);
        attributes.addAll(createAttributesForFields(dataTable, attrDefn));
    } else {
    // TODO
    // every field makes the Key of the Attribute ?! then nameColumn must be specified
    // Attribute attribute = new Attribute();
    // attribute.name = attrDefn.getName();
    // attribute.caption = getDimensionCaption(attrDefn);
    // Key key = new Key();
    // for (FieldDefinition<?> fieldDef : attrDefn.getFieldDefinitions()) {
    // DataColumn dataColumn = dataTable.getDataColumn(fieldDef);
    // if (dataColumn != null) {
    // Column column = new Column();
    // column.name = dataColumn.getName();
    // key.list().add(column);
    // }
    // }
    // attribute.children.add(key);
    // Name name = new Name();
    // name.list().add(e)
    // attribute.children.add(name);
    // attributes.add(attribute);
    }
    return attributes;
}
Also used : CodeTable(org.openforis.collect.relational.model.CodeTable) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) Attribute(mondrian.olap.MondrianDef.Attribute) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ArrayList(java.util.ArrayList) DataColumn(org.openforis.collect.relational.model.DataColumn) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition)

Example 4 with TimeAttributeDefinition

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

the class NewMondrianSchemaGenerator method generateDimension.

private Dimension generateDimension(Cube cube, AttributeDefinition attrDef) {
    Dimension dimension = new Dimension(determineDimensionName(attrDef));
    EntityDefinition ancestorMultipleEntity = attrDef.getNearestAncestorMultipleEntity();
    if (ancestorMultipleEntity.isRoot()) {
        dimension.caption = extractLabel(attrDef);
    } else {
        dimension.caption = String.format("%s %s", extractLabel(ancestorMultipleEntity), extractLabel(attrDef));
    }
    Hierarchy hierarchy = dimension.hierarchy;
    hierarchy.table = new Table(dbSchemaName, cube.tables.get(0).name);
    if (attrDef instanceof CodeAttributeDefinition) {
        CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) attrDef;
        if (!codeAttrDef.getList().isExternal()) {
            if (attrDef.isMultiple()) {
                DataTable dataTable = rdbSchema.getDataTable(attrDef);
                String parentEntityPKColumnName = getEntityPKColumnName(ancestorMultipleEntity);
                dimension.foreignKey = parentEntityPKColumnName;
                hierarchy.table = null;
                hierarchy.primaryKey = parentEntityPKColumnName;
                hierarchy.primaryKeyTable = dataTable.getName();
                Join join = new Join(null);
                String codeListTableName = CodeListTables.getTableName(rdbConfig, codeAttrDef);
                join.leftKey = dataTable.getForeignKeyCodeColumn(codeAttrDef).getName();
                join.rightKey = CodeListTables.getIdColumnName(rdbConfig, codeListTableName);
                join.tables = Arrays.asList(new Table(dbSchemaName, dataTable.getName()), new Table(dbSchemaName, codeListTableName));
                hierarchy.join = join;
            } else {
                String codeListTableName = CodeListTables.getTableName(rdbConfig, codeAttrDef);
                hierarchy.table = new Table(dbSchemaName, codeListTableName);
                DataTable dataTable = rdbSchema.getDataTable(attrDef.getParentEntityDefinition());
                CodeValueFKColumn foreignKeyCodeColumn = dataTable.getForeignKeyCodeColumn(codeAttrDef);
                dimension.foreignKey = foreignKeyCodeColumn.getName();
            }
        }
        hierarchy.levels.add(generateLevel(attrDef));
    } else if (attrDef instanceof CoordinateAttributeDefinition) {
        dimension.type = "";
        hierarchy.type = "StandardDimension";
        {
            String fieldName = CoordinateAttributeDefinition.Y_FIELD_NAME;
            Level level = new Level(dimension.name + "_" + fieldName, extractLabel(attrDef) + " - Latitude");
            level.column = attrDef.getName() + "_" + fieldName;
            hierarchy.levels.add(level);
        }
        {
            String fieldName = CoordinateAttributeDefinition.X_FIELD_NAME;
            Level level = new Level(dimension.name + "_" + fieldName, extractLabel(attrDef) + " - Longitude");
            level.column = attrDef.getName() + "_" + fieldName;
            hierarchy.levels.add(level);
        }
    } else if (attrDef instanceof DateAttributeDefinition) {
        dimension.type = "";
        hierarchy.type = "TimeDimension";
        hierarchy.allMemberName = "attrLabel";
        String[] fieldNames = new String[] { DateAttributeDefinition.YEAR_FIELD_NAME, DateAttributeDefinition.MONTH_FIELD_NAME, DateAttributeDefinition.DAY_FIELD_NAME };
        for (String fieldName : fieldNames) {
            String fieldLabel = StringUtils.capitalize(fieldName);
            Level level = new Level(dimension.name + "_" + fieldName, determineLevelCaption(attrDef, fieldLabel));
            level.column = attrDef.getName() + "_" + fieldName.toLowerCase(Locale.ENGLISH);
            level.levelType = String.format("Time%ss", fieldLabel);
            level.type = NUMERIC_DATATYPE;
            hierarchy.levels.add(level);
        }
    } else if (attrDef instanceof TaxonAttributeDefinition) {
        {
            String fieldName = TaxonAttributeDefinition.CODE_FIELD_NAME;
            Level level = new Level(dimension.name + "_" + fieldName, extractLabel(attrDef) + " - Code");
            level.column = attrDef.getName() + "_" + fieldName;
            hierarchy.levels.add(level);
        }
        {
            String fieldName = TaxonAttributeDefinition.SCIENTIFIC_NAME_FIELD_NAME;
            Level level = new Level(dimension.name + "_" + fieldName, extractLabel(attrDef) + " - Scientific name");
            level.column = attrDef.getName() + "_" + fieldName;
            hierarchy.levels.add(level);
        }
    } else if (attrDef instanceof TimeAttributeDefinition) {
        dimension.type = "";
        hierarchy.type = "TimeDimension";
        hierarchy.allMemberName = "attrLabel";
        String[] fieldNames = new String[] { TimeAttributeDefinition.HOUR_FIELD, TimeAttributeDefinition.MINUTE_FIELD };
        for (String fieldName : fieldNames) {
            String fieldLabel = StringUtils.capitalize(fieldName);
            Level level = new Level(dimension.name + "_" + fieldName, determineLevelCaption(attrDef, fieldLabel));
            level.column = attrDef.getName() + "_" + fieldName.toLowerCase(Locale.ENGLISH);
            level.levelType = String.format("Time%ss", fieldLabel);
            level.type = NUMERIC_DATATYPE;
            hierarchy.levels.add(level);
        }
    } else {
        hierarchy.levels.add(generateLevel(attrDef));
    }
    return dimension;
}
Also used : DataTable(org.openforis.collect.relational.model.DataTable) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) CodeTable(org.openforis.collect.relational.model.CodeTable) DataTable(org.openforis.collect.relational.model.DataTable) TaxonAttributeDefinition(org.openforis.idm.metamodel.TaxonAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) CodeValueFKColumn(org.openforis.collect.relational.model.CodeValueFKColumn) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Aggregations

TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)4 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3 ArrayList (java.util.ArrayList)2 CodeTable (org.openforis.collect.relational.model.CodeTable)2 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)2 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)2 TaxonAttributeDefinition (org.openforis.idm.metamodel.TaxonAttributeDefinition)2 Attribute (mondrian.olap.MondrianDef.Attribute)1 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CodeValueFKColumn (org.openforis.collect.relational.model.CodeValueFKColumn)1 DataColumn (org.openforis.collect.relational.model.DataColumn)1 DataTable (org.openforis.collect.relational.model.DataTable)1 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)1 FileAttributeDefinition (org.openforis.idm.metamodel.FileAttributeDefinition)1 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)1