portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r888004 - in /portals/jetspeed-2/applications/j2-admin/trunk/src/main: java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java webapp/WEB-INF/view/toolbox/toolbox.jsp
Date Mon, 07 Dec 2009 17:04:02 GMT
Author: woonsan
Date: Mon Dec  7 17:04:01 2009
New Revision: 888004

URL: http://svn.apache.org/viewvc?rev=888004&view=rev
Log:
JS2-1057: Code clean up. Removing unused codes.

Modified:
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/toolbox/toolbox.jsp

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java?rev=888004&r1=888003&r2=888004&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/toolbox/JetspeedToolbox.java
Mon Dec  7 17:04:01 2009
@@ -17,17 +17,8 @@
 package org.apache.jetspeed.portlets.toolbox;
 
 import java.io.IOException;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
+import java.util.Arrays;
 import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Random;
-import java.util.StringTokenizer;
 
 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
@@ -35,12 +26,12 @@
 import javax.portlet.PortletConfig;
 import javax.portlet.PortletContext;
 import javax.portlet.PortletException;
-import javax.portlet.PortletPreferences;
 import javax.portlet.PortletRequest;
-import javax.portlet.PortletSession;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.math.NumberUtils;
 import org.apache.jetspeed.CommonPortletServices;
 import org.apache.jetspeed.JetspeedActions;
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
@@ -48,16 +39,9 @@
 import org.apache.jetspeed.om.page.ContentFragment;
 import org.apache.jetspeed.om.page.ContentPage;
 import org.apache.jetspeed.om.page.Fragment;
-import org.apache.jetspeed.om.page.Page;
-import org.apache.jetspeed.om.portlet.InitParam;
-import org.apache.jetspeed.om.portlet.LocalizedField;
-import org.apache.jetspeed.om.portlet.PortletApplication;
-import org.apache.jetspeed.om.portlet.PortletDefinition;
 import org.apache.jetspeed.portlet.HeaderPhaseSupportConstants;
-import org.apache.jetspeed.portlets.PortletInfo;
 import org.apache.jetspeed.profiler.Profiler;
 import org.apache.jetspeed.request.RequestContext;
-import org.apache.jetspeed.search.ParsedObject;
 import org.apache.jetspeed.search.SearchEngine;
 import org.apache.jetspeed.security.SecurityAccessController;
 import org.apache.portals.bridges.common.GenericServletPortlet;
@@ -71,7 +55,7 @@
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  * @version $Id$
  */
