jford 2003/11/02 21:25:31
Modified: src/java/org/apache/jetspeed/modules/actions/portlets
RegistryUpdateAction.java RegistryBrowseAction.java
Added: src/java/org/apache/jetspeed/modules/actions/portlets
PortletFilter.java PortletBrowseAction.java
Log:
Added ability to filter portlets by type, parent, media type, and category
in the portlet browser
PR: Bugzilla #24339
Revision Changes Path
1.8 +5 -1
jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/RegistryUpdateAction.java
Index: RegistryUpdateAction.java
===================================================================
RCS file:
/home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/RegistryUpdateAction.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- RegistryUpdateAction.java 12 Sep 2003 14:38:18 -0000 1.7
+++ RegistryUpdateAction.java 3 Nov 2003 05:25:31 -0000 1.8
@@ -172,6 +172,8 @@
Registry.addEntry(registry, registryEntry);
clearUserData(rundata);
+
+ rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry +
":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
}
}
catch (EntityExistsException e)
@@ -288,6 +290,8 @@
{
Registry.removeEntry(registry, entryName);
clearUserData(rundata);
+
+ rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry +
":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
}
}
catch (Exception e)
1.5 +139 -33
jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/RegistryBrowseAction.java
Index: RegistryBrowseAction.java
===================================================================
RCS file:
/home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/RegistryBrowseAction.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RegistryBrowseAction.java 4 Mar 2003 00:04:53 -0000 1.4
+++ RegistryBrowseAction.java 3 Nov 2003 05:25:31 -0000 1.5
@@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -54,38 +54,58 @@
package org.apache.jetspeed.modules.actions.portlets;
-import org.apache.jetspeed.portal.portlets.VelocityPortlet;
-import org.apache.jetspeed.services.Registry;
-import org.apache.jetspeed.om.registry.RegistryEntry;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
-// Turbine stuff
+import org.apache.jetspeed.om.registry.RegistryEntry;
+import org.apache.jetspeed.portal.Portlet;
+import org.apache.jetspeed.services.Registry;
+import org.apache.jetspeed.util.PortletSessionState;
import org.apache.turbine.util.RunData;
-
-// Velocity Stuff
import org.apache.velocity.context.Context;
-import java.util.Vector;
-import java.util.Iterator;
-
/**
* This action enables to browse any of the system registries for displaying
* available entries and information on these entries
*
* @author <a href="mailto:[EMAIL PROTECTED]">Rapha�l Luta</a>
+ * @version $ID$
*/
-public class RegistryBrowseAction extends VelocityPortletAction
+public class RegistryBrowseAction extends GenericMVCAction
{
+ public static final String PREFIX = "RegistryBrowseAction:";
+
+ public static final String REFRESH = "refresh";
+ public static final String FILTER_FIELDS = "filter_fields";
+ public static final String FILTER_VALUES = "filter_values";
+
+ public static final String START = "start";
+ public static final String RESULTS = "results";
+ public static final String FILTERED_RESULTS = "filtered_results";
/**
* Subclasses must override this method to provide default behavior
* for the portlet action
*/
- protected void buildNormalContext( VelocityPortlet portlet,
+ protected void buildNormalContext( Portlet portlet,
Context context,
RunData rundata )
{
- int start = rundata.getParameters().getInt("start",0);
+ String regName = portlet.getPortletConfig()
+ .getInitParameter("registry",Registry.PORTLET);
+
+ Boolean refresh = (Boolean)PortletSessionState.getAttribute(rundata, PREFIX
+ regName + ":" + REFRESH, Boolean.FALSE);
+ if(refresh.equals(Boolean.TRUE))
+ {
+ PortletSessionState.clearAttribute(portlet, rundata, START);
+ PortletSessionState.clearAttribute(portlet, rundata, RESULTS);
+ PortletSessionState.clearAttribute(portlet, rundata, FILTERED_RESULTS);
+ PortletSessionState.clearAttribute(rundata, PREFIX + regName + ":" +
REFRESH);
+ }
+
+ int start = getStart(rundata, portlet);
if (start < 0) start = 0;
String pageSize = portlet.getPortletConfig()
@@ -95,40 +115,126 @@
int next = start+size+1;
int prev = start-size-1;
-
- String regName = portlet.getPortletConfig()
- .getInitParameter("registry",Registry.PORTLET);
-
- Iterator i = Registry.get(regName).listEntryNames();
- Vector entries = new Vector();
- int count = 0;
-
- while(i.hasNext() && (count < next) )
+ String[] filterFields = (String[])
PortletSessionState.getAttribute(portlet, rundata, FILTER_FIELDS);
+ String[] filterValues = (String[])
PortletSessionState.getAttribute(portlet, rundata, FILTER_VALUES);
+
+
+ List regEntries = (List)PortletSessionState.getAttribute(portlet, rundata,
RESULTS);
+ List filteredEntries = (List)PortletSessionState.getAttribute(portlet,
rundata, FILTERED_RESULTS);
+ if(regEntries == null)
{
- String name = (String)i.next();
-
- RegistryEntry regEntry = Registry.getEntry(regName,name);
+ Iterator i = Registry.get(regName).listEntryNames();
+ regEntries = new ArrayList();
- if ( (regEntry!=null) && (!regEntry.isHidden()) )
+ while(i.hasNext())
{
- if (count >= start)
+ String name = (String)i.next();
+
+ RegistryEntry regEntry = Registry.getEntry(regName,name);
+
+ if ( (regEntry!=null) && (!regEntry.isHidden()) )
{
- entries.add(regEntry);
+ regEntries.add(regEntry);
}
- count++;
}
+
+ PortletSessionState.setAttribute(portlet, rundata, RESULTS, regEntries);
+
+ filteredEntries = filter(regEntries, filterFields, filterValues);
+ PortletSessionState.setAttribute(portlet, rundata, FILTERED_RESULTS,
filteredEntries);
+ }
+
+ if(filterFields != null && filterValues != null && filterFields.length ==
filterValues.length)
+ {
+ for(int i=0; i<filterFields.length; i++)
+ {
+ String field = filterFields[i];
+ String value = filterValues[i];
+
+ context.put(field + "_filter_value", value);
+ }
+ }
+
+ int end = start+size;
+ if(end> filteredEntries.size())
+ {
+ end = filteredEntries.size();
}
-
- context.put("registry", entries);
+ List pageEntries = filteredEntries.subList(start, end);
+
+ context.put("registry", pageEntries);
+ context.put("filtered_entried", filteredEntries);
if (start > 0)
{
context.put("prev",String.valueOf(prev));
}
- if ( (count == next)
- && ( count < Registry.get(regName).getEntryCount() ) )
+ if (next < filteredEntries.size())
{
context.put("next",String.valueOf(next));
}
+ }
+
+ /**
+ * @param rundata The turbine rundata context for this request.
+ * @param portlet The portlet
+ * @return The value of the start variable
+ */
+ private int getStart(RunData rundata, Portlet portlet)
+ {
+ int start = 0;
+ Integer startInteger = rundata.getParameters().getInteger(START, -1);
+
+ if(startInteger.intValue() == -1) {
+ startInteger = (Integer) PortletSessionState.getAttribute(portlet,
rundata, START);
+ if(startInteger != null) {
+ start = startInteger.intValue();
+ }
+ } else {
+ PortletSessionState.setAttribute(portlet, rundata, START, startInteger);
+ start = startInteger.intValue();
+ }
+
+ return start;
+ }
+
+ /**
+ * Adds a filter over the available portlets list based on category
+ *
+ * @param rundata The turbine rundata context for this request.
+ * @param context The velocity context for this request.
+ */
+ public void doFilter(RunData rundata, Context context) throws Exception
+ {
+ String[] filterFields = rundata.getParameters().getStrings("filter_field");
+ String[] filterValues = new String[filterFields.length];
+ for(int i=0; i<filterFields.length; i++)
+ {
+ String filterField = filterFields[i];
+ String filterValue = rundata.getParameters().getString(filterField +
":filter_value");
+ filterValues[i] = filterValue;
+ }
+
+ String regName = getPortlet(context).getPortletConfig()
+
.getInitParameter("registry",Registry.PORTLET);
+
+ PortletSessionState.setAttribute(getPortlet(context), rundata,
FILTER_FIELDS, filterFields);
+ PortletSessionState.setAttribute(getPortlet(context), rundata,
FILTER_VALUES, filterValues);
+ PortletSessionState.setAttribute(rundata, PREFIX + regName + ":" + REFRESH,
Boolean.TRUE);
+ }
+
+
+ /**
+ * Method that filters the registry entries. This should be overridden in
+ * child classes to determine what filters each browser will support. By
+ * default, this implemenation does no filtering.
+ *
+ * @param entries The list of registry entries to filter.
+ * @param fields The array of filter names
+ * @param values The array of filter values. This should be in a 1:1 ratio
with the fitler names.
+ * @return The list of filtered portlets.
+ */
+ protected List filter(List entries, String[] fields, String[] values) {
+ return entries;
}
}
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/PortletFilter.java
Index: PortletFilter.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.modules.actions.portlets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.registry.base.BaseCategory;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
/**
* An abstract class with helper methods for filtering Portlets.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jeremy Ford</a>
* @version $Id: PortletFilter.java,v 1.1 2003/11/03 05:25:31 jford Exp $
*/
public abstract class PortletFilter
{
/**
* Static initialization of the logger for this class
*/
private static final JetspeedLogger logger =
JetspeedLogFactoryService.getLogger(GenericMVCAction.class.getName());
/**
* Method that filters a list of portlets based on a give filter name/value.
*
* @param portlets List of portlets to filter
* @param field The name of the filter
* @param value The value of the filter
* @return List of portlets that met the filter criteria
*/
public static List filterPortlets(
List portlets,
String field,
String value)
{
String[] fields = { field };
String[] values = { value };
return filterPortlets(portlets, fields, values);
}
/**
* Method that filters a list of portlets based on certain criteria.
*
* @param portlets The list of portlets to filter
* @param fields The list of fields
* @param values The list of values. This should be in a 1:1 ratio with the
fields.
* @return List of portlets that met the filter criteria
*/
public static List filterPortlets(
List portlets,
String[] fields,
String[] values)
{
List filteredPortlets = new ArrayList();
Iterator portletIter = portlets.iterator();
while (portletIter.hasNext())
{
PortletEntry entry = (PortletEntry) portletIter.next();
if (isFilteredIn(entry, fields, values))
{
filteredPortlets.add(entry);
}
}
return filteredPortlets;
}
/**
* Method that checks a portlet entry to see if it matches the given
* filter criteria.
*
* @param entry The entry to filter
* @param fields The list of fields.
* @param values The list of values. This should be in a 1:1 ratio with the
fields.
* @return
*/
private static boolean isFilteredIn(
PortletEntry entry,
String[] fields,
String[] values)
{
boolean result = true;
if (fields != null && values != null && fields.length == values.length)
{
for (int i = 0; i < fields.length && result; i++)
{
String field = fields[i];
String value = values[i];
if (field == null
|| value == null
|| field.length() == 0
|| value.length() == 0)
{
//skip and add to list
}
else if (field.equals("category"))
{
result = result && entry.hasCategory(value);
}
else if (field.equals("media_type"))
{
result = result && entry.hasMediaType(value);
}
else if (field.equals("parent"))
{
if (entry.getParent() != null)
{
result = result && entry.getParent().equals(value);
}
else
{
result = false;
}
}
else if (field.equals("type"))
{
if (entry.getType() != null)
{
result = result && entry.getType().equals(value);
}
else
{
result = false;
}
}
/*
else if(field.equals("permission"))
{
result = JetspeedSecurity.checkPermission((JetspeedUser)
rundata.getUser(),
new
PortalResource(entry),
value);
}
*/
else
{
logger.warn("Invalid filter " + field + " attempted");
}
}
}
return result;
}
/**
* Builds a list of all portlet categories
*
* @param List portlets portlets to scan for categories
* @return List of categories
*/
public static List buildCategoryList(List portlets)
{
TreeMap catMap = new TreeMap();
Iterator pItr = portlets.iterator();
while (pItr.hasNext())
{
PortletEntry entry = (PortletEntry) pItr.next();
Iterator cItr = entry.listCategories();
while (cItr.hasNext())
{
BaseCategory cat = (BaseCategory) cItr.next();
catMap.put(cat.getName(), cat);
}
}
return new ArrayList(catMap.values());
}
/**
* Method to return all portlets in the Portlet Registry
*
* @return List of portlets
*/
public static List getAllPortlets()
{
List regEntries = new ArrayList();
Iterator iter = Registry.get(Registry.PORTLET).listEntryNames();
while (iter.hasNext())
{
String entryName = (String) iter.next();
regEntries.add(Registry.getEntry(Registry.PORTLET, entryName));
}
return regEntries;
}
/**
* Method that returns a list of parents from the provided list of portlets.
*
* @param portlets List of portlets to search for parents
* @return List of portlets that are parents
*/
public static List buildParentList(List portlets)
{
HashSet parentSet = new HashSet();
Iterator portletIter = portlets.iterator();
while (portletIter.hasNext())
{
PortletEntry regEntry = (PortletEntry) portletIter.next();
String regType = regEntry.getType();
if (regType.equalsIgnoreCase(PortletEntry.TYPE_ABSTRACT))
{
parentSet.add(regEntry);
}
}
return new ArrayList(parentSet);
}
}
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/PortletBrowseAction.java
Index: PortletBrowseAction.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.modules.actions.portlets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.jetspeed.om.registry.MediaTypeRegistry;
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.util.PortletSessionState;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;
/**
* This action extends the RegistryBrowseAction to provide filtering for portlets
* and add extra information into the context
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jeremy Ford</a>
* @version $Id: PortletBrowseAction.java,v 1.1 2003/11/03 05:25:31 jford Exp $
*/
public class PortletBrowseAction extends RegistryBrowseAction
{
/**
* Subclasses must override this method to provide default behavior
* for the portlet action
*/
/**
* Build the normal state content for this portlet.
*
* @param portlet The velocity-based portlet that is being built.
* @param context The velocity context for this request.
* @param rundata The turbine rundata context for this request.
*/
protected void buildNormalContext(
Portlet portlet,
Context context,
RunData rundata)
{
super.buildNormalContext(portlet, context, rundata);
List portlets =
(List) PortletSessionState.getAttribute(portlet, rundata, RESULTS);
List categories = PortletFilter.buildCategoryList(portlets);
context.put("categories", categories);
MediaTypeRegistry mediaTypeReg =
(MediaTypeRegistry) Registry.get(Registry.MEDIA_TYPE);
ArrayList collection = new ArrayList();
Iterator iter = mediaTypeReg.listEntryNames();
while (iter.hasNext())
{
collection.add(iter.next());
}
context.put("media_types", collection);
context.put("parents", PortletFilter.buildParentList(portlets));
}
/**
* Filter portlets by using the PortletFilter helper class
*
* @see
org.apache.jetspeed.modules.actions.portlets.RegistryBrowseAction#filter(java.util.List,
java.lang.String[], java.lang.String[])
*/
protected List filter(List entries, String[] fields, String[] values)
{
return PortletFilter.filterPortlets(entries, fields, values);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]