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);
}
}
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);
}
}
}
}
}
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;
}
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();
}
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);
}
}
Aggregations