KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2000-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 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.jetspeed.om.registry.RegistryEntry;
26 import org.apache.jetspeed.portal.Portlet;
27 import org.apache.jetspeed.services.Registry;
28 import org.apache.jetspeed.util.PortletSessionState;
29 import org.apache.turbine.util.RunData;
30 import org.apache.velocity.context.Context;
31
32 /**
33  * This action enables to browse any of the system registries for displaying
34  * available entries and information on these entries
35  *
36  * @author <a HREF="mailto:raphael@apache.org">Raphaël Luta</a>
37  * @version $ID$
38  */

39 public class RegistryBrowseAction extends GenericMVCAction
40 {
41     public static final String JavaDoc PREFIX = "RegistryBrowseAction:";
42     
43     public static final String JavaDoc REFRESH = "refresh";
44     public static final String JavaDoc FILTER_FIELDS = "filter_fields";
45     public static final String JavaDoc FILTER_VALUES = "filter_values";
46     
47     public static final String JavaDoc START = "start";
48     public static final String JavaDoc RESULTS = "results";
49     public static final String JavaDoc FILTERED_RESULTS = "filtered_results";
50
51     /**
52      * Subclasses must override this method to provide default behavior
53      * for the portlet action
54      */

55     protected void buildNormalContext( Portlet portlet,
56                                        Context context,
57                                        RunData rundata )
58     {
59         String JavaDoc regName = portlet.getPortletConfig()
60                                 .getInitParameter("registry",Registry.PORTLET);
61         
62         Boolean JavaDoc refresh = (Boolean JavaDoc)PortletSessionState.getAttribute(rundata, PREFIX + regName + ":" + REFRESH, Boolean.FALSE);
63         
64         if(refresh.equals(Boolean.TRUE))
65         {
66             PortletSessionState.clearAttribute(portlet, rundata, START);
67             PortletSessionState.clearAttribute(portlet, rundata, RESULTS);
68             PortletSessionState.clearAttribute(portlet, rundata, FILTERED_RESULTS);
69             PortletSessionState.clearAttribute(rundata, PREFIX + regName + ":" + REFRESH);
70         }
71         
72         int start = getStart(rundata, portlet);
73         if (start < 0) start = 0;
74
75         String JavaDoc pageSize = portlet.getPortletConfig()
76                                  .getInitParameter("page-size","20");
77
78         int size = Integer.parseInt(pageSize);
79
80         int prev = start-size;
81         
82         if(prev < 0)
83         {
84             prev = 0;
85         }
86         
87         String JavaDoc[] filterFields = (String JavaDoc[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_FIELDS);
88         String JavaDoc[] filterValues = (String JavaDoc[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_VALUES);
89         
90                                 
91         List JavaDoc regEntries = (List JavaDoc)PortletSessionState.getAttribute(portlet, rundata, RESULTS);
92         List JavaDoc filteredEntries = (List JavaDoc)PortletSessionState.getAttribute(portlet, rundata, FILTERED_RESULTS);
93         if(regEntries == null)
94         {
95             Iterator JavaDoc i = Registry.get(regName).listEntryNames();
96             regEntries = new ArrayList JavaDoc();
97     
98             while(i.hasNext())
99             {
100                 String JavaDoc name = (String JavaDoc)i.next();
101                 
102                 RegistryEntry regEntry = Registry.getEntry(regName,name);
103         
104                 if ( (regEntry!=null) && (!regEntry.isHidden()) )
105                 {
106                     regEntries.add(regEntry);
107                 }
108             }
109
110             Collections.sort(regEntries,
111                 new Comparator JavaDoc() {
112                     public int compare(Object JavaDoc o1, Object JavaDoc o2)
113                     {
114                         String JavaDoc t1 = ((RegistryEntry) o1).getName().toLowerCase();
115                         String JavaDoc t2 = ((RegistryEntry) o2).getName().toLowerCase();
116                                       
117                         return t1.compareTo(t2);
118                     }
119                 });
120             
121             PortletSessionState.setAttribute(portlet, rundata, RESULTS, regEntries);
122             
123             filteredEntries = filter(regEntries, filterFields, filterValues);
124             PortletSessionState.setAttribute(portlet, rundata, FILTERED_RESULTS, filteredEntries);
125         }
126         
127         if(filterFields != null && filterValues != null && filterFields.length == filterValues.length)
128         {
129             for(int i=0; i<filterFields.length; i++)
130             {
131                 String JavaDoc field = filterFields[i];
132                 String JavaDoc value = filterValues[i];
133                 
134                 context.put(field + "_filter_value", value);
135             }
136         }
137         
138         int end = start+size;
139         if(end> filteredEntries.size())
140         {
141             end = filteredEntries.size();
142         }
143         List JavaDoc pageEntries = filteredEntries.subList(start, end);
144
145         context.put("registry", pageEntries);
146         context.put("filtered_entries", filteredEntries);
147         if (start > 0)
148         {
149             context.put("prev",String.valueOf(prev));
150         }
151         if (end < filteredEntries.size())
152         {
153             context.put("next",String.valueOf(end));
154         }
155     }
156     
157     /**
158      * @param rundata The turbine rundata context for this request.
159      * @param portlet The portlet
160      * @return The value of the start variable
161      */

162     private int getStart(RunData rundata, Portlet portlet)
163     {
164         int start = 0;
165         Integer JavaDoc startInteger = rundata.getParameters().getInteger(START, -1);
166         
167         if(startInteger.intValue() == -1) {
168             startInteger = (Integer JavaDoc) PortletSessionState.getAttribute(portlet, rundata, START);
169             if(startInteger != null) {
170                 start = startInteger.intValue();
171             }
172         } else {
173             PortletSessionState.setAttribute(portlet, rundata, START, startInteger);
174             start = startInteger.intValue();
175         }
176         
177         return start;
178     }
179     
180     /**
181      * Adds a filter over the available portlets list based on category
182      *
183      * @param rundata The turbine rundata context for this request.
184      * @param context The velocity context for this request.
185      */

186     public void doFilter(RunData rundata, Context context) throws Exception JavaDoc
187     {
188         String JavaDoc[] filterFields = rundata.getParameters().getStrings("filter_field");
189         String JavaDoc[] filterValues = new String JavaDoc[filterFields.length];
190         for(int i=0; i<filterFields.length; i++)
191         {
192             String JavaDoc filterField = filterFields[i];
193             String JavaDoc filterValue = rundata.getParameters().getString(filterField + ":filter_value");
194             filterValues[i] = filterValue;
195         }
196         
197         String JavaDoc regName = getPortlet(context).getPortletConfig()
198                                         .getInitParameter("registry",Registry.PORTLET);
199         
200         PortletSessionState.setAttribute(getPortlet(context), rundata, FILTER_FIELDS, filterFields);
201         PortletSessionState.setAttribute(getPortlet(context), rundata, FILTER_VALUES, filterValues);
202         PortletSessionState.setAttribute(rundata, PREFIX + regName + ":" + REFRESH, Boolean.TRUE);
203     }
204     
205     
206     /**
207      * Method that filters the registry entries. This should be overridden in
208      * child classes to determine what filters each browser will support. By
209      * default, this implemenation does no filtering.
210      *
211      * @param entries The list of registry entries to filter.
212      * @param fields The array of filter names
213      * @param values The array of filter values. This should be in a 1:1 ratio with the fitler names.
214      * @return The list of filtered portlets.
215      */

216     protected List JavaDoc filter(List JavaDoc entries, String JavaDoc[] fields, String JavaDoc[] values) {
217        return entries;
218     }
219 }
220
Popular Tags