KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > PsmlBrowseAction


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;
18
19 //Jetspeed
20
import org.apache.jetspeed.om.profile.Profile;
21 import org.apache.jetspeed.om.profile.QueryLocator;
22 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25 import org.apache.jetspeed.portal.portlets.browser.DatabaseBrowserIterator;
26 import org.apache.jetspeed.services.Profiler;
27 import org.apache.jetspeed.util.PortletConfigState;
28 import org.apache.jetspeed.util.PortletSessionState;
29
30 // Turbine stuff
31
import org.apache.turbine.util.RunData;
32
33 // Velocity Stuff
34
import org.apache.velocity.context.Context;
35
36 //Java
37
import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 // regexp stuff
41
import org.apache.regexp.RE;
42 import org.apache.regexp.RECompiler;
43
44 /**
45  * This action enables to browse any of the psml info, for displaying
46  * available entries and information on these entries
47  *
48  * @author <a HREF="mailto:david@apache.org">David Sean Taylor</a>
49  * @version $Id: PsmlBrowseAction.java,v 1.14 2004/02/23 02:56:58 jford Exp $
50  */

51 public class PsmlBrowseAction extends VelocityPortletAction
52 {
53
54     protected static final String JavaDoc PSML_REFRESH_FLAG = "psmlRefreshFlag";
55     protected static final String JavaDoc TRUE = "true";
56     protected static final String JavaDoc FALSE = "false";
57     protected static final String JavaDoc PROFILE_ITERATOR = "profileIterator";
58     protected static final String JavaDoc PAGE_SIZE = "page-size";
59     protected static final String JavaDoc CUSTOMIZE_TEMPLATE = "customize-template";
60     private static final String JavaDoc PEID = "js_peid";
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_USER = "filter_type_user";
73
74     /** value of the filter type parameter for searching by role */
75     public static final String JavaDoc FILTER_TYPE_ROLE = "filter_type_role";
76
77     /** value of the filter type parameter for searching by group */
78     public static final String JavaDoc FILTER_TYPE_GROUP = "filter_type_group";
79
80     /**
81      * Static initialization of the logger for this class
82      */

83     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PsmlBrowseAction.class.getName());
84     
85     /**
86      * Subclasses should override this method if they wish to
87      * provide their own customization behavior.
88      * Default is to use Portal base customizer action
89      */

90     protected void buildConfigureContext(VelocityPortlet portlet,
91                                          Context context,
92                                          RunData rundata)
93     {
94         try
95         {
96             super.buildConfigureContext(portlet, context, rundata);
97         }
98         catch (Exception JavaDoc ex)
99         {
100             logger.error("Exception", ex);
101         }
102         context.put(PAGE_SIZE, PortletConfigState.getParameter(portlet, rundata, PAGE_SIZE, "20"));
103         setTemplate(rundata, PortletConfigState.getParameter(portlet, rundata, CUSTOMIZE_TEMPLATE, null));
104     }
105     /**
106      * Subclasses must override this method to provide default behavior
107      * for the portlet action
108      */

