Search in sources :

Example 56 with CodeListItem

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

the class CodeListImportTask method parseCSVLines.

private void parseCSVLines() {
    long currentRowNumber = 0;
    try {
        CollectSurvey survey = (CollectSurvey) codeList.getSurvey();
        List<String> languages = survey.getLanguages();
        String defaultLanguage = survey.getDefaultLanguage();
        File file = OpenForisIOUtils.copyToTempFile(inputStream);
        reader = new CodeListCSVReader(file, csvFileOptions, languages, defaultLanguage);
        reader.init();
        levels = reader.getLevels();
        addProcessedRow(1);
        currentRowNumber = 2;
        while (isRunning()) {
            try {
                CodeListLine line = reader.readNextLine();
                if (line != null) {
                    CodeListItem currentParentItem = null;
                    List<String> levelCodes = line.getLevelCodes();
                    for (int levelIdx = 0; levelIdx < levelCodes.size(); levelIdx++) {
                        boolean lastLevel = levelIdx == levelCodes.size() - 1;
                        CodeListItem item = processLevel(currentParentItem, line, levelIdx, lastLevel);
                        currentParentItem = item;
                    }
                    addProcessedRow(currentRowNumber);
                }
                if (!reader.isReady()) {
                    break;
                }
            } catch (ParsingException e) {
                addParsingError(currentRowNumber, e.getError());
            } finally {
                currentRowNumber++;
            }
        }
    } catch (ParsingException e) {
        changeStatus(Status.FAILED);
        addParsingError(1, e.getError());
    } catch (Exception e) {
        changeStatus(Status.FAILED);
        addParsingError(currentRowNumber, new ParsingError(ErrorType.IOERROR, e.toString()));
        LOG.error("Error importing code list CSV file", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : CodeListCSVReader(org.openforis.collect.manager.codelistimport.CodeListCSVReader) ParsingException(org.openforis.collect.io.exception.ParsingException) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) ParsingException(org.openforis.collect.io.exception.ParsingException) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListLine(org.openforis.collect.manager.codelistimport.CodeListLine) CodeListItem(org.openforis.idm.metamodel.CodeListItem) File(java.io.File)

Example 57 with CodeListItem

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

the class CEComponentHTMLFormatter method buildCodeButtonGroup.

private void buildCodeButtonGroup(XMLBuilder formControlContainer, CECodeField comp) {
    String elId = comp.getHtmlParameterName();
    // button groups external container
    // $NON-NLS-1$
    String groupId = elId + "_group";
    XMLBuilder itemsGroupExternalContainer = // $NON-NLS-1$
    formControlContainer.e("div").a("id", // $NON-NLS-1$
    groupId).a("class", // $NON-NLS-1$ //$NON-NLS-2$
    "code-items-group");
    XMLBuilder hiddenInputField = // $NON-NLS-1$
    itemsGroupExternalContainer.e("input").a("id", // $NON-NLS-1$
    elId).a("name", // $NON-NLS-1$
    elId).a("type", // $NON-NLS-1$ //$NON-NLS-2$
    "hidden").a("class", // $NON-NLS-1$ //$NON-NLS-2$
    "form-control").a("data-field-type", // $NON-NLS-1$
    comp.getType().name());
    if (comp.getParentName() != null) {
        // $NON-NLS-1$
        hiddenInputField.a("data-parent-id-field-id", comp.getParentName());
    }
    Map<Integer, List<CodeListItem>> itemsByParentCode = ((CECodeField) comp).getCodeItemsByParentId();
    for (Entry<Integer, List<CodeListItem>> entry : itemsByParentCode.entrySet()) {
        // one button group for every list of codes by parent code
        Integer parentId = entry.getKey();
        // $NON-NLS-1$
        String itemsGroupId = groupId + "_" + parentId;
        XMLBuilder buttonsGroup = itemsGroupExternalContainer.e(// $NON-NLS-1$
        "div").a("id", // $NON-NLS-1$
        itemsGroupId).a("class", // $NON-NLS-1$ //$NON-NLS-2$
        "code-items").a("data-toggle", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        comp.isMultiple() ? "buttons" : "buttons-radio");
        if (parentId != 0) {
            buttonsGroup.a("data-parent-id", // $NON-NLS-1$
            parentId.toString()).a("style", // $NON-NLS-1$ //$NON-NLS-2$
            "display: none;");
        }
        List<CodeListItem> items = entry.getValue();
        if (items == null || items.isEmpty()) {
            // always use close tag //$NON-NLS-1$
            buttonsGroup.t(" ");
        } else {
            for (CodeListItem item : items) {
                String itemLabel = getItemLabel(item);
                XMLBuilder itemBuilder = buttonsGroup.e(// $NON-NLS-1$
                "button").a("type", // $NON-NLS-1$ //$NON-NLS-2$
                "button").a("class", // $NON-NLS-1$ //$NON-NLS-2$
                "btn btn-info code-item").a("data-code-item-id", // $NON-NLS-1$
                String.valueOf(item.getId())).a("value", // $NON-NLS-1$
                item.getCode()).t(itemLabel);
                String description = getDescription(item);
                if (item.hasUploadedImage()) {
                    String imgFilePath = CollectEarthProjectFileCreatorImpl.getCodeListImageFilePath(item);
                    // $NON-NLS-1$
                    String titleText = StringUtils.isBlank(description) ? "" : description;
                    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    String htmlTitle = "<span><img src=\"" + imgFilePath + "\" width=\"250\"><br/>" + titleText + "</span>";
                    itemBuilder.a("title", // $NON-NLS-1$
                    htmlTitle).a("data-html", // $NON-NLS-1$ //$NON-NLS-2$
                    "true").a("data-placement", // $NON-NLS-1$ //$NON-NLS-2$
                    "auto bottom");
                } else if (StringUtils.isNotBlank(description)) {
                    // $NON-NLS-1$
                    itemBuilder.a("title", description);
                }
            }
        }
    }
}
Also used : List(java.util.List) CodeListItem(org.openforis.idm.metamodel.CodeListItem) XMLBuilder(com.jamesmurty.utils.XMLBuilder)

Example 58 with CodeListItem

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

the class SurveyErrorsPopUpVM method createErrors.

protected static List<SurveyObjectError> createErrors(List<? extends SurveyObject> items) {
    List<SurveyObjectError> errors = new ArrayList<SurveyErrorsPopUpVM.SurveyObjectError>();
    for (SurveyObject item : items) {
        String path = null;
        String message = null;
        if (item instanceof NodeDefinition) {
            path = ((NodeDefinition) item).getPath();
        } else if (item instanceof CodeList) {
            path = ((CodeList) item).getName();
        } else if (item instanceof CodeListItem) {
            CodeListItem codeListItem = (CodeListItem) item;
            path = getPath(codeListItem);
        }
        SurveyObjectError error = new SurveyObjectError(path, message);
        errors.add(error);
    }
    return errors;
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) SurveyObject(org.openforis.idm.metamodel.SurveyObject) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 59 with CodeListItem

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

the class SurveyErrorsPopUpVM method getPath.

protected static String getPath(CodeListItem codeListItem) {
    CodeList codeList = codeListItem.getCodeList();
    StringBuilder sb = new StringBuilder();
    CodeListItem currentItem = codeListItem;
    while (currentItem != null) {
        sb.insert(0, currentItem.getCode());
        sb.insert(0, "/");
        currentItem = currentItem.getParentItem();
    }
    sb.insert(0, codeList.getName());
    return sb.toString();
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 60 with CodeListItem

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

the class CodeListImportProcess method parseCSVLines.

protected void parseCSVLines() {
    long currentRowNumber = 0;
    try {
        CollectSurvey survey = (CollectSurvey) codeList.getSurvey();
        List<String> languages = survey.getLanguages();
        String defaultLanguage = survey.getDefaultLanguage();
        reader = new CodeListCSVReader(file, csvFileOptions, languages, defaultLanguage);
        reader.init();
        levels = reader.getLevels();
        status.addProcessedRow(1);
        currentRowNumber = 2;
        while (status.isRunning()) {
            try {
                CodeListLine line = reader.readNextLine();
                if (line != null) {
                    CodeListItem currentParentItem = null;
                    List<String> levelCodes = line.getLevelCodes();
                    for (int levelIdx = 0; levelIdx < levelCodes.size(); levelIdx++) {
                        boolean lastLevel = levelIdx == levelCodes.size() - 1;
                        CodeListItem item = processLevel(currentParentItem, line, levelIdx, lastLevel);
                        currentParentItem = item;
                    }
                    status.addProcessedRow(currentRowNumber);
                }
                if (!reader.isReady()) {
                    break;
                }
            } catch (ParsingException e) {
                status.addParsingError(currentRowNumber, e.getError());
            } finally {
                currentRowNumber++;
            }
        }
        status.setTotal(reader.getLinesRead() + 1);
    } catch (ParsingException e) {
        status.error();
        status.addParsingError(1, e.getError());
    } catch (Exception e) {
        status.error();
        status.addParsingError(currentRowNumber, new ParsingError(ErrorType.IOERROR, e.toString()));
        LOG.error("Error importing species CSV file", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) ParsingException(org.openforis.collect.io.exception.ParsingException) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) IOException(java.io.IOException) ParsingException(org.openforis.collect.io.exception.ParsingException)

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