KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > util > UserUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package info.jtrac.util;
18
19 import info.jtrac.domain.Space;
20 import info.jtrac.domain.UserSpaceRole;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import org.springframework.util.StringUtils;
28 import org.springframework.web.servlet.i18n.SessionLocaleResolver;
29 import org.springframework.webflow.context.servlet.ServletExternalContext;
30 import org.springframework.webflow.execution.RequestContext;
31
32 /**
33  * routines to filter User, UserSpaceRoles collections etc
34  */

35 public class UserUtils {
36     
37     public static SessionLocaleResolver slr = new SessionLocaleResolver();
38     
39     public static void refreshLocale(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc localeString) {
40         if (localeString == null) {
41             localeString = "en";
42         }
43         Locale JavaDoc locale = StringUtils.parseLocaleString(localeString);
44         slr.setLocale(request, response, locale);
45     }
46     
47     public static void refreshLocale(RequestContext context, String JavaDoc localeString) {
48         ServletExternalContext servletContext = (ServletExternalContext) context.getLastEvent().getSource();
49         refreshLocale(servletContext.getRequest(), servletContext.getResponse(), localeString);
50     }
51     
52     /**
53      * This is a rather 'deep' concept, first of course you need to restrict the next possible
54      * states that an item can be switched to based on the current state and the workflow defined.
55      * But what about who all it can be assigned to? This will be the set of users who fall into roles
56      * that have permissions to transition FROM the state being switched to. Ouch.
57      * This is why the item_view / history update screen has to be Ajaxed so that the drop
58      * down list of users has to dynamically change based on the TO state
59      */

60      public static List JavaDoc<UserSpaceRole> filterUsersAbleToTransitionFrom(List JavaDoc<UserSpaceRole> userSpaceRoles, Space space, int state) {
61         Set JavaDoc<String JavaDoc> set = space.getMetadata().getRolesAbleToTransitionFrom(state);
62         List JavaDoc<UserSpaceRole> list = new ArrayList JavaDoc<UserSpaceRole>(userSpaceRoles.size());
63         for(UserSpaceRole usr : userSpaceRoles) {
64             if(set.contains(usr.getRoleKey())) {
65                 list.add(usr);
66             }
67         }
68         return list;
69      }
70 }
71
Popular Tags