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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.actions.portlets;
1819import java.util.ArrayList;
20import java.util.HashSet;
21import java.util.Iterator;
22import java.util.List;
23import java.util.TreeMap;
2425import org.apache.jetspeed.om.registry.PortletEntry;
26import org.apache.jetspeed.om.registry.base.BaseCategory;
27import org.apache.jetspeed.services.Registry;
28import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29import org.apache.jetspeed.services.logging.JetspeedLogger;
3031/***32 * An abstract class with helper methods for filtering Portlets.33 *34 * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>35 * @version $Id: PortletFilter.java,v 1.3 2004/02/23 02:56:58 jford Exp $36 */37publicabstractclassPortletFilter38 {
39/***40 * Static initialization of the logger for this class41 */42privatestaticfinalJetspeedLogger logger =
43 JetspeedLogFactoryService.getLogger(PortletFilter.class.getName());
4445/***46 * Method that filters a list of portlets based on a give filter name/value.47 * 48 * @param portlets List of portlets to filter49 * @param field The name of the filter50 * @param value The value of the filter51 * @return List of portlets that met the filter criteria52 */53publicstatic List filterPortlets(
54 List portlets,
55 String field,
56 String value)
57 {
58 String[] fields = { field };
59 String[] values = { value };
6061return filterPortlets(portlets, fields, values);
62 }
6364/***65 * Method that filters a list of portlets based on certain criteria.66 * 67 * @param portlets The list of portlets to filter68 * @param fields The list of fields69 * @param values The list of values. This should be in a 1:1 ratio with the fields.70 * @return List of portlets that met the filter criteria71 */72publicstatic List filterPortlets(
73 List portlets,
74 String[] fields,
75 String[] values)
76 {
77 List filteredPortlets = new ArrayList();
7879 Iterator portletIter = portlets.iterator();
80while (portletIter.hasNext())
81 {
82PortletEntry entry = (PortletEntry) portletIter.next();
83if (isFilteredIn(entry, fields, values))
84 {
85 filteredPortlets.add(entry);
86 }
87 }
8889return filteredPortlets;
90 }
9192/***93 * Method that checks a portlet entry to see if it matches the given94 * filter criteria.95 * 96 * @param entry The entry to filter97 * @param fields The list of fields.98 * @param values The list of values. This should be in a 1:1 ratio with the fields.99 * @return100 */101privatestaticboolean isFilteredIn(
102PortletEntry entry,
103 String[] fields,
104 String[] values)
105 {
106boolean result = true;
107108if (fields != null && values != null && fields.length == values.length)
109 {
110for (int i = 0; i < fields.length && result; i++)
111 {
112 String field = fields[i];
113 String value = values[i];
114115if (field == null116 || value == null117 || field.length() == 0
118 || value.length() == 0)
119 {
120//skip and add to list121 }
122elseif (field.equals("category"))
123 {
124 result = result && entry.hasCategory(value);
125 }
126elseif (field.equals("media_type"))
127 {
128 result = result && entry.hasMediaType(value);
129 }
130elseif (field.equals("parent"))
131 {
132if (entry.getParent() != null)
133 {
134 result = result && entry.getParent().equals(value);
135 }
136else137 {
138 result = false;
139 }
140 }
141elseif (field.equals("type"))
142 {
143if (entry.getType() != null)
144 {
145 result = result && entry.getType().equals(value);
146 }
147else148 {
149 result = false;
150 }
151 }
152/*153 else if(field.equals("permission"))154 {155 result = JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(), 156 new PortalResource(entry), 157 value);158 }159 */160else161 {
162 logger.warn("Invalid filter " + field + " attempted");
163 }
164 }
165 }
166167return result;
168 }
169170/***171 * Builds a list of all portlet categories172 * 173 * @param List portlets portlets to scan for categories174 * @return List of categories175 */176publicstatic List buildCategoryList(List portlets)
177 {
178 TreeMap catMap = new TreeMap();
179 Iterator pItr = portlets.iterator();
180while (pItr.hasNext())
181 {
182PortletEntry entry = (PortletEntry) pItr.next();
183184 Iterator cItr = entry.listCategories();
185while (cItr.hasNext())
186 {
187BaseCategory cat = (BaseCategory) cItr.next();
188 catMap.put(cat.getName(), cat);
189 }
190 }
191192returnnew ArrayList(catMap.values());
193 }
194195/***196 * Method to return all portlets in the Portlet Registry197 * 198 * @return List of portlets199 */200publicstatic List getAllPortlets()
201 {
202 List regEntries = new ArrayList();
203204 Iterator iter = Registry.get(Registry.PORTLET).listEntryNames();
205while (iter.hasNext())
206 {
207 String entryName = (String) iter.next();
208 regEntries.add(Registry.getEntry(Registry.PORTLET, entryName));
209 }
210211return regEntries;
212 }
213214/***215 * Method that returns a list of parents from the provided list of portlets.216 * 217 * @param portlets List of portlets to search for parents218 * @return List of portlets that are parents219 */220publicstatic List buildParentList(List portlets)
221 {
222 HashSet parentSet = new HashSet();
223224 Iterator portletIter = portlets.iterator();
225while (portletIter.hasNext())
226 {
227PortletEntry regEntry = (PortletEntry) portletIter.next();
228229 String regType = regEntry.getType();
230if (regType.equalsIgnoreCase(PortletEntry.TYPE_ABSTRACT))
231 {
232 parentSet.add(regEntry);
233 }
234 }
235236returnnew ArrayList(parentSet);
237 }
238239 }