109     protected void buildNormalContext(VelocityPortlet portlet,
110                                       Context context,
111                                       RunData rundata)
112     {
113         int start = rundata.getParameters().getInt("start", 0);
114
115         if (start < 0)
116         {
117             start = 0;
118         }
119
120         String JavaDoc pageSize = PortletConfigState.getParameter(portlet, rundata, PAGE_SIZE, "20");
121         int size = Integer.parseInt(pageSize);
122
123         int next = start + size + 1;
124         int prev = start - size - 1;
125
126         //System.out.println("start="+start+" size="+size+" next="+next+" prev="+prev);
127

128         //check to see if resultset has changed due to PsmlUpdateAction
129
//if so reconstruct the iterator and reset the flag
130

131         boolean refreshFlag = (rundata.getUser().getTemp(PSML_REFRESH_FLAG, FALSE)).equals(TRUE);
132         rundata.getUser().setTemp(PSML_REFRESH_FLAG, FALSE);
133         
134         //Get the iterator
135
DatabaseBrowserIterator windowIterator =
136             (DatabaseBrowserIterator) PortletSessionState.getAttribute(portlet, rundata, PROFILE_ITERATOR);
137         if ((windowIterator == null) || refreshFlag)
138         {
139             int index = 0;
140             QueryLocator ql = new QueryLocator(QueryLocator.QUERY_ALL);
141             ArrayList JavaDoc entries = new ArrayList JavaDoc();
142             Iterator i = Profiler.query(ql);
143
144             // Is filtering requested?
145
String JavaDoc filterValue = rundata.getParameters().getString(FILTER_VALUE);
146             if (filterValue != null && !filterValue.trim().equalsIgnoreCase(""))
147             {
148                 String JavaDoc filterType = rundata.getParameters().getString(FILTER_TYPE, FILTER_TYPE_USER);
149                 boolean useRE = rundata.getParameters().getBoolean(FILTER_REGEXP);
150                 RE r = null;
151                 RECompiler rc = null;
152                 if (useRE)
153                 {
154                     try
155                     {
156                         rc = new RECompiler();
157                         r = new RE();
158                         r.setProgram(rc.compile(filterValue));
159                     }
160                     catch (org.apache.regexp.RESyntaxException rex)
161                     {
162                         logger.warn("PsmlBrowseAction: error processing regular expression [" + filterValue + "]: " +
163                                  rex.toString());
164                     }
165                 }
166                 try
167                 {
168                     while (i.hasNext())
169                     {
170                         Profile profile = (Profile) i.next();
171                         String JavaDoc compareValue = null;
172                         if (filterType.equals(FILTER_TYPE_USER))
173                         {
174                             compareValue = profile.getUserName();
175                         }
176                         else if (filterType.equals(FILTER_TYPE_ROLE))
177                         {
178                             compareValue = profile.getRoleName(); }
179                         else if (filterType.equals(FILTER_TYPE_GROUP))
180                         {
181                             compareValue = profile.getGroupName();
182                         }
183
184                         if (compareValue != null)
185                         {
186                             if (useRE && r.match(compareValue))
187                             {
188                                 entries.add(profile);
189                             }
190                             else if (compareValue.startsWith(filterValue))
191                             {
192                                 entries.add(profile);
193                             }
194                         }
195                     }
196                 }
197                 catch (Exception JavaDoc e)
198                 {
199                     logger.error("Exception", e);
200                 }
201             }
202             else
203             {
204                 while (i.hasNext())
205                 {
206                     Profile profile = (Profile) i.next();
207                     //System.out.println("profile["+index+"]="+profile.getPath());
208
entries.add(profile);
209                     index++;
210                 }
211             }
212
213             ArrayList JavaDoc entryType = new ArrayList JavaDoc();
214             entryType.add("Profile");
215             windowIterator = new DatabaseBrowserIterator(entries, entryType, entryType, size);
216             PortletSessionState.setAttribute(portlet, rundata, PROFILE_ITERATOR, windowIterator);
217         }
218         else
219         {
220             windowIterator.setTop(start);
221         }
222         
223
224         if (windowIterator != null)
225         {
226             context.put("psml", windowIterator);
227             if (start > 0)
228             {
229                 context.put("prev", String.valueOf(prev + 1));
230             }
231             if (next <= windowIterator.getResultSetSize())
232             {
233                 context.put("next", String.valueOf(next - 1));
234             }
235
236         }
237         else
238         {
239             logger.error("No Psml entries Found");
240         }
241
242     }
243
244     /**
245      * This method is called when the user configures any of the parameters.
246      * @param data The turbine rundata context for this request.
247      * @param context The velocity context for this request.
248      */

249     public void doUpdate(RunData rundata, Context context)
250     {
251         String JavaDoc pageSize = null;
252
253         VelocityPortlet portlet = (VelocityPortlet) context.get("portlet");
254         if (portlet != null)
255         {
256             String JavaDoc peid = portlet.getID();
257             if ((peid != null)
258                 && peid.equals(rundata.getParameters().getString(PEID)))
259             {
260                 pageSize = rundata.getParameters().getString(PAGE_SIZE);
261             }
262             if (pageSize != null)
263             {
264                 PortletConfigState.setInstanceParameter(portlet, rundata, PAGE_SIZE, pageSize);
265                 PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR);
266             }
267         }
268
269         buildNormalContext(portlet, context, rundata);
270     }
271
272     /**
273      * This method is to refresh psml from disk or database.
274      * @param data The turbine rundata context for this request.
275      * @param context The velocity context for this request.
276      */

277     public void doRefresh(RunData rundata, Context context)
278     {
279         VelocityPortlet portlet = (VelocityPortlet) context.get("portlet");
280         PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR);
281         rundata.getParameters().remove(FILTER_VALUE);
282         buildNormalContext(portlet, context, rundata);
283     }
284
285     /**
286      * This method is to enter filtering mode.
287      * @param data The turbine rundata context for this request.
288      * @param context The velocity context for this request.
289      */

290     public void doFilter(RunData rundata, Context context)
291     {
292         VelocityPortlet portlet = (VelocityPortlet) context.get("portlet");
293         PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR);
294         buildNormalContext(portlet, context, rundata);
295     }
296
297 }
298
Popular Tags