1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
145 if(pa != null)
146 {
147
148 String pdefName = child.getName().substring(pa.getName().length() + 2);
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
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 }