use of org.openforis.idm.metamodel.CodeListItem in project collect by openforis.
the class CodeListImportProcessIntegrationTest method testMultiLangImport.
@Test
public void testMultiLangImport() throws Exception {
survey.addLanguage("es");
CodeList codeList = survey.createCodeList();
codeList.setName(TEST_CODE_LIST_NAME);
survey.addCodeList(codeList);
CodeListImportProcess process = importCSVFile(VALID_MULTILANG_TEST_CSV, codeList);
CodeListImportStatus status = process.getStatus();
assertTrue(status.isComplete());
assertTrue(status.getSkippedRows().isEmpty());
assertEquals(5, status.getProcessed());
List<CodeListItem> items = codeListManager.loadRootItems(codeList);
assertEquals(2, items.size());
{
CodeListItem item = codeListManager.loadRootItem(codeList, "001", null);
assertNotNull(item);
assertEquals("Dodoma", item.getLabel(LANG));
assertEquals("Dodoma ES", item.getLabel("es"));
List<CodeListItem> childItems = codeListManager.loadChildItems(item);
assertEquals(2, childItems.size());
CodeListItem childItem = childItems.get(0);
assertEquals("001", childItem.getCode());
assertEquals("Kondoa", childItem.getLabel(LANG));
childItem = childItems.get(1);
assertEquals("002", childItem.getCode());
assertEquals("Mpwapwa", childItem.getLabel(LANG));
assertEquals("Mpwapwa ES", childItem.getLabel("es"));
}
{
CodeListItem item = codeListManager.loadRootItem(codeList, "002", null);
assertNotNull(item);
assertEquals("Arusha", item.getLabel(LANG));
List<CodeListItem> childItems = codeListManager.loadChildItems(item);
assertEquals(2, childItems.size());
CodeListItem childItem = childItems.get(0);
assertEquals("001", childItem.getCode());
assertEquals("Monduli", childItem.getLabel(LANG));
assertNull(childItem.getLabel("es"));
childItem = childItems.get(1);
assertEquals("002", childItem.getCode());
assertEquals("Arumeru", childItem.getLabel(LANG));
}
}
use of org.openforis.idm.metamodel.CodeListItem in project collect by openforis.
the class CodeTableDataExtractor method next.
@Override
public Row next() {
Row row;
if (isDefaultCodeRowToBeCreated()) {
row = createDefaultCodeRow();
} else {
CodeListItem item = items.get(itemIndex);
row = extractRow(item);
itemIndex++;
}
count++;
return row;
}
use of org.openforis.idm.metamodel.CodeListItem in project collect by openforis.
the class CodeValueFKColumnValueExtractor method extractValue.
private Object extractValue(CodeAttribute valNode) {
CodeListItem item = findCodeListItem(valNode);
if (item == null) {
String defaultCodeValue = column.getDefaultCodeValue();
String codeValue = getCodeValue(valNode);
if (defaultCodeValue == null || (codeValue != null && !codeValue.equals(defaultCodeValue))) {
// no default code value specified or code list item not found (invalid code?)
return null;
} else {
CodeAttributeDefinition definition = valNode.getDefinition();
CodeList list = definition.getList();
Record record = valNode.getRecord();
ModelVersion version = record.getVersion();
return getDefaultCodeItemId(list, version);
}
} else {
return item.getId();
}
}
use of org.openforis.idm.metamodel.CodeListItem in project collect by openforis.
the class DataService method findAssignableCodeListItems.
/**
* Gets the code list items assignable to the specified attribute.
*
* @param parentEntityId
* @param attrName
* @return
*/
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attrName) {
CollectRecord record = getActiveRecord();
CollectSurvey survey = (CollectSurvey) record.getSurvey();
UserGroup surveyUserGroup = survey.getUserGroup();
User user = sessionManager.getLoggedUser();
final UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyUserGroup, user);
if (userInGroup == null) {
throw new IllegalStateException(String.format("User %s not allowed to access survey %s", user.getUsername(), survey.getName()));
}
Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attrName);
List<CodeListItem> items = codeListManager.loadValidItems(parent, def);
List<CodeListItem> filteredItems = new ArrayList<CodeListItem>(items);
// filter by user group qualifier (if any)
UserGroup group = userGroupManager.loadById(userInGroup.getGroupId());
String qualifierName = group.getQualifier1Name();
String listHierarchicalLevelName = def.getList().isHierarchical() ? def.getHierarchicalLevel() : def.getListName();
if (qualifierName != null && qualifierName.equals(listHierarchicalLevelName)) {
CollectionUtils.filter(filteredItems, new Predicate<CodeListItem>() {
public boolean evaluate(CodeListItem item) {
return item.getCode().equals(group.getQualifier1Value());
}
});
}
List<CodeListItemProxy> result = CodeListItemProxy.fromList(filteredItems);
List<Node<?>> selectedCodes = parent.getChildren(attrName);
CodeListItemProxy.setSelectedItems(result, selectedCodes);
return result;
}
use of org.openforis.idm.metamodel.CodeListItem in project collect by openforis.
the class DataService method getCodeListItems.
/**
* Gets the code list items assignable to the specified attribute and matching the specified codes.
*
* @param parentEntityId
* @param attrName
* @param codes
* @return
*/
@Secured(USER)
public List<CodeListItemProxy> getCodeListItems(int parentEntityId, String attrName, String[] codes) {
CollectRecord record = getActiveRecord();
Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attrName);
List<CodeListItem> items = codeListManager.loadValidItems(parent, def);
List<CodeListItem> filteredItems = new ArrayList<CodeListItem>();
if (codes != null && codes.length > 0) {
// filter by specified codes
for (CodeListItem item : items) {
for (String code : codes) {
if (item.getCode().equals(code)) {
filteredItems.add(item);
}
}
}
}
List<CodeListItemProxy> result = CodeListItemProxy.fromList(filteredItems);
return result;
}
Aggregations