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.pam;
18  
19  import java.io.IOException;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Locale;
23  
24  import javax.portlet.ActionRequest;
25  import javax.portlet.ActionResponse;
26  
27  import javax.portlet.PortletConfig;
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletSession;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.jetspeed.CommonPortletServices;
35  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
36  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
37  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
38  import org.apache.jetspeed.search.SearchEngine;
39  import org.apache.jetspeed.search.SearchResults;
40  import org.apache.pluto.om.portlet.PortletDefinition;
41  import org.apache.portals.bridges.beans.TabBean;
42  import org.apache.portals.bridges.common.GenericServletPortlet;
43  import org.apache.webapp.admin.TreeControl;
44  import org.apache.webapp.admin.TreeControlNode;
45  
46  //import org.apache.jetspeed.cps.util.Streams;
47  
48  /***
49   * This portlet is a browser over all the portlet applications in the system.
50   *
51   * @author <a href="mailto:jford@apache.com">Jeremy Ford</a>
52   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
53   * @version $Id: PortletApplicationBrowser.java 348264 2005-11-22 22:06:45Z taylor $
54   */
55  public class PortletApplicationBrowser extends GenericServletPortlet
56  {
57      private PortletContext context;
58      private PortletRegistry registry;
59      private SearchEngine searchEngine;
60      
61      public void init(PortletConfig config)
62      throws PortletException 
63      {
64          super.init(config);
65          context = getPortletContext();                
66          registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
67          if (null == registry)
68          {
69              throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
70          }        
71          searchEngine = (SearchEngine)context.getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
72          if (null == searchEngine)
73          {
74              throw new PortletException("Failed to find the Search Engine on portlet initialization");
75          }        
76      }
77      
78      public void doView(RenderRequest request, RenderResponse response)
79      throws PortletException, IOException
80      {
81          response.setContentType("text/html");
82          
83          TreeControl control = (TreeControl) request.getPortletSession().getAttribute("j2_tree");
84          if(control == null)
85          {
86              Collection apps = registry.getPortletApplications();
87          	control = buildTree(apps, request.getLocale());
88          	request.getPortletSession().setAttribute("j2_tree", control);
89          }
90          request.setAttribute("j2_tree", control);
91          request.setAttribute("search_results", request.getPortletSession().getAttribute("search_results"));
92          
93          super.doView(request, response);
94          
95      }
96  
97      
98  	public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException
99  	{
100 		TreeControl control = (TreeControl) actionRequest.getPortletSession().getAttribute("j2_tree");
101 		//assert control != null
102 		if(control != null)
103 		{
104 		    String searchString = actionRequest.getParameter("query");
105 		    if(searchString != null)
106 		    {
107 		        SearchResults results = searchEngine.search(searchString);
108 		        actionRequest.getPortletSession().setAttribute("search_results", results.getResults());
109 		    }
110 		    
111 			String node = actionRequest.getParameter("node");
112 			if(node != null)
113 			{
114 			    TreeControlNode controlNode = control.findNode(node);
115 			    if(controlNode != null)
116 			    {
117 			        controlNode.setExpanded(!controlNode.isExpanded());
118 			    }
119 			}
120 			
121 			String selectedNode = actionRequest.getParameter(PortletApplicationResources.REQUEST_SELECT_NODE);
122 			if(selectedNode != null)
123 			{
124 			    control.selectNode(selectedNode);
125 			    TreeControlNode child = control.findNode(selectedNode);
126 			    if(child != null)
127 			    {
128 			        MutablePortletApplication pa = null;
129 			        
130 				    String domain = child.getDomain();
131 				    if(domain.equals("PA_APP_DOMAIN"))
132 				    {
133 				        pa = registry.getPortletApplicationByIdentifier(selectedNode);
134 				        if(pa != null)
135 				        {
136 				            actionRequest.getPortletSession().removeAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, PortletSession.APPLICATION_SCOPE);
137 				        }
138 				    }
139 				    else if(domain.equals("PD_DOMAIN"))
140 				    {
141 				        TreeControlNode parent = child.getParent();
142 			            pa = registry.getPortletApplicationByIdentifier(parent.getName());
143 			            
144 			            //set selected tab to portlets tab
145 			            if(pa != null)
146 			            {
147 			                //TODO:  do we need to look up the pdef?  Could we just pass the child name into setAttribute?
148 			                String pdefName = child.getName().substring(pa.getName().length() + 2); //remove pa prefix
149 			                PortletDefinition pdef = pa.getPortletDefinitionByName(pdefName);
150 			                actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, pdef.getName(), PortletSession.APPLICATION_SCOPE);
151 			                actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, new TabBean("pa_portlets"), PortletSession.APPLICATION_SCOPE);
152 			            }
153 				    }
154 				    else
155 				    {
156 				        //warn about not recognized domain
157 				    }
158 				    
159 				    if (pa != null)
160 		            {
161 		                actionRequest.getPortletSession().setAttribute(PortletApplicationResources.PAM_CURRENT_PA, pa.getName(), PortletSession.APPLICATION_SCOPE);
162 		            }
163 			    }
164 			}
165 		}
166 	}
167 	
168 	private TreeControl buildTree(Collection apps, Locale locale) 
169     {	    
170 		TreeControlNode root =
171             new TreeControlNode("ROOT-NODE",
172                                 null, 
173                                 "J2_ROOT",
174                                 PortletApplicationResources.PORTLET_URL,
175                                 null, 
176                                 true, 
177                                 "J2_DOMAIN");
178 		
179 		TreeControl control = new TreeControl(root);
180 		
181 		
182 		TreeControlNode portletApps = 
183 			new TreeControlNode("APP_ROOT", 
184                                 null, 
185                                 "APP_ROOT", 
186                                 PortletApplicationResources.PORTLET_URL, 
187                                 null, 
188                                 false, 
189                                 "J2_DOMAIN");
190 		root.addChild(portletApps);
191 		
192 		Iterator it = apps.iterator();
193         while (it.hasNext())
194         {
195             MutablePortletApplication pa = (MutablePortletApplication)it.next();
196             TreeControlNode appNode = new TreeControlNode(pa.getName(), 
197                                                           null, 
198                                                           pa.getName(), 
199                                                           PortletApplicationResources.PORTLET_URL, 
200                                                           null, 
201                                                           false, 
202                                                           "PA_APP_DOMAIN"  );
203             portletApps.addChild(appNode);
204             
205             Iterator pdefIter = pa.getPortletDefinitionList().iterator();
206             while (pdefIter.hasNext())
207             {
208                 PortletDefinitionComposite portlet = (PortletDefinitionComposite) pdefIter.next();
209                 TreeControlNode portletNode = new TreeControlNode(pa.getName() + "::" + portlet.getName(), 
210                                                                   null, 
211                                                                   portlet.getDisplayNameText(locale), 
212                                                                   PortletApplicationResources.PORTLET_URL, 
213                                                                   null, 
214                                                                   false, 
215                                                                   "PD_DOMAIN");
216                 appNode.addChild(portletNode);
217             }
218         }
219 		
220 		return control;
221 	}
222 }