KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > security > UserBrowserAction


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
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 org.apache.jetspeed.modules.actions.portlets.security;
18
19 // velocity
20
import org.apache.velocity.context.Context;
21 import org.apache.velocity.app.FieldMethodizer;
22
23 // turbine util
24
import org.apache.turbine.util.RunData;
25 import org.apache.turbine.util.StringUtils;
26
27 // jetspeed services
28
import org.apache.jetspeed.services.JetspeedSecurity;
29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30 import org.apache.jetspeed.services.logging.JetspeedLogger;
31 import org.apache.jetspeed.services.security.JetspeedSecurityException;
32 import org.apache.jetspeed.services.resources.JetspeedResources;
33 import org.apache.jetspeed.util.template.JetspeedLink;
34 import org.apache.jetspeed.om.security.JetspeedUser;
35
36 // jetspeed velocity
37
import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
38 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
39
40 import java.util.List JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.ArrayList JavaDoc;
43
44 // regexp stuff
45
import org.apache.regexp.RE;
46 import org.apache.regexp.RECompiler;
47
48 /**
49  * This action sets up the template context for browsing of users in the Turbine database.
50  *
51  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
52  * @version $Id: UserBrowserAction.java,v 1.14 2004/02/23 02:53:08 jford Exp $
53  */

54 public class UserBrowserAction extends VelocityPortletAction
55 {
56     /** name of the parameter to this portlet that tells us how many rows to show per page */
57     public static final String JavaDoc NUMBER_PER_PAGE = "number-per-page";
58
59     /** name of the parameter that holds the page number to display */
60     public static final String JavaDoc DISPLAY_PAGE = "ubpage";
61
62     /** name of the parameter that holds the filter value */
63     public static final String JavaDoc FILTER_VALUE = "filter_value";
64
65     /** name of the parameter that holds the regexp flag */
66     public static final String JavaDoc FILTER_REGEXP = "filter_regexp";
67
68     /** name of the parameter that holds the filter type */
69     public static final String JavaDoc FILTER_TYPE = "filter_type";
70
71     /** value of the filter type parameter for searching by username */
72     public static final String JavaDoc FILTER_TYPE_USERNAME = "filter_type_username";
73
74     /** value of the filter type parameter for searching by last name */
75     public static final String JavaDoc FILTER_TYPE_LASTNAME = "filter_type_lastname";
76
77     /**
78      * Static initialization of the logger for this class
79      */

80     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserBrowserAction.class.getName());
81     
82     /**
83      * Build the maximized state content for this portlet. (Same as normal state).
84      *
85      * @param portlet The velocity-based portlet that is being built.
86      * @param context The velocity context for this request.
87      * @param rundata The turbine rundata context for this request.
88      */

89     protected void buildMaximizedContext( VelocityPortlet portlet,
90                                           Context context,
91                                           RunData rundata )
92     {
93         buildNormalContext( portlet, context, rundata);
94     }
95
96     /**
97      * Build the configure state content for this portlet.
98      * TODO: we could configure this portlet with configurable skins, etc..
99      *
100      * @param portlet The velocity-based portlet that is being built.
101      * @param context The velocity context for this request.
102      * @param rundata The turbine rundata context for this request.
103      */

104     protected void buildConfigureContext( VelocityPortlet portlet,
105                                           Context context,
106                                           RunData rundata )
107     {
108
109         buildNormalContext( portlet, context, rundata);
110     }
111
112     /**
113      * Build the normal state content for this portlet.
114      *
115      * @param portlet The velocity-based portlet that is being built.
116      * @param context The velocity context for this request.
117      * @param rundata The turbine rundata context for this request.
118      */