-public class JetspeedToolbox extends GenericServletPortlet implements Comparator
+public class JetspeedToolbox extends GenericServletPortlet
 {
     static Logger log = LoggerFactory.getLogger(JetspeedToolbox.class);
     
@@ -80,23 +64,14 @@
     protected Profiler profiler;
     protected SecurityAccessController securityAccessController;
     protected DecorationFactory decorationFactory;    
-    protected Random rand;
     protected String yuiScriptPath = "/javascript/yui/build/yui/yui-min.js";
     
-    public final String[] DEFAULT_IMAGES = new String[]
-                                                      {
-                                                              "images/portlets/applications-development.png",
-                                                              "images/portlets/applications-system.png",
-                                                              "images/portlets/applications-other.png",
-                                                              "images/portlets/linux.png"
-                                                      };
-    public final static String PORTLET_ICON = "portlet-icon";
-    
-
     public void init(PortletConfig config) throws PortletException
     {
         super.init(config);
-        PortletContext context = getPortletContext();                
+        
+        PortletContext context = getPortletContext();
+        
         registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
         if (null == registry)
         {
@@ -122,7 +97,6 @@
         {
             throw new PortletException("Failed to find the Decoration Factory on portlet
initialization");
         }        
-        rand = new Random( 19580427 );
         
         String param = config.getInitParameter("yuiScriptPath");
         
@@ -145,33 +119,27 @@
     }
     
     @Override
-    @SuppressWarnings("unchecked")
     public void doView(RenderRequest request, RenderResponse response)
             throws PortletException, IOException
     {
-        String category = request.getParameter("category");
-        String filter = request.getParameter("filter");
-        if (filter == null ||  filter.equals("")) filter = null;       
-        if (category == null || category.equals("")) category = "All";
-        RequestContext requestContext = (RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV);
-        
-        //TODO: The following two lines need to be move somewhere like 'serveResource()'
-        //      because portlets info should be retrieved from ajax calls.
-        //List<PortletInfo> portlets = retrievePortlets(request, null);
-        //request.setAttribute("portlets", portlets);
-        
         request.setAttribute("categories", retrieveCategories(request));
         request.setAttribute("layouts", LayoutBean.retrieveLayouts(request, decorationFactory));
         request.setAttribute("themes", ThemeBean.retrieveThemes(request, decorationFactory));
+        
         boolean hasEditAccess = false;
+        
         try
         {
+            RequestContext requestContext = (RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV);
             requestContext.getPage().checkAccess(JetspeedActions.EDIT);
             hasEditAccess = true;
         }
-        catch(Exception e)
-        {}
-        request.setAttribute("editAccess", new Boolean(hasEditAccess));
+        catch(Exception ignore)
+        {
+        }
+        
+        request.setAttribute("editAccess", hasEditAccess ? Boolean.TRUE : Boolean.FALSE);
+        
         super.doView(request, response);        
     }
     
@@ -179,32 +147,14 @@
     public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
throws PortletException,
     IOException
     {
-        String category = null;
-        String filter = actionRequest.getParameter("filter");
         String theme =  actionRequest.getParameter("theme");
         String layout =  actionRequest.getParameter("layout");
-        RequestContext requestContext = (RequestContext) actionRequest.getAttribute(RequestContext.REQUEST_PORTALENV);
                   
-        if (filter != null)
-        {
-            if (filter.trim().equals(""))
-            {
-                filter = null;
-                category = "all";
-                actionRequest.getPortletSession().removeAttribute("portlets", PortletSession.PORTLET_SCOPE);
-            }
-            CategoryResult result = getPortlets(actionRequest, category, "1", 10, filter);
   
-            actionRequest.getPortletSession().setAttribute("portlets", result.getPortlets(),
 PortletSession.PORTLET_SCOPE);
-            
-            // Invalidate category results...
-            List<CategoryResult> categories = (List) actionRequest.getPortletSession().getAttribute("categories");
-            if (categories != null)
-            {
-                actionRequest.getPortletSession().removeAttribute("categories");
-            }
-        }
+        RequestContext requestContext = (RequestContext) actionRequest.getAttribute(RequestContext.REQUEST_PORTALENV);
+        
         if (theme != null)
         {
             ContentPage page = requestContext.getPage();
+            
             try
             {
                 page.updateDefaultDecorator(theme, ContentFragment.LAYOUT);
@@ -216,10 +166,12 @@
                 log.error("Page has not been updated.", e);
             }            
         }
+        
         if (layout != null)
         {
             ContentPage page = requestContext.getPage();
             ContentFragment layoutFragment = page.getRootFragment();
+            
             try
             {
                 layoutFragment.updateName(layout);
@@ -230,11 +182,14 @@
                 log.error("Page has not been updated.", e);
             }            
         }
+        
         String portletAdd = actionRequest.getParameter("portletAdd");
+        
         if (portletAdd != null)
         {
             // BOZO: this should be done via AJAX API
             ContentPage page = requestContext.getPage();
+            
             try
             {                
                 addPortletToLeastUsedColumn(page, portletAdd); 
@@ -247,317 +202,35 @@
     }    
     
     @SuppressWarnings("unchecked")
-    public List<PortletInfo> retrievePortlets(PortletRequest request, String filter)
-    {
-        List<PortletInfo> portletsList = (List<PortletInfo>)request.getPortletSession().getAttribute("portlets",
PortletSession.PORTLET_SCOPE);
-        if (filter == null && portletsList != null)
-        {
-            return portletsList;
-        }        
-        Iterator portlets = null;
-        List list = new ArrayList();
-        Locale locale = request.getLocale();                
-        if (filter == null)
-            portlets = registry.getAllPortletDefinitions().iterator();
-        else
-            portlets = searchEngine.search(filter).getResults().iterator();        
-        while (portlets.hasNext())
-        {
-            PortletDefinition portlet = null;
-            if (filter == null)
-                portlet = (PortletDefinition)portlets.next();
-            else
-                portlet = getPortletFromParsedObject((ParsedObject)portlets.next());
-            
-            PortletInfo portletInfo = filterPortlet(portlet, locale, request);
-            if (portletInfo != null)
-            {
-                list.add(portletInfo);
-            }
-        }            
-        Collections.sort(list, this);
-//        if (filter == null ) 
-            request.getPortletSession().setAttribute("portlets", list,  PortletSession.PORTLET_SCOPE);
-        return list;
-    }
- 
-    protected PortletDefinition getPortletFromParsedObject(ParsedObject po)
+    public List<String> retrieveCategories(PortletRequest request) throws PortletException
     {
-        boolean found = false;
-        String name = "";
-        Map fields = po.getFields();
-        if(fields != null)
-        {
-            Object id = fields.get("ID");
-    
-            if(id != null)
-            {
-                if(id instanceof Collection)
-                {
-                    Collection coll = (Collection)id;
-                    name = (String) coll.iterator().next();
-                }
-                else
-                {
-                    name = (String)id;
-                }
-            }
-            
-            if(po.getType().equals("portlet"))
-            {
-                Object pa = fields.get("portlet_application");
-                String paName = "";
-                if(pa != null)
-                {
-                    if(id instanceof Collection)
-                    {
-                        Collection coll = (Collection)pa;
-                        paName = (String) coll.iterator().next();
-                    }
-                    else
-                    {
-                        paName = (String)pa;
-                    }
-                }
-                name = paName + "::" + name;
-                found = true;
-            }
-        }
-        if (found == false)
-            return null;
+        List<String> categories = (List) request.getPortletSession().getAttribute("categories");
         
-        return registry.getPortletDefinitionByUniqueName(name);
-    }
- 
-    /**
-     * Filters portlets being added to the based on security checks and layout criteria
-     * 
-     * @param portlet
-     * @return null if filtered, otherwise PortletInfo to be added to list
-     */
-    protected PortletInfo filterPortlet(PortletDefinition portlet, Locale locale, PortletRequest
request)
-    {
-        if (portlet == null)
-            return null;
-        if (filterByRole(portlet, request))
-        {
-            return null; 
-        }        
-        // Do not display Jetspeed Layout Applications
-        PortletApplication pa = (PortletApplication)portlet.getApplication();
-        if (pa.isLayoutApplication())
-            return null;
-        
-        // SECURITY filtering
-        String uniqueName = pa.getName() + "::" + portlet.getPortletName();
-        if (securityAccessController.checkPortletAccess(portlet, JetspeedActions.MASK_VIEW))
-        {
-            InitParam param = portlet.getInitParam(PORTLET_ICON);
-            String image;
-            if (param != null)
-            {                
-                //String relativeImagePath = param.getValue();
-                //String context = muta.getWebApplicationDefinition().getContextRoot();
-                // Have to use a supported icon in jetspeed, otherwise image can be out of
skew
-                String  imagePath = param.getParamValue();
-                if (imagePath == null)
-                {
-                    image = DEFAULT_IMAGES[rand.nextInt(DEFAULT_IMAGES.length)];
-                }
-                else
-                {
-                    if (-1 == imagePath.indexOf("/"))
-                        image = "images/portlets/" + param.getParamValue();
-                    else
-                        image = param.getParamValue();
-                }
-            }
-            else
-            {
-                image = DEFAULT_IMAGES[rand.nextInt(DEFAULT_IMAGES.length)];
-            }
-            return new PortletInfo(uniqueName, cleanup(portlet.getDisplayNameText(locale)),
cleanup(portlet.getDescriptionText(locale)), "/" + image);
-        }
-        return null;
-    }
- 
-    public int compare(Object obj1, Object obj2)
-    {
-        PortletInfo portlet1 = (PortletInfo)obj1;
-        PortletInfo portlet2 = (PortletInfo)obj2;
-        String name1 = portlet1.getDisplayName();
-        String name2 = portlet2.getDisplayName();
-        name1 = (name1 == null) ? "unknown" : name1;
-        name2 = (name2 == null) ? "unknown" : name2;
-        return name1.compareTo(name2);
-    }
-    
-    protected boolean filterByRole(PortletDefinition portlet, PortletRequest request)
-    {
-        boolean doFilter = false;
-        Collection c = portlet.getMetadata().getFields("selector.conditional.role");
-        if (c != null) 
-        {
-            Iterator it = c.iterator();
-            if (it.hasNext()) 
-            {
-                LocalizedField roleField = (LocalizedField) it.next();
-                String role = roleField.getValue();
-                if (role != null)
-                {
-                    if (role.equals("*"))
-                        doFilter = true;
-                    else
-                    {
-                        return (!request.isUserInRole(role));
-                    }
-                }
-            }
-        }
-        return doFilter;
-    }
- 
-    protected String cleanup(String str)
-    {
-        if (str == null)
-            return str;
-        return str.replaceAll("\r|\n|\"|\'", "");
-    }
- 
-    @SuppressWarnings("unchecked")
-    public List<CategoryResult> retrieveCategories(PortletRequest request)
-    throws PortletException
-    {
-        List<CategoryResult> categories = (List)request.getPortletSession().getAttribute("categories");
         if (categories != null)
         {
             return categories;
         }
-        Locale locale = request.getLocale();        
-        categories = new ArrayList<CategoryResult>();
-        PortletPreferences prefs = request.getPreferences();
-        String cats = prefs.getValue("Categories", null);
+        
+        String cats = request.getPreferences().getValue("Categories", null);
+        
         if (cats == null)
         {
             throw new PortletException("No categories defined, please add categories via
edit mode.");
         }
-        StringTokenizer catTokenizer = new StringTokenizer(cats, ",");
-        while (catTokenizer.hasMoreTokens())
-        {
-            String name = catTokenizer.nextToken().trim();
-            List<PortletInfo> portlets = new ArrayList<PortletInfo>();
-            String keywords = prefs.getValue("Keywords:" + name, null);
-            if (keywords != null)
-            {
-                StringTokenizer keyTokenizer = new StringTokenizer(keywords, ",");
-                StringBuffer searchString = new StringBuffer();
-                int count = 0;
-                while (keyTokenizer.hasMoreTokens())
-                {
-                    String keyword = keyTokenizer.nextToken().trim();
-                    if (count > 0)
-                    {
-                        searchString.append(" | ");
-                    }
-                    searchString.append(keyword);
-                    count++;
-                }
-                if (count > 0)
-                {
-                    Iterator portletsIter = searchEngine.search(searchString.toString()).getResults().iterator();
-                    while (portletsIter.hasNext())
-                    {
-                        PortletDefinition portlet = 
-                            getPortletFromParsedObject((ParsedObject) portletsIter.next());
-                        PortletInfo portletInfo = filterPortlet(portlet, locale, request);
-                        if (portletInfo != null)
-                        {
-                            portlets.add(portletInfo);
-                        }                    
-                    }                
-                    Collections.sort(portlets, this);
-                }
-            }
-            categories.add(new CategoryResult(name, portlets));
-        }
-        request.getPortletSession().setAttribute("categories", categories);        
-        return categories;
-    }
- 
-    private CategoryResult getPortlets(PortletRequest request,
-            String category, String pageNumber, int portletPerPages,String filter) 
-    {
-        List<PortletInfo> list = new ArrayList<PortletInfo>();
         
-        try 
-        {           
-            if (category != null && category.equalsIgnoreCase("all")) 
-            {             
-                list = retrievePortlets(request, filter);
-            } 
-            else if (filter != null)
-            {
-                list = retrievePortlets(request, filter);
-            } 
-            else 
-            {
-                List<CategoryResult> catList = retrieveCategories(request);
-                for (CategoryResult catInfo : catList)
-                {
-                    if (catInfo.getName().equalsIgnoreCase(category)) 
-                    {
-                        list = catInfo.getPortlets();
-                        break;
-                    }                    
-                }
-            }
-        }
-        catch (Exception e) 
-        {
-            log.error("Cannot retrieve portlets.", e);
-        }
+        categories = Arrays.asList(StringUtils.split(cats, ", \t\n"));
         
-        return new CategoryResult(category, list);
-    }
-
-    public static class CategoryResult implements Serializable
-    {
+        request.getPortletSession().setAttribute("categories", categories);
         
-        String name;
-        List<PortletInfo> portlets;
-
-        /**
-         * @param name
-         * @param list
-         */
-        public CategoryResult(String name, List<PortletInfo> portlets) 
-        {
-            this.name = name;
-            this.portlets = portlets;
-        }
-        
-        /**
-         * @return the name
-         */
-        public String getName() {
-            return name;
-        }
-
-        /**
-         * @return the list
-         */
-        public List<PortletInfo> getPortlets() 
-        {
-            return portlets;
-        }
-        
-    }   
-
+        return categories;
+    }
+ 
     protected ContentFragment findFirstUnlockedFragment(ContentFragment fragment)
     {
         if (fragment.isLocked())
         {
-            List<ContentFragment> fragments = (List<ContentFragment>)fragment.getFragments();
+            List<ContentFragment> fragments = (List<ContentFragment>) fragment.getFragments();
+            
             for (ContentFragment f : fragments)
             {
                 if (f.getType().equals(Fragment.LAYOUT))
@@ -566,7 +239,8 @@
                     if (fragment != null)
                         return fragment;
                 }
-            }       
+            }
+            
             return null;
         }
         return fragment;
@@ -577,17 +251,19 @@
     protected void addPortletToLeastUsedColumn(ContentPage page, String portletName)
     {
         ContentFragment layoutFragment = findFirstUnlockedFragment(page.getRootFragment());
+        
         if (layoutFragment != null)
         {
             String layoutType = layoutFragment.getName();
             List<ContentFragment> fragments = (List<ContentFragment>)layoutFragment.getFragments();
             int columnCounts[] = new int[getLayoutSize(layoutType)];
+            
             for (ContentFragment fragment : fragments)
             {
                 int column = fragment.getLayoutColumn();
                 int row = fragment.getLayoutRow();
                 if (column == -1)
-                    column = min(columnCounts);
+                    column = NumberUtils.min(columnCounts);
                 if (row == -1)
                     row = columnCounts[column];            
                 if (column > fragments.size() -1)
@@ -597,29 +273,19 @@
                 }
                 columnCounts[column]++;
             }
+            
             int placedColumn = 0;
+            
             for (int ix = 1; ix < columnCounts.length; ix++)
             {
                 if (columnCounts[ix] < columnCounts[placedColumn])
                     placedColumn = ix;
             }
+            
             layoutFragment.addPortlet(ContentFragment.PORTLET, portletName, columnCounts[placedColumn],
placedColumn);
         }
     }
     
-    public static int min(int[] numbers)
-    {
-        int minValue = numbers[0];
-        for (int i = 1; i < numbers.length; i++)
-        {
-            if (numbers[i] < minValue)
-            {
-                minValue = numbers[i];
-            }
-        }
-        return minValue;
-    }
-    
     public static int getLayoutSize(String layoutType)
     {
         if (layoutType.indexOf("TwoColumns") > -1)

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/toolbox/toolbox.jsp
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/toolbox/toolbox.jsp?rev=888004&r1=888003&r2=888004&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/toolbox/toolbox.jsp
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/toolbox/toolbox.jsp
Mon Dec  7 17:04:01 2009
@@ -67,7 +67,7 @@
             <select id="<portlet:namespace/>categories">
                 <option value="">Choose a category</option>
                 <c:forEach var="category" items="${categories}">
-                    <option value="${category.name}">${category.name}</option>
+                    <option value="${category}">${category}</option>
                 </c:forEach>
             </select>
         </td>



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message