Search in sources :

Example 6 with UserInGroup

use of org.openforis.collect.model.UserInGroup in project collect by openforis.

the class RecordManager method addQualifierValues.

private void addQualifierValues(CollectRecord record, User user) {
    if (userGroupManager == null) {
        return;
    }
    NodeChangeMap changeSet = new NodeChangeMap();
    CollectSurvey survey = (CollectSurvey) record.getSurvey();
    UserGroup surveyUserGrup = survey.getUserGroup();
    UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyUserGrup, user);
    if (userInGroup == null) {
        throw new IllegalArgumentException(String.format("User %s is not allowed to create records for survey %s", user.getUsername(), survey.getName()));
    }
    UserGroup group = userGroupManager.loadById(userInGroup.getGroupId());
    Map<String, String> qualifiersByName = group.getQualifiersByName();
    for (Entry<String, String> qualifier : qualifiersByName.entrySet()) {
        String attributePath = record.getRootEntity().getName() + "/" + qualifier.getKey();
        Attribute<?, Value> attribute = record.findNodeByPath(attributePath);
        Value qualifierValue = attribute.getDefinition().createValue(qualifier.getValue());
        NodeChangeSet changes = updater.updateAttribute(attribute, qualifierValue);
        changeSet.addMergeChanges(changes);
    }
}
Also used : NodeChangeSet(org.openforis.collect.model.NodeChangeSet) UserInGroup(org.openforis.collect.model.UserInGroup) Value(org.openforis.idm.model.Value) NodeChangeMap(org.openforis.collect.model.NodeChangeMap) CollectSurvey(org.openforis.collect.model.CollectSurvey) UserGroup(org.openforis.collect.model.UserGroup)

Example 7 with UserInGroup

use of org.openforis.collect.model.UserInGroup in project collect by openforis.

the class UserGroupDao method findUsersInGroup.

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<UserInGroup> findUsersInGroup(final int userGroupId, final Integer userId) {
    final List<UserInGroup> result = new ArrayList<UserInGroup>();
    Condition conditions = OFC_USER_USERGROUP.GROUP_ID.eq(userGroupId);
    if (userId != null) {
        conditions = conditions.and(OFC_USER_USERGROUP.USER_ID.eq(userId));
    }
    dsl().select(OFC_USER_USERGROUP.USER_ID, OFC_USER_USERGROUP.ROLE_CODE, OFC_USER_USERGROUP.STATUS_CODE, OFC_USER_USERGROUP.REQUEST_DATE, OFC_USER_USERGROUP.MEMBER_SINCE, OFC_USER.USERNAME, OFC_USER.ENABLED).from(OFC_USER_USERGROUP).join(OFC_USER).on(OFC_USER.ID.eq(OFC_USER_USERGROUP.USER_ID)).where(conditions).orderBy(OFC_USER.USERNAME).fetchInto(new RecordHandler() {

        public void next(Record record) {
            UserInGroup userInGroup = new UserInGroup();
            userInGroup.setGroupId(userGroupId);
            userInGroup.setUserId(userId == null ? record.getValue(OFC_USER_USERGROUP.USER_ID) : userId);
            userInGroup.setRole(UserGroupRole.fromCode(record.getValue(OFC_USER_USERGROUP.ROLE_CODE)));
            userInGroup.setMemberSince(record.getValue(OFC_USER_USERGROUP.MEMBER_SINCE));
            userInGroup.setRequestDate(record.getValue(OFC_USER_USERGROUP.REQUEST_DATE));
            userInGroup.setJoinStatus(UserGroupJoinRequestStatus.fromCode(record.getValue(OFC_USER_USERGROUP.STATUS_CODE)));
            result.add(userInGroup);
        }
    });
    return result;
}
Also used : Condition(org.jooq.Condition) UserInGroup(org.openforis.collect.model.UserInGroup) RecordHandler(org.jooq.RecordHandler) ArrayList(java.util.ArrayList) Record(org.jooq.Record) OfcUsergroupRecord(org.openforis.collect.persistence.jooq.tables.records.OfcUsergroupRecord)

Example 8 with UserInGroup

use of org.openforis.collect.model.UserInGroup in project collect by openforis.

the class LocalUserGroupManager method save.

