View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.selector;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.Random;
30  import java.util.StringTokenizer;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.PortletContext;
36  import javax.portlet.PortletException;
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletPreferences;
39  import javax.portlet.PortletSession;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  import org.apache.jetspeed.CommonPortletServices;
46  import org.apache.jetspeed.JetspeedActions;
47  import org.apache.jetspeed.PortalReservedParameters;
48  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
49  import org.apache.jetspeed.headerresource.HeaderResource;
50  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
51  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
52  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
53  import org.apache.jetspeed.om.page.Fragment;
54  import org.apache.jetspeed.om.page.Page;
55  import org.apache.jetspeed.page.PageManager;
56  import org.apache.jetspeed.portlets.CategoryInfo;
57  import org.apache.jetspeed.portlets.PortletInfo;
58  import org.apache.jetspeed.profiler.Profiler;
59  import org.apache.jetspeed.request.RequestContext;
60  import org.apache.jetspeed.search.ParsedObject;
61  import org.apache.jetspeed.search.SearchEngine;
62  import org.apache.jetspeed.security.SecurityAccessController;
63  import org.apache.pluto.om.common.Parameter;
64  import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
65  import org.apache.velocity.context.Context;
66  
67  /***
68   * CategoryPortletSelector selects categories organized by categories
69   * 
70   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
71   * @version $Id: $
72   */
73  public class CategoryPortletSelector extends AbstractDojoVelocityPortlet implements Comparator
74  {
75      public final String[] DEFAULT_IMAGES = new String[]
76      {
77              "images/portlets/applications-development.png",
78              "images/portlets/applications-system.png",
79              "images/portlets/applications-other.png",
80              "images/portlets/linux.png"
81      };
82      protected final Log logger = LogFactory.getLog(this.getClass());
83      public final static String PORTLET_ICON = "portlet-icon";
84      protected final static String PORTLETS = "category.selector.portlets";
85      protected final static String CATEGORIES = "category.selector.categories";
86      protected final static String PAGE = "category.selector.page";
87      protected final static String LAYOUT = "category.selector.layout";
88      
89      private final String JSPAGE = "jspage";
90      private final String JSLAYOUT = "jslayoutid";
91  	private final String CATEGORY = "category";
92  	private final String PAGENUMNER = "pageNumber";
93  	private final String FILTER = "filter";
94      
95      protected PortletRegistry registry;
96      protected SearchEngine searchEngine;
97      protected PageManager pageManager;
98      protected Profiler profiler;
99      protected SecurityAccessController securityAccessController;
100     protected Random rand;
101     
102     public void init(PortletConfig config)
103     throws PortletException 
104     {
105         super.init(config);
106         PortletContext context = getPortletContext();                
107         registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
108         if (null == registry)
109         {
110             throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
111         }        
112         searchEngine = (SearchEngine)context.getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
113         if (null == searchEngine)
114         {
115             throw new PortletException("Failed to find the Search Engine on portlet initialization");
116         }
117         pageManager = (PageManager)context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
118         if (null == pageManager)
119         {
120             throw new PortletException("Failed to find the Page Manager on portlet initialization");
121         }        
122         securityAccessController = (SecurityAccessController)context.getAttribute(CommonPortletServices.CPS_SECURITY_ACCESS_CONTROLLER);
123         if (null == securityAccessController)
124         {
125             throw new PortletException("Failed to find the Security Access Controller on portlet initialization");
126         }
127         profiler = (Profiler)context.getAttribute(CommonPortletServices.CPS_PROFILER_COMPONENT);
128         if (null == profiler)
129         {
130             throw new PortletException("Failed to find the Profiler on portlet initialization");
131         }        
132         rand = new Random( 19580427 );
133     }
134     
135     public void doView(RenderRequest request, RenderResponse response)
136             throws PortletException, IOException
137     {
138     	String category = "All";
139     	String pageNumber = "";	
140 		String row = "";		
141 		String columns = "";
142 		int portletPerPages=0;
143     	PortletPreferences prefs = request.getPreferences();
144 		category = request.getParameter(CATEGORY);
145 		pageNumber = request.getParameter(PAGENUMNER);
146 		String filter = request.getParameter(FILTER);
147 		columns = prefs.getValue("Columns", "4");
148 		row = prefs.getValue("Rows", "6");
149 		portletPerPages = Integer.parseInt(columns) * Integer.parseInt(row);
150 		if (category == null || category.equals("")) category = "All";
151 		if (pageNumber == null || pageNumber.equals("")) pageNumber = "1";
152 		if (filter == null ||  filter.equals("")) filter = null;
153 		CategoryResult result = getPortlets(request, category, pageNumber, portletPerPages, filter);
154         this.getContext(request).put("Columns", columns);
155         this.getContext(request).put("Rows", row);
156         this.getContext(request).put("pageNumber", pageNumber);
157         this.getContext(request).put("category", category);
158         this.getContext(request).put("portlets", mergePortletCount(result.getList(),request));
159         this.getContext(request).put("Count", new Integer(result.getResultSize()));
160         this.getContext(request).put("categories", retrieveCategories(request));
161         processPage(request);
162         super.doView(request, response);
163     }
164     
165     protected Page getPage(RenderRequest request) throws Exception
166     {
167         String path = request.getParameter(JSPAGE);
168         RequestContext requestContext = (RequestContext) 
169             request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);        
170         return requestContext.locatePage(profiler, path);
171     }    
172     
173     protected void processPage(RenderRequest request)
174     {
175         String page = request.getParameter(JSPAGE);
176         if (page == null || page.equals(""))
177         {
178             page = (String)request.getPortletSession().getAttribute(PAGE);
179         }
180         else
181         {
182             request.getPortletSession().setAttribute(PAGE, page);
183         }
184         String layout = request.getParameter(JSLAYOUT);
185         this.getContext(request).put(JSPAGE, page); 
186         if (layout == null || layout.equals(""))
187         {
188             layout = (String)request.getPortletSession().getAttribute(LAYOUT);
189         }
190         else
191         {
192             request.getPortletSession().setAttribute(LAYOUT, layout);
193         }
194         this.getContext(request).put(JSLAYOUT, layout);                
195     }
196     
197     public List retrieveCategories(RenderRequest request)
198     throws PortletException
199     {
200         List categories = (List)request.getPortletSession().getAttribute(CATEGORIES);
201         if (categories != null)
202         {
203             return categories;
204         }
205         Locale locale = request.getLocale();        
206         categories = new ArrayList();
207         PortletPreferences prefs = request.getPreferences();
208         String cats = prefs.getValue("Categories", null);
209         if (cats == null)
210         {
211             throw new PortletException("No categories defined, please add categories via edit mode.");
212         }
213         StringTokenizer catTokenizer = new StringTokenizer(cats, ",");
214         while (catTokenizer.hasMoreTokens())
215         {
216             String name = catTokenizer.nextToken().trim();
217             CategoryInfo cat = new CategoryInfo(name);
218             String keywords = prefs.getValue("Keywords:" + name, null);
219             if (keywords != null)
220             {
221                 StringTokenizer keyTokenizer = new StringTokenizer(keywords, ",");
222                 StringBuffer searchString = new StringBuffer();
223                 int count = 0;
224                 while (keyTokenizer.hasMoreTokens())
225                 {
226                     String keyword = keyTokenizer.nextToken().trim();
227                     if (count > 0)
228                     {
229                         searchString.append(" | ");
230                     }
231                     searchString.append(keyword);
232                     count++;
233                 }
234                 if (count > 0)
235                 {
236                     Iterator portlets = searchEngine.search(searchString.toString()).getResults().iterator();
237                     while (portlets.hasNext())
238                     {
239                         PortletDefinitionComposite portlet = 
240                             getPortletFromParsedObject((ParsedObject)portlets.next());
241                         PortletInfo portletInfo = filterPortlet(portlet, locale);
242                         if (portletInfo != null)
243                         {
244                             cat.addPortlet(portletInfo);
245                         }                    
246                     }                
247                     Collections.sort(cat.getPortlets(), this);
248                     categories.add(cat);
249                 }
250             }
251         }
252         request.getPortletSession().setAttribute(CATEGORIES, categories);        
253         return categories;
254     }
255     
256     public List retrievePortlets(RenderRequest request, String filter)
257     {
258         List portletsList = (List)request.getPortletSession().getAttribute(PORTLETS);
259         if (filter == null && portletsList != null)
260         {
261             return portletsList;
262         }        
263         Iterator portlets = null;
264         List list = new ArrayList();
265         Locale locale = request.getLocale();                
266         if (filter == null)
267             portlets = registry.getAllPortletDefinitions().iterator();
268         else
269             portlets = searchEngine.search(filter).getResults().iterator();
270         
271         while (portlets.hasNext())
272         {
273             PortletDefinitionComposite portlet = null;
274             if (filter == null)
275                 portlet = (PortletDefinitionComposite)portlets.next();
276             else
277                 portlet = getPortletFromParsedObject((ParsedObject)portlets.next());
278             
279             PortletInfo portletInfo = filterPortlet(portlet, locale);
280             if (portletInfo != null)
281             {
282                 list.add(portletInfo);
283             }
284         }            
285         Collections.sort(list, this);
286         if(filter == null ) request.getPortletSession().setAttribute(PORTLETS, list);
287         return list;
288     }
289     
290 
291     private List mergePortletCount(List sysPortlets,
292 			RenderRequest request) {
293 		List list = new ArrayList();
294 		int sPortletCnt = 0;
295 		Iterator iterator;
296 		String usrPortletName = null;
297 		PortletInfo tmpPortletInfo = null;
298 		String portletName;
299 		try {
300 			Map usrPortlet = getUserPortlet(getPage(request));
301 			sPortletCnt = sysPortlets.size();
302 			for (int si = 0; si < sPortletCnt; si++) {
303 				tmpPortletInfo = (PortletInfo)((PortletInfo)sysPortlets.get(si)).clone(); 
304 				portletName = tmpPortletInfo.getName();
305 				iterator = usrPortlet.keySet().iterator();
306 				while (iterator.hasNext()) {
307 					usrPortletName = (String) iterator.next();
308 					if (usrPortletName.equalsIgnoreCase(portletName)) {
309 						tmpPortletInfo.setCount(((Integer)usrPortlet.get(usrPortletName)).intValue());
310 					}
311 				}
312 				list.add(tmpPortletInfo);
313 				tmpPortletInfo = null;
314 			}
315 		} catch (Exception e) {
316 			e.printStackTrace();
317 		}
318 		return list;
319 	}
320 
321     
322     /***
323      * Filters portlets being added to the based on security checks and layout criteria
324      * 
325      * @param portlet
326      * @return null if filtered, otherwise PortletInfo to be added to list
327      */
328     protected PortletInfo filterPortlet(PortletDefinitionComposite portlet, Locale locale)
329     {
330         if (portlet == null)
331             return null;
332         
333         // Do not display Jetspeed Layout Applications
334         MutablePortletApplication pa = (MutablePortletApplication)portlet.getPortletApplicationDefinition();
335         if (pa.isLayoutApplication())
336             return null;
337         
338         // SECURITY filtering
339         String uniqueName = pa.getName() + "::" + portlet.getName();
340         if (securityAccessController.checkPortletAccess(portlet, JetspeedActions.MASK_VIEW))
341         {
342             Parameter param = portlet.getInitParameterSet().get(PORTLET_ICON);
343             String image;
344             if (param != null)
345             {                
346                 //String relativeImagePath = param.getValue();
347                 //String context = muta.getWebApplicationDefinition().getContextRoot();
348                 // Have to use a supported icon in jetspeed, otherwise image can be out of skew
349                 String  imagePath = param.getValue();
350                 if (imagePath == null)
351                 {
352                     image = DEFAULT_IMAGES[rand.nextInt(DEFAULT_IMAGES.length)];
353                 }
354                 else
355                 {
356                     if (-1 == imagePath.indexOf("/"))
357                         image = "images/portlets/" + param.getValue();
358                     else
359                         image = param.getValue();
360                 }
361             }
362             else
363             {
364                 image = DEFAULT_IMAGES[rand.nextInt(DEFAULT_IMAGES.length)];
365             }
366             return new PortletInfo(uniqueName, cleanup(portlet.getDisplayNameText(locale)), cleanup(portlet.getDescriptionText(locale)), image);
367         }
368         return null;
369     }
370     
371     protected String cleanup(String str)
372     {
373         if (str == null)
374             return str;
375         return str.replaceAll("\r|\n|\"|\'", "");
376     }
377     
378     protected PortletDefinitionComposite getPortletFromParsedObject(ParsedObject po)
379     {
380         boolean found = false;
381         String name = "";
382         Map fields = po.getFields();
383         if(fields != null)
384         {
385             Object id = fields.get("ID");
386     
387             if(id != null)
388             {
389                 if(id instanceof Collection)
390                 {
391                     Collection coll = (Collection)id;
392                     name = (String) coll.iterator().next();
393                 }
394                 else
395                 {
396                     name = (String)id;
397                 }
398             }
399             
400             if(po.getType().equals("portlet"))
401             {
402                 Object pa = fields.get("portlet_application");
403                 String paName = "";
404                 if(pa != null)
405                 {
406                     if(id instanceof Collection)
407                     {
408                         Collection coll = (Collection)pa;
409                         paName = (String) coll.iterator().next();
410                     }
411                     else
412                     {
413                         paName = (String)pa;
414                     }
415                 }
416                 name = paName + "::" + name;
417                 found = true;
418             }
419         }
420         if (found == false)
421             return null;
422         
423         return registry.getPortletDefinitionByUniqueName(name);
424     }
425         
426     
427     public void processAction(ActionRequest request,
428             ActionResponse actionResponse) throws PortletException, IOException
429     {
430         if (request.getPortletMode() == PortletMode.EDIT)
431         {
432             String removes = request.getParameter("jsRemovedCats");
433             String modifiedCats = request.getParameter("jsModifiedCats");
434             String modifiedKeys = request.getParameter("jsModifiedKeys");
435             String addedCats = request.getParameter("jsAddedCats");
436             String addedKeys = request.getParameter("jsAddedKeys");
437             String columns = request.getParameter("Columns");
438             String rows = request.getParameter("Rows");
439             String cats = request.getParameter("jsCats");
440             String page = request.getParameter(JSPAGE);
441             MutablePortletApplication pa = registry.getPortletApplication("j2-admin");
442             String portletName = this.getPortletName();
443             PortletDefinitionComposite portlet = (PortletDefinitionComposite) pa.getPortletDefinitionByName(portletName);
444             boolean updated = updateNumericPref("Columns", columns, 10, portlet);
445             updated = updated | updateNumericPref("Rows", rows, 100, portlet);
446                         
447             // process removes first
448             if (!isEmpty(removes))
449             {
450                 StringTokenizer tokenizer = new StringTokenizer(removes, ",");
451                 while (tokenizer.hasMoreTokens())
452                 {
453                     String name = tokenizer.nextToken().trim();
454                     updated = updated | removePref("Keywords:" + name, portlet);
455                 }                
456             }
457             
458             // process adds
459             if (!isEmpty(addedCats))
460             {
461                 StringTokenizer keyTokenizer = new StringTokenizer(addedKeys, "|");
462                 StringTokenizer tokenizer = new StringTokenizer(addedCats, ",");
463                 while (tokenizer.hasMoreTokens() && keyTokenizer.hasMoreTokens())
464                 {
465                     String name = tokenizer.nextToken().trim();
466                     String keys = keyTokenizer.nextToken().trim();
467                     updated = updated | addPref("Keywords:" + name, keys, portlet);
468                 }                
469             }
470             
471             // process updates
472             if (!isEmpty(modifiedCats))
473             {
474                 StringTokenizer keyTokenizer = new StringTokenizer(modifiedKeys, "|");
475                 StringTokenizer tokenizer = new StringTokenizer(modifiedCats, ",");
476                 while (tokenizer.hasMoreTokens())
477                 {
478                     String name = tokenizer.nextToken().trim();
479                     String keys = keyTokenizer.nextToken().trim();                    
480                     updated = updated | modifyPref("Keywords:" + name, keys, portlet);
481                 }                
482             }
483                                     
484             try
485             {
486                 if (updated)
487                 {
488                     // process category string list
489                     // sort it first
490                     StringTokenizer catTokenizer = new StringTokenizer(cats, ",");
491                     List sorted = new ArrayList();
492                     while (catTokenizer.hasMoreTokens())
493                     {
494                         String name = catTokenizer.nextToken().trim();
495                         sorted.add(name);
496                     }
497                     Collections.sort(sorted);
498                     Iterator si = sorted.iterator();
499                     StringBuffer temp = new StringBuffer();
500                     int count = 0;
501                     while (si.hasNext())
502                     {
503                         String name = (String)si.next();
504                         if (count > 0)
505                         {
506                             temp.append(",");
507                         }
508                         temp.append(name);
509                         count++;
510                     }
511                     cats = temp.toString();
512                     //System.out.println("cats = [" + cats + "]");
513                     modifyPref("Categories", cats, portlet);
514                     
515                     // finally save it all
516                     registry.savePortletDefinition(portlet);
517                 }
518             }
519             catch (Exception e)
520             {
521                 throw new PortletException("Failed to update portlet", e);
522             }
523             PortletSession session = request.getPortletSession();
524             session.removeAttribute(PORTLETS);
525             session.removeAttribute(CATEGORIES);
526             actionResponse.setPortletMode(PortletMode.VIEW);
527             actionResponse.setRenderParameter(JSPAGE, page);
528         }
529         else
530         {            
531         	String reset = request.getParameter("reset");        
532             if (reset != null && reset.equals("true"))
533             {
534                 PortletSession session = request.getPortletSession();
535                 session.removeAttribute(PORTLETS);
536                 session.removeAttribute(CATEGORIES);
537             }
538             String category = request.getParameter(CATEGORY);
539             if (category != null)
540                 actionResponse.setRenderParameter(CATEGORY, category);
541             String pageNumber = request.getParameter(PAGENUMNER);
542             if (pageNumber != null)
543                 actionResponse.setRenderParameter(PAGENUMNER, pageNumber);                
544             String filter = request.getParameter(FILTER);
545             if (filter != null)
546                 actionResponse.setRenderParameter(FILTER, filter);
547             String pagePath = request.getParameter(JSPAGE);
548             if (pagePath != null)
549                 actionResponse.setRenderParameter(JSPAGE, pagePath);            
550         }
551     }
552 
553     private boolean addPref(String prefName, String keywords, PortletDefinitionComposite portlet)
554     {
555         PreferenceComposite pref = (PreferenceComposite) portlet.getPreferenceSet().get(prefName);        
556         if (pref == null)
557         {
558             portlet.addPreference(prefName, new String[] { keywords });
559         }
560         else
561         {
562             return modifyPref(prefName, keywords, portlet);           
563         }        
564         return true;
565     }
566     
567     private boolean modifyPref(String prefName, String keywords, PortletDefinitionComposite portlet)
568     {
569         PreferenceComposite prefComp = (PreferenceComposite) portlet.getPreferenceSet().get(prefName);
570         String[] values = prefComp.getValueArray();
571         if(!values[0].equals(keywords))
572         {
573             prefComp.setValueAt(0, keywords);
574             return true;
575         }
576         return false;
577     }
578     
579     private boolean removePref(String prefName, PortletDefinitionComposite portlet)
580     {
581         Iterator prefIter = portlet.getPreferenceSet().iterator();
582         while (prefIter.hasNext())
583         {
584             PreferenceComposite pref = (PreferenceComposite) prefIter.next();
585             String name = pref.getName();
586             if (name.equals(prefName))
587             {
588                     prefIter.remove();
589                     return true;
590             }
591         }
592         return false;
593     }
594     
595     private boolean updateNumericPref(String prefName, String param, int max, PortletDefinitionComposite portlet)
596     {
597         if (!isEmpty(param))
598         {
599             int val = 4;
600             try
601             {
602                 val = Integer.parseInt(param);
603             }
604             catch (NumberFormatException e)
605             {
606                 return false;                
607             }
608             if (val > max)
609                 return false;
610             PreferenceComposite pref = (PreferenceComposite) portlet.getPreferenceSet().get(prefName);
611             String[] values = pref.getValueArray();
612             if(!values[0].equals(param))
613             {
614                 pref.setValueAt(0, param);
615                 return true;
616             }                           
617         }
618         return false;
619     }
620     private boolean isEmpty(String param)
621     {
622         if (param == null)
623             return true;
624         param = param.trim();
625         if (param.length() == 0)
626             return true;
627         return false;
628     }
629 
630     public int compare(Object obj1, Object obj2)
631     {
632         PortletInfo portlet1 = (PortletInfo)obj1;
633         PortletInfo portlet2 = (PortletInfo)obj2;
634         String name1 = portlet1.getDisplayName();
635         String name2 = portlet2.getDisplayName();
636         name1 = (name1 == null) ? "unknown" : name1;
637         name2 = (name2 == null) ? "unknown" : name2;
638         return name1.compareTo(name2);
639     }
640         
641     protected void includeHeaderContent(HeaderResource headerResource)
642     {
643         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Dialog" );
644         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Button" );
645         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.ContentPane" );
646         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.LayoutContainer" );
647         headerResource.dojoAddModuleLibraryRequire( "jetspeed.selector" );
648     }
649 
650     /* (non-Javadoc)
651      * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
652      */
653     public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
654     {
655         Context context = getContext(request);
656         PortletPreferences prefs = request.getPreferences();
657         String cats = prefs.getValue("Categories", null);
658         List categories = new ArrayList();
659         if (cats != null)
660         {
661             StringTokenizer catTokenizer = new StringTokenizer(cats, ",");
662             while (catTokenizer.hasMoreTokens())
663             {
664                 String name = catTokenizer.nextToken().trim();
665                 CategoryInfo cat = new CategoryInfo(name);
666                 String keys = prefs.getValue("Keywords:" + name, "");
667                 cat.setKeywords(keys);
668                 categories.add(cat);
669             }
670         }
671         context.put("categories", categories);
672         context.put("Rows", prefs.getValue("Rows", "5"));
673         context.put("Columns", prefs.getValue("Columns", "4"));
674         processPage(request);
675         super.doEdit(request, response);
676     }
677     
678     private CategoryResult getPortlets(RenderRequest request,
679 			String category, String pageNumber, int portletPerPages,String filter) {
680 		List list = new ArrayList();
681 		List tmpList = null;
682 		List catList = null;
683 		Iterator iterator = null;
684 		int portletCount = 0;
685 		CategoryInfo catInfo = null;
686 		int strtCnt = getStartRow(pageNumber, portletPerPages);
687 		int endCnt = getEndRow(pageNumber, portletPerPages);
688 		try {			
689 			if (category.equalsIgnoreCase("all")) {				
690 				tmpList = retrievePortlets(request, filter);
691 				portletCount = tmpList.size();
692 				if (endCnt > portletCount)
693 					endCnt = portletCount;
694 				for (int index = strtCnt; index < endCnt; index++) {
695 					list.add(tmpList.get(index));
696 				}
697 			} else if (category.equalsIgnoreCase("search")) {
698 				tmpList = retrievePortlets(request, filter);
699 				portletCount = tmpList.size();
700 				if (endCnt > portletCount)
701 					endCnt = portletCount;
702 				for (int index = strtCnt; index < endCnt; index++) {
703 					list.add(tmpList.get(index));
704 				}
705 			} else {
706 				tmpList =  retrieveCategories(request);
707 				iterator = tmpList.iterator();
708 				while (iterator.hasNext()) {
709 					catInfo = (CategoryInfo) iterator.next();
710 					if (catInfo.getName().equalsIgnoreCase(category)) {
711 						catList = catInfo.getPortlets();
712 						break;
713 					}
714 				}
715 				portletCount = catList.size();
716 				if (endCnt > portletCount)
717 					endCnt = portletCount;
718 				for (int index = strtCnt; index < endCnt; index++) {
719 					list.add(catList.get(index));
720 				}
721 			}
722 		} catch (Exception e) {
723 			e.printStackTrace();
724 		}
725 		return new CategoryResult(list, portletCount);
726 	}
727     
728     
729 	private int getStartRow(String sPageNumber, int portletPerPages) {
730 		int iPageNumber = Integer.parseInt(sPageNumber);
731 		return (iPageNumber - 1) * portletPerPages;
732 	}
733 
734 	private int getEndRow(String sPageNumber,int portletPerPages) {
735 		int iPageNumber = Integer.parseInt(sPageNumber);
736 		return iPageNumber * portletPerPages;
737 	}
738 	private Map getUserPortlet(Page page) {
739 		int portletCount = 0;
740 		Map map = new HashMap();
741 		try {
742 			Fragment fragment;
743 			Iterator it = page.getRootFragment().getFragments().iterator();
744 			while (it.hasNext()) {
745 				fragment = (Fragment) it.next();
746 				if (map.containsKey(fragment.getName())) {
747 					portletCount =((Integer) map.get(fragment.getName())).intValue();				
748 					map.put(fragment.getName(), new Integer(portletCount + 1));					
749 				} else {
750 					map.put(fragment.getName(), new Integer(1));
751 				}
752 			}
753 		} catch (Exception e) {
754 //			System.out.println(e.getMessage());
755 			e.printStackTrace();
756 		}
757 		return map;
758 	}
759 
760     class CategoryResult {
761 		List list;
762 
763 		int resultSize;
764 
765 		/***
766 		 * @param list
767 		 * @param resultSize
768 		 */
769 		public CategoryResult(List list, int resultSize) {
770 			this.list = list;
771 			this.resultSize = resultSize;
772 		}
773 
774 		/***
775 		 * @return the list
776 		 */
777 		public List getList() {
778 			return list;
779 		}
780 
781 		/***
782 		 * @return the resultSize
783 		 */
784 		public int getResultSize() {
785 			return resultSize;
786 		}
787 	}
788 }