Search in sources :

Example 16 with CodeListItem

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

the class CollectEarthBalloonGenerator method createComponent.

private CEComponent createComponent(NodeDefinition def, int entityPosition) {
    String label = def.getLabel(Type.INSTANCE, language);
    if (label == null && !isDefaultLanguage()) {
        label = def.getLabel(Type.INSTANCE);
    }
    if (label == null) {
        label = def.getName();
    }
    boolean multiple = def.isMultiple();
    UIOptions uiOptions = survey.getUIOptions();
    boolean hideWhenNotRelevant = uiOptions.isHideWhenNotRelevant(def);
    CEComponent comp;
    if (def instanceof EntityDefinition) {
        if (def.isMultiple() && ((EntityDefinition) def).isEnumerable()) {
            comp = createEnumeratedEntityComponent((EntityDefinition) def);
        } else {
            String tooltip = def.getDescription(language);
            CEFieldSet fieldSet = new CEFieldSet(def.getName(), label, tooltip);
            for (NodeDefinition child : ((EntityDefinition) def).getChildDefinitions()) {
                if (!uiOptions.isHidden(child)) {
                    fieldSet.addChild(createComponent(child));
                }
            }
            comp = fieldSet;
        }
    } else {
        AttributeDefinition attrDef = (AttributeDefinition) def;
        String htmlParameterName;
        boolean insideEnumeratedEntity = def.getParentEntityDefinition().isEnumerable();
        if (insideEnumeratedEntity) {
            htmlParameterName = getEnumeratedEntityComponentHtmlParameterName(def.getParentEntityDefinition(), entityPosition, def);
        } else {
            htmlParameterName = getHtmlParameterName(def);
        }
        String tooltip = attrDef.getDescription(language);
        CEFieldType type = getFieldType(def);
        boolean key = def instanceof KeyAttributeDefinition ? ((KeyAttributeDefinition) def).isKey() : false;
        if (insideEnumeratedEntity && key) {
            comp = new CEEnumeratingCodeField(htmlParameterName, def.getName(), label, tooltip, multiple, type, key);
        } else if (def instanceof CodeAttributeDefinition) {
            CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) def;
            CodeList list = codeAttrDef.getList();
            Integer listLevelIndex = codeAttrDef.getListLevelIndex();
            Map<Integer, List<CodeListItem>> codeItemsByParentCodeItemId = getCodeListItemsByParentId(list, listLevelIndex);
            CodeAttributeDefinition parentCodeAttributeDef = codeAttrDef.getParentCodeAttributeDefinition();
            String parentName = parentCodeAttributeDef == null ? null : getHtmlParameterName(parentCodeAttributeDef);
            comp = new CECodeField(htmlParameterName, def.getName(), label, tooltip, type, multiple, key, codeItemsByParentCodeItemId, parentName);
        } else {
            comp = new CEField(htmlParameterName, def.getName(), label, tooltip, multiple, type, key);
        }
        CollectAnnotations annotations = survey.getAnnotations();
        if (attrDef.isCalculated() || (annotations.isFromCollectEarthCSV(attrDef) && annotations.isShowReadOnlyFieldInCollectEarth(attrDef))) {
            ((CEField) comp).setReadOnly(true);
        }
    }
    comp.hideWhenNotRelevant = hideWhenNotRelevant;
    componentByName.put(comp.getName(), comp);
    return comp;
}
Also used : UIOptions(org.openforis.collect.metamodel.ui.UIOptions) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) TextAttributeDefinition(org.openforis.idm.metamodel.TextAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) KeyAttributeDefinition(org.openforis.idm.metamodel.KeyAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) CollectAnnotations(org.openforis.collect.metamodel.CollectAnnotations) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeList(org.openforis.idm.metamodel.CodeList) CEFieldType(org.openforis.collect.io.metadata.collectearth.balloon.CEField.CEFieldType) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) KeyAttributeDefinition(org.openforis.idm.metamodel.KeyAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with CodeListItem

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

the class CollectEarthBalloonGenerator method getCodeListItemsByParentId.

private Map<Integer, List<CodeListItem>> getCodeListItemsByParentId(CodeList list, Integer listLevelIndex) {
    CodeListService codeListService = list.getSurvey().getContext().getCodeListService();
    Map<Integer, List<CodeListItem>> codeItemsByParentCodeItemId = new HashMap<Integer, List<CodeListItem>>();
    if (listLevelIndex == null || listLevelIndex == 0) {
        List<CodeListItem> rootCodeItems = codeListService.loadRootItems(list);
        // root items
        codeItemsByParentCodeItemId.put(0, rootCodeItems);
    } else {
        int listLevelPosition = listLevelIndex + 1;
        List<CodeListItem> parentLevelItems;
        if (listLevelPosition == 2) {
            parentLevelItems = codeListService.loadRootItems(list);
        } else {
            parentLevelItems = codeListService.loadItems(list, listLevelPosition - 1);
        }
        for (CodeListItem parentItem : parentLevelItems) {
            List<CodeListItem> childItems = codeListService.loadChildItems(parentItem);
            if (!childItems.isEmpty()) {
                codeItemsByParentCodeItemId.put(parentItem.getId(), childItems);
            }
        }
    }
    return codeItemsByParentCodeItemId;
}
Also used : HashMap(java.util.HashMap) CodeListService(org.openforis.idm.metamodel.CodeListService) ArrayList(java.util.ArrayList) CodeList(org.openforis.idm.metamodel.CodeList) List(java.util.List) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 18 with CodeListItem

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

the class CodeListImportProcess method processLevel.

protected CodeListItem processLevel(CodeListItem parent, CodeListLine line, int levelIdx, boolean lastLevel) {
    CodeListItem result;
    // validate code
    List<String> codes = line.getLevelCodes();
    if (codes.isEmpty()) {
        addEmptyCodeColumnError(line, levelIdx);
    }
    String code = codes.get(levelIdx);
    if (lastLevel && isDuplicate(code, parent)) {
        addDuplicateCodeError(line, levelIdx);
    }
    // validate labels
    List<LanguageSpecificText> labels = line.getLabelItems(levelIdx);
    if (CollectionUtils.isEmpty(labels)) {
        addMissingDefaultLanguageLabelError(line, levelIdx);
    } else {
        for (LanguageSpecificText label : labels) {
            if (hasDifferentLabel(code, label, parent)) {
                addDifferentLabelError(line, levelIdx, label.getLanguage());
            }
        }
    }
    result = getChildItem(parent, code);
    if (result == null) {
        result = codeList.createItem(levelIdx + 1);
        List<LanguageSpecificText> descriptions = line.getDescriptionItems(levelIdx);
        fillItem(result, code, labels, descriptions);
        if (parent == null) {
            codeToRootItem.put(code, result);
        } else {
            parent.addChildItem(result);
        }
    }
    return result;
}
Also used : LanguageSpecificText(org.openforis.idm.metamodel.LanguageSpecificText) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 19 with CodeListItem

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

the class CodeListExportProcess method writeItem.

protected void writeItem(CsvWriter writer, CodeListItem item, List<CodeListItem> ancestors) {
    List<String> lineValues = new ArrayList<String>();
    addAncestorsLineValues(lineValues, ancestors);
    addItemLineValues(lineValues, item);
    writer.writeNext(lineValues);
    List<CodeListItem> children = codeListManager.loadChildItems(item);
    List<CodeListItem> childAncestors = new ArrayList<CodeListItem>(ancestors);
    childAncestors.add(item);
    for (CodeListItem child : children) {
        writeItem(writer, child, childAncestors);
    }
}
Also used : ArrayList(java.util.ArrayList) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 20 with CodeListItem

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

the class CodeListExportProcess method exportToCSV.

public void exportToCSV(OutputStream out, CollectSurvey survey, int codeListId) {
    CsvWriter writer = null;
    try {
        OutputStreamWriter osWriter = new OutputStreamWriter(out, Charset.forName("UTF-8"));
        writer = new CsvWriter(osWriter, SEPARATOR, QUOTECHAR);
        CodeList list = survey.getCodeListById(codeListId);
        initHeaders(writer, survey, list);
        List<CodeListItem> rootItems = codeListManager.loadRootItems(list);
        for (CodeListItem item : rootItems) {
            List<CodeListItem> ancestors = Collections.emptyList();
            writeItem(writer, item, ancestors);
        }
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CsvWriter(org.openforis.commons.io.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Aggregations

CodeListItem (org.openforis.idm.metamodel.CodeListItem)69 CodeList (org.openforis.idm.metamodel.CodeList)26 ArrayList (java.util.ArrayList)19 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)19 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)18 CodeListService (org.openforis.idm.metamodel.CodeListService)13 List (java.util.List)7 CollectSurvey (org.openforis.collect.model.CollectSurvey)7 ModelVersion (org.openforis.idm.metamodel.ModelVersion)7 Test (org.junit.Test)5 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)5 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)5 Entity (org.openforis.idm.model.Entity)5 Record (org.openforis.idm.model.Record)5 CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)4 CollectRecord (org.openforis.collect.model.CollectRecord)4 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)4 CodeAttribute (org.openforis.idm.model.CodeAttribute)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3