@Override
@Transactional(propagation = REQUIRED)
public UserGroup save(UserGroup userGroup, User modifiedByUser) {
    dao.save(userGroup);
    List<UserInGroup> oldUsersInGroup = dao.findUsersByGroup(userGroup.getId());
    Map<Integer, UserInGroup> parameterUsersInGroupByUserId = new HashMap<Integer, UserInGroup>();
    for (UserInGroup userInGroup : userGroup.getUsers()) {
        userInGroup.setGroupId(userGroup.getId());
        parameterUsersInGroupByUserId.put(userInGroup.getUserId(), userInGroup);
    }
    Map<Integer, UserInGroup> removedUsersInGroupByUserId = new HashMap<Integer, UserInGroup>();
    Map<Integer, UserInGroup> updatedUsersInGroupByUserId = new HashMap<Integer, UserInGroup>();
    Map<Integer, UserInGroup> newUsersInGroupByUserId = new HashMap<Integer, UserInGroup>();
    for (UserInGroup oldUserInGroup : oldUsersInGroup) {
        UserInGroup updatedUserInGroup = parameterUsersInGroupByUserId.get(oldUserInGroup.getUserId());
        if (updatedUserInGroup != null) {
            oldUserInGroup.setJoinStatus(updatedUserInGroup.getJoinStatus());
            oldUserInGroup.setRole(updatedUserInGroup.getRole());
            updatedUsersInGroupByUserId.put(oldUserInGroup.getUserId(), oldUserInGroup);
        } else {
            removedUsersInGroupByUserId.put(oldUserInGroup.getUserId(), oldUserInGroup);
        }
    }
    for (UserInGroup newUserInGroup : parameterUsersInGroupByUserId.values()) {
        if (!oldUsersInGroup.contains(newUserInGroup)) {
            newUserInGroup.setRequestDate(new Date());
            if (newUserInGroup.getJoinStatus() == ACCEPTED) {
                newUserInGroup.setMemberSince(new Date());
            }
            newUsersInGroupByUserId.put(newUserInGroup.getUserId(), newUserInGroup);
        }
    }
    for (UserInGroup userInGroup : removedUsersInGroupByUserId.values()) {
        dao.deleteRelation(userGroup.getId(), userInGroup.getUserId());
    }
    for (UserInGroup userInGroup : updatedUsersInGroupByUserId.values()) {
        dao.updateRelation(userInGroup);
    }
    for (UserInGroup userInGroup : newUsersInGroupByUserId.values()) {
        dao.insertRelation(userInGroup);
    }
    return userGroup;
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) HashMap(java.util.HashMap) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserInGroup

use of org.openforis.collect.model.UserInGroup in project collect by openforis.

the class LocalUserGroupManager method requestJoin.

@Transactional(readOnly = false, propagation = REQUIRED)
public void requestJoin(User user, UserGroup userGroup, UserGroupRole role) {
    UserInGroup userInGroup = new UserInGroup();
    userInGroup.setGroupId(userGroup.getId());
    userInGroup.setUserId(user.getId());
    userInGroup.setRole(role);
    userInGroup.setRequestDate(new Date());
    userInGroup.setJoinStatus(PENDING);
    dao.insertRelation(userInGroup);
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserInGroup

use of org.openforis.collect.model.UserInGroup in project collect by openforis.

the class LocalUserGroupManager method getQualifiers.

@Override
public Map<String, String> getQualifiers(UserGroup group, User user) {
    UserInGroup userInGroup = findUserInGroupOrDescendants(group, user);
    if (userInGroup == null) {
        throw new IllegalArgumentException(String.format("User %s not allowed to see records for user group %s", user.getUsername(), group.getName()));
    }
    UserGroup associatedGroup = loadById(userInGroup.getGroupId());
    return associatedGroup.getQualifiersByName();
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) UserGroup(org.openforis.collect.model.UserGroup)

Aggregations

UserInGroup (org.openforis.collect.model.UserInGroup)12 UserGroup (org.openforis.collect.model.UserGroup)7 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Transactional (org.springframework.transaction.annotation.Transactional)4 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 User (org.openforis.collect.model.User)3 HashMap (java.util.HashMap)2 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Condition (org.jooq.Condition)1 Record (org.jooq.Record)1 RecordHandler (org.jooq.RecordHandler)1 RecordAccessControlManager (org.openforis.collect.manager.RecordAccessControlManager)1 CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)1 CollectRecord (org.openforis.collect.model.CollectRecord)1 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)1 NodeChangeMap (org.openforis.collect.model.NodeChangeMap)1 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)1