View Javadoc

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;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.TreeMap;
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 filterPortlets(
54          List portlets,
55          String field,
56          String value)
57      {
58          String[] fields = { field };
59          String[] 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 filterPortlets(
73          List portlets,
74          String[] fields,
75          String[] values)
76      {
77          List filteredPortlets = new ArrayList();
78  
79          Iterator 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[] fields,
104         String[] 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 field = fields[i];
113                 String 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 buildCategoryList(List portlets)
177     {
178         TreeMap catMap = new TreeMap();
179         Iterator pItr = portlets.iterator();
180         while (pItr.hasNext())
181         {
182             PortletEntry entry = (PortletEntry) pItr.next();
183 
184             Iterator 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(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 getAllPortlets()
201     {
202         List regEntries = new ArrayList();
203 
204         Iterator iter = Registry.get(Registry.PORTLET).listEntryNames();
205         while (iter.hasNext())
206         {
207             String entryName = (String) 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 buildParentList(List portlets)
221     {
222         HashSet parentSet = new HashSet();
223 
224         Iterator portletIter = portlets.iterator();
225         while (portletIter.hasNext())
226         {
227             PortletEntry regEntry = (PortletEntry) portletIter.next();
228 
229             String regType = regEntry.getType();
230             if (regType.equalsIgnoreCase(PortletEntry.TYPE_ABSTRACT))
231             {
232                 parentSet.add(regEntry);
233             }
234         }
235 
236         return new ArrayList(parentSet);
237     }
238 
239 }