01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package info.jtrac.util;
17:
18: import info.jtrac.domain.ItemUser;
19: import info.jtrac.domain.Space;
20: import info.jtrac.domain.User;
21: import info.jtrac.domain.UserSpaceRole;
22: import java.util.ArrayList;
23: import java.util.HashMap;
24: import java.util.HashSet;
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Set;
28:
29: /**
30: * routines to filter User, UserSpaceRoles collections etc
31: */
32: public class UserUtils {
33:
34: /**
35: * This is a rather 'deep' concept, first of course you need to restrict the next possible
36: * states that an item can be switched to based on the current state and the workflow defined.
37: * But what about who all it can be assigned to? This will be the set of users who fall into roles
38: * that have permissions to transition FROM the state being switched to. Ouch.
39: * This is why the item_view / history update screen has to be Ajaxed so that the drop
40: * down list of users has to dynamically change based on the TO state
41: */
42: public static List<User> filterUsersAbleToTransitionFrom(
43: List<UserSpaceRole> userSpaceRoles, Space space, int state) {
44: Set<String> set = space.getMetadata()
45: .getRolesAbleToTransitionFrom(state);
46: List<User> list = new ArrayList<User>(userSpaceRoles.size());
47: for (UserSpaceRole usr : userSpaceRoles) {
48: if (set.contains(usr.getRoleKey())) {
49: list.add(usr.getUser());
50: }
51: }
52: return list;
53: }
54:
55: /**
56: * used to init backing form object in wicket corresponding to ItemUser / notifyList
57: */
58: public static List<ItemUser> convertToItemUserList(
59: List<UserSpaceRole> userSpaceRoles) {
60: List<ItemUser> itemUsers = new ArrayList<ItemUser>(
61: userSpaceRoles.size());
62: Set<User> users = new HashSet<User>(itemUsers.size());
63: for (UserSpaceRole usr : userSpaceRoles) {
64: User user = usr.getUser();
65: // we need to do this check as now JTrac supports same user mapped
66: // more than once to a space with different roles
67: if (!users.contains(user)) {
68: users.add(user);
69: itemUsers.add(new ItemUser(user));
70: }
71: }
72: return itemUsers;
73: }
74:
75: /**
76: * used to prepare drop down lists for the search screen in the ui
77: */
78: public static Map<Long, String> getSpaceNamesMap(User user) {
79: Map<Long, String> map = new HashMap<Long, String>(user
80: .getUserSpaceRoles().size());
81: for (UserSpaceRole usr : user.getUserSpaceRoles()) {
82: if (usr.getSpace() != null) {
83: map.put(usr.getSpace().getId(), usr.getSpace()
84: .getName());
85: }
86: }
87: return map;
88: }
89: }
|