KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 import org.apache.jetspeed.om.registry.PortletEntry;
26 import org.apache.jetspeed.om.registry.base.BaseCategory;
27 import org.apache.jetspeed.services.Registry;
28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29 import org.apache.jetspeed.services.logging.JetspeedLogger;
30
31 /**
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  */

37 public abstract class PortletFilter
38 {
39     /**
40     * Static initialization of the logger for this class
41     */

42     private static final JetspeedLogger logger =
43         JetspeedLogFactoryService.getLogger(PortletFilter.class.getName());
44
45     /**
46      * Method that filters a list of portlets based on a give filter name/value.
47      *
48      * @param portlets List of portlets to filter
49      * @param field The name of the filter
50      * @param value The value of the filter
51      * @return List of portlets that met the filter criteria
52      */

53     public static List JavaDoc filterPortlets(
54         List JavaDoc portlets,
55         String JavaDoc field,
56         String JavaDoc value)
57     {
58         String JavaDoc[] fields = { field };
59         String JavaDoc[] values = { value };
60
61         return filterPortlets(portlets, fields, values);
62     }
63
64     /**
65      * Method that filters a list of portlets based on certain criteria.
66      *
67      * @param portlets The list of portlets to filter
68      * @param fields The list of fields
69      * @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 criteria
71      */

72     public static List JavaDoc filterPortlets(
73         List JavaDoc portlets,
74         String JavaDoc[] fields,
75         String JavaDoc[] values)
76     {
77         List JavaDoc filteredPortlets = new ArrayList JavaDoc();
78
79         Iterator JavaDoc portletIter = portlets.iterator();
80         while (portletIter.hasNext())
81         {
82             PortletEntry entry = (PortletEntry) portletIter.next();
83             if (isFilteredIn(entry, fields, values))
84             {
85                 filteredPortlets.add(entry);
86             }
87         }
88
89         return filteredPortlets;
90     }
91
92     /**
93      * Method that checks a portlet entry to see if it matches the given
94      * filter criteria.
95      *
96      * @param entry The entry to filter
97      * @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      * @return
100      */

101     private static boolean isFilteredIn(
102         PortletEntry entry,
103         String JavaDoc[] fields,
104         String JavaDoc[] values)
105     {
106         boolean result = true;
107
108         if (fields != null && values != null && fields.length == values.length)
109         {
110             for (int i = 0; i < fields.length && result; i++)
111             {
112                 String JavaDoc field = fields[i];
113                 String JavaDoc value = values[i];
114
115                 if (field == null
116                     || value == null
117                     || field.length() == 0
118                     || value.length() == 0)
119                 {
120                     //skip and add to list
121
}
122                 else if (field.equals("category"))
123                 {
124                     result = result && entry.hasCategory(value);
125                 }
126                 else if (field.equals("media_type"))
127                 {
128                     result = result && entry.hasMediaType(value);
129                 }
130                 else if (field.equals("parent"))
131                 {
132                     if (entry.getParent() != null)
133                     {
134                         result = result && entry.getParent().equals(value);
135                     }
136                     else
137                     {
138                         result = false;
139                     }
140                 }
141                 else if (field.equals("type"))
142                 {
143                     if (entry.getType() != null)
144                     {
145                         result = result && entry.getType().equals(value);
146                     }
147                     else
148                     {
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                 */

160                 else
161                 {
162                     logger.warn("Invalid filter " + field + " attempted");
163                 }
164             }
165         }
166
167         return result;
168     }
169
170     /**
171      * Builds a list of all portlet categories
172      *
173      * @param List portlets portlets to scan for categories
174      * @return List of categories
175      */

176     public static List JavaDoc buildCategoryList(List JavaDoc portlets)
177     {
178         TreeMap JavaDoc catMap = new TreeMap JavaDoc();
179         Iterator JavaDoc pItr = portlets.iterator();
180         while (pItr.hasNext())
181         {
182             PortletEntry entry = (PortletEntry) pItr.next();
183
184             Iterator JavaDoc cItr = entry.listCategories();
185             while (cItr.hasNext())
186             {
187                 BaseCategory cat = (BaseCategory) cItr.next();
188                 catMap.put(cat.getName(), cat);
189             }
190         }
191
192         return new ArrayList JavaDoc(catMap.values());
193     }
194
195     /**
196      * Method to return all portlets in the Portlet Registry
197      *
198      * @return List of portlets
199      */

200     public static List JavaDoc getAllPortlets()
201     {
202         List JavaDoc regEntries = new ArrayList JavaDoc();
203
204         Iterator JavaDoc iter = Registry.get(Registry.PORTLET).listEntryNames();
205         while (iter.hasNext())
206         {
207             String JavaDoc entryName = (String JavaDoc) iter.next();
208             regEntries.add(Registry.getEntry(Registry.PORTLET, entryName));
209         }
210
211         return regEntries;
212     }
213
214     /**
215      * Method that returns a list of parents from the provided list of portlets.
216      *
217      * @param portlets List of portlets to search for parents
218      * @return List of portlets that are parents
219      */

220     public static List JavaDoc buildParentList(List JavaDoc portlets)
221     {
222         HashSet JavaDoc parentSet = new HashSet JavaDoc();
223
224         Iterator JavaDoc portletIter = portlets.iterator();
225         while (portletIter.hasNext())
226         {
227             PortletEntry regEntry = (PortletEntry) portletIter.next();
228
229             String JavaDoc regType = regEntry.getType();
230             if (regType.equalsIgnoreCase(PortletEntry.TYPE_ABSTRACT))
231             {
232                 parentSet.add(regEntry);
233             }
234         }
235
236         return new ArrayList JavaDoc(parentSet);
237     }
238
239 }
240
Popular Tags