Search in sources :

Example 1 with CodeListItemProxy

use of org.openforis.collect.metamodel.proxy.CodeListItemProxy 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;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) User(org.openforis.collect.model.User) UserInGroup(org.openforis.collect.model.UserInGroup) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) UserGroup(org.openforis.collect.model.UserGroup) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with CodeListItemProxy

use of org.openforis.collect.metamodel.proxy.CodeListItemProxy 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;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ArrayList(java.util.ArrayList) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Example 3 with CodeListItemProxy

use of org.openforis.collect.metamodel.proxy.CodeListItemProxy in project collect by openforis.

the class CodeAttributeProxy method getCodeListItem.

@ExternalizedProperty
public CodeListItemProxy getCodeListItem() {
    if (isEnumerator()) {
        CodeListService codeListManager = getCodeListService();
        CodeListItem codeListItem = codeListManager.loadItem(codeAttribute);
        return codeListItem == null ? null : new CodeListItemProxy(codeListItem);
    } else {
        return null;
    }
}
Also used : CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) ExternalizedProperty(org.granite.messaging.amf.io.util.externalizer.annotation.ExternalizedProperty)

Example 4 with CodeListItemProxy

use of org.openforis.collect.metamodel.proxy.CodeListItemProxy in project collect by openforis.

the class DataService method findAssignableCodeListItems.

/**
 * Finds a list of code list items assignable to the specified attribute and matching the passed codes
 *
 * @param parentEntityId
 * @param attributeName
 * @param codes
 * @return
 */
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attributeName, String[] codes) {
    CollectRecord record = getActiveRecord();
    Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attributeName);
    List<CodeListItem> items = codeListManager.findValidItems(parent, def, codes);
    List<CodeListItemProxy> result = new ArrayList<CodeListItemProxy>();
    for (CodeListItem item : items) {
        result.add(new CodeListItemProxy(item));
    }
    return result;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ArrayList(java.util.ArrayList) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)4 CodeListItem (org.openforis.idm.metamodel.CodeListItem)4 ArrayList (java.util.ArrayList)3 CollectRecord (org.openforis.collect.model.CollectRecord)3 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 Entity (org.openforis.idm.model.Entity)3 Secured (org.springframework.security.access.annotation.Secured)3 ExternalizedProperty (org.granite.messaging.amf.io.util.externalizer.annotation.ExternalizedProperty)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 User (org.openforis.collect.model.User)1 UserGroup (org.openforis.collect.model.UserGroup)1 UserInGroup (org.openforis.collect.model.UserInGroup)1 CodeListService (org.openforis.idm.metamodel.CodeListService)1 Node (org.openforis.idm.model.Node)1