1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.portlets;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.List;
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 PREFIX = "RegistryBrowseAction:";
42
43 public static final String REFRESH = "refresh";
44 public static final String FILTER_FIELDS = "filter_fields";
45 public static final String FILTER_VALUES = "filter_values";
46
47 public static final String START = "start";
48 public static final String RESULTS = "results";
49 public static final String 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 regName = portlet.getPortletConfig()
60 .getInitParameter("registry",Registry.PORTLET);
61
62 Boolean refresh = (Boolean)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 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[] filterFields = (String[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_FIELDS);
88 String[] filterValues = (String[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_VALUES);
89
90
91 List regEntries = (List)PortletSessionState.getAttribute(portlet, rundata, RESULTS);
92 List filteredEntries = (List)PortletSessionState.getAttribute(portlet, rundata, FILTERED_RESULTS);
93 if(regEntries == null)
94 {
95 Iterator i = Registry.get(regName).listEntryNames();
96 regEntries = new ArrayList();
97
98 while(i.hasNext())
99 {
100 String name = (String)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() {
112 public int compare(Object o1, Object o2)
113 {
114 String t1 = ((RegistryEntry) o1).getName().toLowerCase();
115 String 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 field = filterFields[i];
132 String 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 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 startInteger = rundata.getParameters().getInteger(START, -1);
166
167 if(startInteger.intValue() == -1) {
168 startInteger = (Integer) 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
187 {
188 String[] filterFields = rundata.getParameters().getStrings("filter_field");
189 String[] filterValues = new String[filterFields.length];
190 for(int i=0; i<filterFields.length; i++)
191 {
192 String filterField = filterFields[i];
193 String filterValue = rundata.getParameters().getString(filterField + ":filter_value");
194 filterValues[i] = filterValue;
195 }
196
197 String 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 filter(List entries, String[] fields, String[] values) {
217 return entries;
218 }
219 }