Search in sources :

Example 16 with CodeListService

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

the class SurveyViewGenerator method generateView.

public SurveyView generateView(final CollectSurvey survey) {
    final SurveyView surveyView = new SurveyView(survey);
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList codeList : codeLists) {
        CodeListView codeListView = new CodeListView();
        codeListView.setId(codeList.getId());
        codeListView.setName(codeList.getName());
        codeListView.setLabel(codeList.getLabel(CodeListLabel.Type.ITEM, languageCode));
        if (includeCodeListValues && !codeList.isExternal()) {
            CodeListService service = survey.getContext().getCodeListService();
            List<CodeListItem> items = service.loadRootItems(codeList);
            for (CodeListItem item : items) {
                codeListView.addItem(createCodeListItemView(item));
            }
        }
        surveyView.addCodeList(codeListView);
    }
    final Map<Integer, NodeDefView> viewById = new HashMap<Integer, NodeDefView>();
    survey.getSchema().traverse(new NodeDefinitionVisitor() {

        public void visit(NodeDefinition def) {
            int id = def.getId();
            String name = def.getName();
            String label = getLabel(def);
            NodeDefView view;
            if (def instanceof EntityDefinition) {
                view = new EntityDefView(((EntityDefinition) def).isRoot(), id, name, label, def.isMultiple());
            } else if (def instanceof CodeAttributeDefinition) {
                CodeAttributeDefinition attrDef = (CodeAttributeDefinition) def;
                int codeListId = attrDef.getList() == null ? -1 : attrDef.getList().getId();
                view = new CodeAttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef), codeListId);
            } else {
                AttributeDefinition attrDef = (AttributeDefinition) def;
                view = new AttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef));
            }
            NodeDefinition parentDef = def.getParentDefinition();
            if (parentDef == null) {
                surveyView.getSchema().addRootEntity((EntityDefView) view);
            } else {
                EntityDefView parentView = (EntityDefView) viewById.get(parentDef.getId());
                parentView.addChild(view);
            }
            viewById.put(id, view);
        }
    });
    return surveyView;
}
Also used : HashMap(java.util.HashMap) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor) CodeList(org.openforis.idm.metamodel.CodeList) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 17 with CodeListService

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

the class CodeValueFKColumnValueExtractor method getDefaultCodeItemId.

private Integer getDefaultCodeItemId(CodeList list, ModelVersion version) {
    CodeListService codeListService = getCodeListService((CollectSurvey) list.getSurvey());
    CodeListItem defaultCodeItem = codeListService.loadRootItem(list, column.getDefaultCodeValue(), version);
    return defaultCodeItem == null ? CodeTableDataExtractor.DEFAULT_CODE_ROW_ID : defaultCodeItem.getId();
}
Also used : CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 18 with CodeListService

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

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

the class CollectEarthBalloonGenerator method createEnumeratedEntityComponent.

private CEComponent createEnumeratedEntityComponent(EntityDefinition def) {
    String label = def.getLabel(Type.INSTANCE, language);
    if (label == null && !isDefaultLanguage()) {
        label = def.getLabel(Type.INSTANCE);
    }
    if (label == null) {
        label = def.getName();
    }
    UIOptions uiOptions = survey.getUIOptions();
    String tableTooltip = def.getDescription(language);
    CEEnumeratedEntityTable ceTable = new CEEnumeratedEntityTable(def.getName(), label, tableTooltip);
    for (NodeDefinition child : def.getChildDefinitions()) {
        if (!uiOptions.isHidden(child)) {
            String heading = child.getLabel(Type.INSTANCE, language);
            if (heading == null && !isDefaultLanguage()) {
                heading = child.getLabel(Type.INSTANCE);
            }
            if (heading == null) {
                heading = child.getName();
            }
            ceTable.addHeading(heading);
        }
    }
    CodeAttributeDefinition enumeratingCodeAttribute = def.getEnumeratingKeyCodeAttribute();
    CodeListService codeListService = def.getSurvey().getContext().getCodeListService();
    List<CodeListItem> codeItems = codeListService.loadRootItems(enumeratingCodeAttribute.getList());
    int codeItemIdx = 0;
    for (CodeListItem item : codeItems) {
        String key = item.getCode();
        String itemLabel = CEComponentHTMLFormatter.getItemLabel(item, language);
        String tooltip = CEComponentHTMLFormatter.getDescription(item, language);
        CETableRow row = new CETableRow(key, itemLabel, tooltip);
        for (NodeDefinition child : def.getChildDefinitions()) {
            if (!uiOptions.isHidden(child)) {
                row.addChild(createComponent(child, codeItemIdx + 1));
            }
        }
        ceTable.addRow(row);
        codeItemIdx++;
    }
    return ceTable;
}
Also used : CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) UIOptions(org.openforis.collect.metamodel.ui.UIOptions) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Aggregations

CodeListService (org.openforis.idm.metamodel.CodeListService)19 CodeListItem (org.openforis.idm.metamodel.CodeListItem)13 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)8 CodeList (org.openforis.idm.metamodel.CodeList)8 SurveyContext (org.openforis.idm.metamodel.SurveyContext)6 ArrayList (java.util.ArrayList)4 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 HashMap (java.util.HashMap)2 List (java.util.List)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 Code (org.openforis.idm.model.Code)2 NumberValue (org.openforis.idm.model.NumberValue)2 IOException (java.io.IOException)1 NumberFormat (java.text.NumberFormat)1 ExternalizedProperty (org.granite.messaging.amf.io.util.externalizer.annotation.ExternalizedProperty)1 CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)1 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1