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