Search in sources :

Example 1 with CodeAttributeDefinition

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

the class DatabaseExternalCodeListProvider method getItem.

@Override
public ExternalCodeListItem getItem(CodeAttribute attribute) {
    CodeAttributeDefinition defn = attribute.getDefinition();
    CodeList list = defn.getList();
    List<NameValueEntry> filters = new ArrayList<NameValueEntry>();
    addSurveyFilter(list, filters);
    CodeAttribute codeParent = attribute.getCodeParent();
    while (codeParent != null) {
        String colName = getLevelKeyColumnName(codeParent);
        String codeValue = getCodeValue(codeParent);
        filters.add(new NameValueEntry(colName, codeValue));
        codeParent = codeParent.getCodeParent();
    }
    String colName = getLevelKeyColumnName(attribute);
    String codeValue = getCodeValue(attribute);
    filters.add(new NameValueEntry(colName, codeValue));
    int level = defn.getLevelPosition();
    List<NameValueEntry> emptyNextLevelsFilters = createEmptyNextLevelFilters(list, level);
    filters.addAll(emptyNextLevelsFilters);
    Map<String, String> row = dynamicTableDao.loadRow(list.getLookupTable(), filters.toArray(new NameValueEntry[filters.size()]));
    if (row == null) {
        return null;
    } else {
        ExternalCodeListItem result = parseRow(row, list, level);
        return result;
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) ArrayList(java.util.ArrayList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 2 with CodeAttributeDefinition

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

the class Mondrian4SchemaGenerator method createDimensionLink.

private DimensionLink createDimensionLink(Dimension dimension, AttributeDefinition attrDef) {
    DimensionLink dimensionLink;
    if (attrDef instanceof CodeAttributeDefinition) {
        CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) attrDef;
        if (codeAttrDef.isMultiple()) {
            dimensionLink = new NoLink();
        } else if (codeAttrDef.getList().isExternal()) {
            dimensionLink = new FactLink();
        } else {
            dimensionLink = new ForeignKeyLink();
            DataTable dataTable = rdbSchema.getDataTable(attrDef.getParentEntityDefinition());
            CodeValueFKColumn fkColumn = dataTable.getForeignKeyCodeColumn((CodeAttributeDefinition) attrDef);
            ((ForeignKeyLink) dimensionLink).foreignKeyColumn = fkColumn.getName();
        }
    } else {
        dimensionLink = new FactLink();
    }
    dimensionLink.dimension = dimension.name;
    return dimensionLink;
}
Also used : NoLink(mondrian.olap.MondrianDef.NoLink) DataTable(org.openforis.collect.relational.model.DataTable) FactLink(mondrian.olap.MondrianDef.FactLink) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ForeignKeyLink(mondrian.olap.MondrianDef.ForeignKeyLink) CodeValueFKColumn(org.openforis.collect.relational.model.CodeValueFKColumn) DimensionLink(mondrian.olap.MondrianDef.DimensionLink)

Example 3 with CodeAttributeDefinition

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

the class CodeValueFKColumnValueExtractor method extractValue.

private Object extractValue(CodeAttribute valNode) {
    CodeListItem item = findCodeListItem(valNode);
    if (item == null) {
        String defaultCodeValue = column.getDefaultCodeValue();
        String codeValue = getCodeValue(valNode);
        if (defaultCodeValue == null || (codeValue != null && !codeValue.equals(defaultCodeValue))) {
            // no default code value specified or code list item not found (invalid code?)
            return null;
        } else {
            CodeAttributeDefinition definition = valNode.getDefinition();
            CodeList list = definition.getList();
            Record record = valNode.getRecord();
            ModelVersion version = record.getVersion();
            return getDefaultCodeItemId(list, version);
        }
    } else {
        return item.getId();
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 4 with CodeAttributeDefinition

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

the class DataService method findAssignableCodeListItems.

/**
 * Gets the code list items assignable to the specified attribute.
 *
 * @param parentEntityId
 * @param attrName
 * @return
 */
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attrName) {
    CollectRecord record = getActiveRecord();
    CollectSurvey survey = (CollectSurvey) record.getSurvey();
    UserGroup surveyUserGroup = survey.getUserGroup();
    User user = sessionManager.getLoggedUser();
    final UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyUserGroup, user);
    if (userInGroup == null) {
        throw new IllegalStateException(String.format("User %s not allowed to access survey %s", user.getUsername(), survey.getName()));
    }
    Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attrName);
    List<CodeListItem> items = codeListManager.loadValidItems(parent, def);
    List<CodeListItem> filteredItems = new ArrayList<CodeListItem>(items);
    // filter by user group qualifier (if any)
    UserGroup group = userGroupManager.loadById(userInGroup.getGroupId());
    String qualifierName = group.getQualifier1Name();
    String listHierarchicalLevelName = def.getList().isHierarchical() ? def.getHierarchicalLevel() : def.getListName();
    if (qualifierName != null && qualifierName.equals(listHierarchicalLevelName)) {
        CollectionUtils.filter(filteredItems, new Predicate<CodeListItem>() {

            public boolean evaluate(CodeListItem item) {
                return item.getCode().equals(group.getQualifier1Value());
            }
        });
    }
    List<CodeListItemProxy> result = CodeListItemProxy.fromList(filteredItems);
    List<Node<?>> selectedCodes = parent.getChildren(attrName);
    CodeListItemProxy.setSelectedItems(result, selectedCodes);
    return result;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) User(org.openforis.collect.model.User) UserInGroup(org.openforis.collect.model.UserInGroup) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) UserGroup(org.openforis.collect.model.UserGroup) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Example 5 with CodeAttributeDefinition

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

the class DataService method getCodeListItems.

/**
 * Gets the code list items assignable to the specified attribute and matching the specified codes.
 *
 * @param parentEntityId
 * @param attrName
 * @param codes
 * @return
 */
@Secured(USER)
public List<CodeListItemProxy> getCodeListItems(int parentEntityId, String attrName, String[] codes) {
    CollectRecord record = getActiveRecord();
    Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attrName);
    List<CodeListItem> items = codeListManager.loadValidItems(parent, def);
    List<CodeListItem> filteredItems = new ArrayList<CodeListItem>();
    if (codes != null && codes.length > 0) {
        // filter by specified codes
        for (CodeListItem item : items) {
            for (String code : codes) {
                if (item.getCode().equals(code)) {
                    filteredItems.add(item);
                }
            }
        }
    }
    List<CodeListItemProxy> result = CodeListItemProxy.fromList(filteredItems);
    return result;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ArrayList(java.util.ArrayList) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)48 CodeList (org.openforis.idm.metamodel.CodeList)21 CodeListItem (org.openforis.idm.metamodel.CodeListItem)19 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)13 ArrayList (java.util.ArrayList)12 CollectSurvey (org.openforis.collect.model.CollectSurvey)8 CodeListService (org.openforis.idm.metamodel.CodeListService)8 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)8 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)7 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)7 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)6 ModelVersion (org.openforis.idm.metamodel.ModelVersion)6 Code (org.openforis.idm.model.Code)6 Entity (org.openforis.idm.model.Entity)6 DataTable (org.openforis.collect.relational.model.DataTable)5 CodeAttribute (org.openforis.idm.model.CodeAttribute)5 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)4 CollectRecord (org.openforis.collect.model.CollectRecord)4 CodeTable (org.openforis.collect.relational.model.CodeTable)4 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)4