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