Search in sources :

Example 36 with CodeAttributeDefinition

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

the class CodeListManager method loadItemByAttribute.

@SuppressWarnings("unchecked")
public <T extends CodeListItem> T loadItemByAttribute(CodeAttribute attribute) {
    CodeAttributeDefinition defn = attribute.getDefinition();
    CodeList list = defn.getList();
    boolean persistedSurvey = list.getSurvey().getId() != null;
    if (persistedSurvey && list.isExternal()) {
        return (T) provider.getItem(attribute);
    } else if (persistedSurvey && list.isEmpty()) {
        return (T) loadPersistedItem(attribute);
    } else {
        return (T) getInternalCodeListItem(attribute);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 37 with CodeAttributeDefinition

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

the class CollectEarthGridTemplateGenerator method validateCell.

/**
 * Checks if a value can be used as the input for an attribute
 * @param attributeDefinition The attribute that we want to check the value against
 * @param value The value that should be checked
 * @return True if the value can be used in the attribute. False otherwise (for instance trying to use a string "abc" as the input for a Number attribute
 */
private String validateCell(CollectSurvey survey, AttributeDefinition attributeDefinition, String value) {
    try {
        // By creating the value using hte attribute definitoon a validation is performed
        Value valueCreated = attributeDefinition.createValue(value);
        if (attributeDefinition.isAlwaysRequired() && StringUtils.isBlank(value)) {
            return String.format("The attribute %s is marekd as \"always required\". The value in the cell is empty!", attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LAT_COORDINATE)) {
            double lat = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (lat < -90 || lat > 90) {
                return "The latitude of a plot must be between -90 and 90 degrees!";
            }
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LONG_COORDINATE)) {
            double longitude = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (longitude < -180 || longitude > 180) {
                return "The latitude of a plot must be between -180 and 180 degrees!";
            }
        }
        // Make sure that the code used in a code-attribute is actually present on the codelist
        if (attributeDefinition instanceof CodeAttributeDefinition) {
            CodeAttributeDefinition cad = (CodeAttributeDefinition) attributeDefinition;
            // IF IT IS A STRICT CODE LIST
            if (!cad.isAllowUnlisted()) {
                // Check that the code exists in the codelist
                // Gets the level (in case of hierarchical codelists) that the attribute refers to. If it is a flat codelist then it is alway 0
                int levelIndex = cad.getLevelIndex();
                CodeListService codeListService = attributeDefinition.getSurvey().getContext().getCodeListService();
                List<CodeListItem> items = codeListService.loadItems(cad.getList(), levelIndex + 1);
                // Check one by one in the codes of the codelist assigned to the attribute if the value is present as a code!
                for (CodeListItem codeListItem : items) {
                    if (codeListItem.getCode().equals(value)) {
                        // FOUND! All good, return null
                        return null;
                    }
                }
                return String.format("The code with value \"%s\" is not part of the codelist used by code %s ", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
            }
        } else if (attributeDefinition instanceof CodeAttributeDefinition) {
        }
    } catch (Exception e) {
        return String.format("The value \"%s\" cannot be used as a value for the attribute %s", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
    }
    return null;
}
Also used : CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumberValue(org.openforis.idm.model.NumberValue) Value(org.openforis.idm.model.Value) CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem) IOException(java.io.IOException)

Example 38 with CodeAttributeDefinition

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

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

the class DataService method findAssignableCodeListItems.

/**
 * Finds a list of code list items assignable to the specified attribute and matching the passed codes
 *
 * @param parentEntityId
 * @param attributeName
 * @param codes
 * @return
 */
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attributeName, String[] codes) {
    CollectRecord record = getActiveRecord();
    Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attributeName);
    List<CodeListItem> items = codeListManager.findValidItems(parent, def, codes);
    List<CodeListItemProxy> result = new ArrayList<CodeListItemProxy>();
    for (CodeListItem item : items) {
        result.add(new CodeListItemProxy(item));
    }
    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)

Example 40 with CodeAttributeDefinition

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

the class CodeListController method loadAvailableItems.

@RequestMapping(value = "survey/{surveyId}/codelist/{codeListId}", method = GET)
@ResponseBody
public List<CodeListItemView> loadAvailableItems(@PathVariable Integer surveyId, @PathVariable Integer codeListId, @RequestParam Integer recordId, @RequestParam Integer parentEntityId, @RequestParam Integer codeAttrDefId) {
    CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
    CollectRecord record = recordManager.load(survey, recordId, false);
    Entity parentEntity = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) survey.getSchema().getDefinitionById(codeAttrDefId);
    List<CodeListItem> items = codeListManager.loadValidItems(parentEntity, codeAttrDef);
    return toViews(items);
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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