Search in sources :

Example 46 with CodeListItem

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

the class CodeListManager method loadValidItems.

public <T extends CodeListItem> List<T> loadValidItems(Entity parent, CodeAttributeDefinition def) {
    List<T> items = null;
    CodeList list = def.getList();
    if (StringUtils.isEmpty(def.getParentExpression())) {
        items = loadRootItems(list);
    } else {
        CodeAttribute parentCodeAttribute = getCodeParent(parent, def);
        if (parentCodeAttribute != null) {
            CodeListItem parentCodeListItem = loadItemByAttribute(parentCodeAttribute);
            if (parentCodeListItem != null) {
                items = loadChildItems(parentCodeListItem);
            }
        }
    }
    Record record = parent.getRecord();
    ModelVersion version = record.getVersion();
    return filterApplicableItems(items, version);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 47 with CodeListItem

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

the class CodeListManager method createPersistedItems.

protected List<PersistedCodeListItem> createPersistedItems(Collection<CodeListItem> items, int nextId, Integer parentItemId) {
    List<PersistedCodeListItem> result = new ArrayList<PersistedCodeListItem>();
    int sortOrder = 1;
    for (CodeListItem item : items) {
        PersistedCodeListItem persistedChildItem = PersistedCodeListItem.fromItem(item);
        persistedChildItem.setParentId(parentItemId);
        int id = nextId++;
        persistedChildItem.setSystemId(id);
        persistedChildItem.setSortOrder(sortOrder++);
        result.add(persistedChildItem);
        List<PersistedCodeListItem> temp = createPersistedItems(item.getChildItems(), nextId, id);
        nextId += temp.size();
        result.addAll(temp);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 48 with CodeListItem

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

the class CodeListManager method findValidItems.

public List<CodeListItem> findValidItems(Entity parent, CodeAttributeDefinition defn, String... codes) {
    List<CodeListItem> result = new ArrayList<CodeListItem>();
    List<CodeListItem> assignableItems = loadValidItems(parent, defn);
    if (!assignableItems.isEmpty()) {
        Record record = parent.getRecord();
        ModelVersion version = record.getVersion();
        for (String code : codes) {
            CodeListItem item = findCodeListItem(assignableItems, code, version);
            if (item != null) {
                result.add(item);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 49 with CodeListItem

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

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

the class CodeListImagesImportTask method execute.

@Override
protected void execute() throws Throwable {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        String entryName = entry.getName();
        if (CodeListImageEntry.isValidEntry(entryName)) {
            CodeListImageEntry codeListImageEntry = CodeListImageEntry.parseEntryName(entryName);
            CodeList codeList = survey.getCodeListById(codeListImageEntry.getListId());
            if (codeList != null) {
                CodeListItem item = codeListManager.loadItem(codeList, codeListImageEntry.getItemId());
                if (item != null && item instanceof PersistedCodeListItem) {
                    byte[] content = IOUtils.toByteArray(zipFile.getInputStream(entry));
                    codeListManager.saveImageContent((PersistedCodeListItem) item, new FileWrapper(content, codeListImageEntry.getFileName()));
                } else {
                    log().warn("Error restoring code list image file : " + entry.getName());
                }
            }
        }
    }
}
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) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

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