119     protected void buildNormalContext( VelocityPortlet portlet,
120                                        Context context,
121                                        RunData rundata )
122     {
123         try
124         {
125             //hack to make the static variables visible in template
126
context.put("s_config", new FieldMethodizer( context.get("config") ) );
127
128             // Currently, the getUsers(filter) is not implemented - need to do local filtering
129
Iterator JavaDoc users = JetspeedSecurity.getUsers();
130
131             List JavaDoc userList = new ArrayList JavaDoc();
132
133             // Is filtering requested?
134
String JavaDoc filterValue = rundata.getParameters().getString(FILTER_VALUE);
135             if (filterValue != null)
136             {
137                 String JavaDoc filterType = rundata.getParameters().getString(FILTER_TYPE, FILTER_TYPE_USERNAME);
138                 boolean useRE = rundata.getParameters().getBoolean(FILTER_REGEXP);
139                 RE r = null;
140                 RECompiler rc = null;
141                 if (useRE)
142                 {
143                     try
144                     {
145                         rc = new RECompiler();
146                         r = new RE();
147                         r.setProgram(rc.compile(filterValue));
148                     }
149                     catch (org.apache.regexp.RESyntaxException rex)
150                     {
151                         logger.warn("UserBrowserAction: error processing regular expression [" + filterValue + "]: " +
152                                  rex.toString());
153                     }
154                 }
155                 while (users.hasNext())
156                 {
157                     JetspeedUser user = (JetspeedUser) users.next();
158                     String JavaDoc compareValue = null;
159                     if (filterType.equals(FILTER_TYPE_USERNAME))
160                     {
161                         compareValue = user.getUserName();
162                     }
163                     else if (filterType.equals(FILTER_TYPE_LASTNAME))
164                     {
165                         compareValue = user.getLastName();
166                     }
167
168                     if (compareValue != null)
169                     {
170                         if (useRE && r.match(compareValue))
171                         {
172                                 userList.add(user);
173                         }
174                         else if (compareValue.startsWith(filterValue))
175                         {
176                             userList.add(user);
177                         }
178                     }
179                 }
180             } else {
181                 while (users.hasNext())
182                 {
183                     userList.add(users.next());
184                 }
185             }
186
187
188             int currentPage = rundata.getParameters().getInt(DISPLAY_PAGE, 1);
189
190             int numberPerPage;
191
192             try
193             {
194                 numberPerPage = Integer.parseInt(portlet.getPortletConfig().getInitParameter(NUMBER_PER_PAGE,"50"));
195             }
196             catch (NumberFormatException JavaDoc e)
197             {
198                 numberPerPage = 50;
199             }
200
201             if (userList.size() > numberPerPage)
202             {
203                 int numberOfPages = (int) ((userList.size() - 1 + numberPerPage) / numberPerPage);
204                 int from = (currentPage - 1) * numberPerPage;
205                 int to = Math.min(currentPage * numberPerPage,userList.size());
206                 context.put(SecurityConstants.CONTEXT_USERS, userList.subList(from, to));
207
208                 //now build a set of links to access each page (assumed we will show all links)
209
StringBuffer JavaDoc pageLinks = new StringBuffer JavaDoc("page: ");
210                 for (int i = 1; i <= numberOfPages; i++)
211                 {
212                     if (i == currentPage)
213                     {
214                         pageLinks.append("( " + i + " ) &nbsp;");
215                     }
216                     else
217                     {
218 // make sure the page navigation always points to
219
// the right pane
220
Object JavaDoc jslink = context.get("jslink");
221                         if (jslink instanceof JetspeedLink) {
222                          pageLinks.append("[ <a HREF=\"" + ((JetspeedLink)jslink).getPaneByName("UserBrowser").addQueryData(DISPLAY_PAGE, new Integer JavaDoc(i)).toString() + "\">" + i + "</a> ] &nbsp;");
223                         } else {
224                          pageLinks.append("[ <a HREF=\"./portal/" + DISPLAY_PAGE + "/" + i + "\">" + i + "</a> ] &nbsp;");
225                         }
226                     }
227                 }
228                 context.put("pagelinks", pageLinks);
229             }
230             else
231             {
232                 context.put(SecurityConstants.CONTEXT_USERS, userList);
233             }
234             context.put(DISPLAY_PAGE,Integer.toString(currentPage));
235
236
237         }
238         catch (JetspeedSecurityException e)
239         {
240           // log the error msg
241
logger.error("Exception", e);
242
243             rundata.setMessage("Error in Jetspeed User Security: " + e.toString());
244             rundata.setStackTrace(StringUtils.stackTrace(e), e);
245             rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
246         }
247     }
248
249 }
Popular Tags