Search in sources :

Example 21 with CodeListItem

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

the class CodeListManager method findCodeListItem.

private CodeListItem findCodeListItem(List<CodeListItem> siblings, String code, ModelVersion version) {
    String adaptedCode = code.trim();
    // remove initial zeros
    adaptedCode = adaptedCode.replaceFirst("^0+", "");
    adaptedCode = Pattern.quote(adaptedCode);
    Pattern pattern = Pattern.compile("^[0]*" + adaptedCode + "$", Pattern.CASE_INSENSITIVE);
    for (CodeListItem item : siblings) {
        if (version == null || version.isApplicable(item)) {
            String itemCode = item.getCode();
            Matcher matcher = pattern.matcher(itemCode);
            if (matcher.find()) {
                return item;
            }
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 22 with CodeListItem

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

the class CodeListImagesExportTask method execute.

@Override
protected void execute() throws Throwable {
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList list : codeLists) {
        if (!list.isExternal()) {
            Deque<CodeListItem> stack = new LinkedList<CodeListItem>();
            List<CodeListItem> rootItems = codeListManager.loadRootItems(list);
            stack.addAll(rootItems);
            while (!stack.isEmpty()) {
                if (!isRunning()) {
                    break;
                }
                CodeListItem item = stack.pop();
                if (item instanceof PersistedCodeListItem && item.hasUploadedImage()) {
                    FileWrapper imageFileWrapper = codeListManager.loadImageContent((PersistedCodeListItem) item);
                    ZipEntry entry = new ZipEntry(getEntryName(item));
                    zipOutputStream.putNextEntry(entry);
                    IOUtils.write(imageFileWrapper.getContent(), zipOutputStream);
                    zipOutputStream.closeEntry();
                }
                List<CodeListItem> childItems = codeListManager.loadChildItems(item);
                for (CodeListItem childItem : childItems) {
                    stack.push(childItem);
                }
            }
        }
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ZipEntry(java.util.zip.ZipEntry) FileWrapper(org.openforis.collect.model.FileWrapper) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem) LinkedList(java.util.LinkedList) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 23 with CodeListItem

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

the class CollectEarthGridTemplateGenerator method getFirstAvailableCodeItem.

private CodeListItem getFirstAvailableCodeItem(AttributeDefinition attrDef) {
    CodeAttributeDefinition codeDefn = (CodeAttributeDefinition) attrDef;
    CodeList list = codeDefn.getList();
    CodeListService codeListService = attrDef.getSurvey().getContext().getCodeListService();
    Integer levelIndex = codeDefn.getListLevelIndex();
    int levelPosition = levelIndex == null ? 1 : levelIndex + 1;
    List<CodeListItem> items;
    if (levelPosition == 1) {
        items = codeListService.loadRootItems(list);
    } else {
        items = codeListService.loadItems(list, levelPosition);
    }
    if (items.isEmpty()) {
        return null;
    } else {
        return items.get(0);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 24 with CodeListItem

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

the class CollectEarthGridTemplateGenerator method getDummyValue.

private String getDummyValue(AttributeDefinition attrDef, Integer ord) {
    String attrName = attrDef.getName();
    String value;
    if (attrDef instanceof NumericAttributeDefinition || attrDef instanceof BooleanAttributeDefinition) {
        value = "0";
        if (ord != null) {
            value = ord + "";
        }
    } else if (attrDef instanceof DateAttributeDefinition) {
        value = "1/1/2000";
    } else if (attrDef instanceof CodeAttributeDefinition) {
        CodeListItem firstAvailableItem = getFirstAvailableCodeItem(attrDef);
        value = firstAvailableItem == null ? "0" : firstAvailableItem.getCode();
    } else {
        value = attrName;
        if (ord != null) {
            value += "_" + ord;
        }
    }
    return value;
}
Also used : CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 25 with CodeListItem

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

the class CodeColumnProvider method extractValues.

@Override
public List<String> extractValues(Node<?> axis) {
    List<String> values = super.extractValues(axis);
    if (hasExpandedItems) {
        List<Node<?>> attributes = extractNodes(axis);
        for (CodeListItem item : expandedItems) {
            CodeAttribute attr = findAttributeByCode(attributes, item.getCode());
            values.add(Boolean.valueOf(attr != null).toString());
            if (item.isQualifiable()) {
                values.add(attr == null ? "" : attr.getValue().getQualifier());
            }
        }
    }
    return values;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Node(org.openforis.idm.model.Node) 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