001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.pam;
018:
019: import java.io.IOException;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Locale;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026:
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletContext;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletSession;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033:
034: import org.apache.jetspeed.CommonPortletServices;
035: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
036: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
037: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
038: import org.apache.jetspeed.search.SearchEngine;
039: import org.apache.jetspeed.search.SearchResults;
040: import org.apache.pluto.om.portlet.PortletDefinition;
041: import org.apache.portals.bridges.beans.TabBean;
042: import org.apache.portals.bridges.common.GenericServletPortlet;
043: import org.apache.webapp.admin.TreeControl;
044: import org.apache.webapp.admin.TreeControlNode;
045:
046: //import org.apache.jetspeed.cps.util.Streams;
047:
048: /**
049: * This portlet is a browser over all the portlet applications in the system.
050: *
051: * @author <a href="mailto:jford@apache.com">Jeremy Ford</a>
052: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
053: * @version $Id: PortletApplicationBrowser.java 348264 2005-11-22 22:06:45Z taylor $
054: */
055: public class PortletApplicationBrowser extends GenericServletPortlet {
056: private PortletContext context;
057: private PortletRegistry registry;
058: private SearchEngine searchEngine;
059:
060: public void init(PortletConfig config) throws PortletException {
061: super .init(config);
062: context = getPortletContext();
063: registry = (PortletRegistry) context
064: .getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
065: if (null == registry) {
066: throw new PortletException(
067: "Failed to find the Portlet Registry on portlet initialization");
068: }
069: searchEngine = (SearchEngine) context
070: .getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
071: if (null == searchEngine) {
072: throw new PortletException(
073: "Failed to find the Search Engine on portlet initialization");
074: }
075: }
076:
077: public void doView(RenderRequest request, RenderResponse response)
078: throws PortletException, IOException {
079: response.setContentType("text/html");
080:
081: TreeControl control = (TreeControl) request.getPortletSession()
082: .getAttribute("j2_tree");
083: if (control == null) {
084: Collection apps = registry.getPortletApplications();
085: control = buildTree(apps, request.getLocale());
086: request.getPortletSession()
087: .setAttribute("j2_tree", control);
088: }
089: request.setAttribute("j2_tree", control);
090: request.setAttribute("search_results", request
091: .getPortletSession().getAttribute("search_results"));
092:
093: super .doView(request, response);
094:
095: }
096:
097: public void processAction(ActionRequest actionRequest,
098: ActionResponse actionResponse) throws PortletException,
099: IOException {
100: TreeControl control = (TreeControl) actionRequest
101: .getPortletSession().getAttribute("j2_tree");
102: //assert control != null
103: if (control != null) {
104: String searchString = actionRequest.getParameter("query");
105: if (searchString != null) {
106: SearchResults results = searchEngine
107: .search(searchString);
108: actionRequest.getPortletSession().setAttribute(
109: "search_results", results.getResults());
110: }
111:
112: String node = actionRequest.getParameter("node");
113: if (node != null) {
114: TreeControlNode controlNode = control.findNode(node);
115: if (controlNode != null) {
116: controlNode.setExpanded(!controlNode.isExpanded());
117: }
118: }
119:
120: String selectedNode = actionRequest
121: .getParameter(PortletApplicationResources.REQUEST_SELECT_NODE);
122: if (selectedNode != null) {
123: control.selectNode(selectedNode);
124: TreeControlNode child = control.findNode(selectedNode);
125: if (child != null) {
126: MutablePortletApplication pa = null;
127:
128: String domain = child.getDomain();
129: if (domain.equals("PA_APP_DOMAIN")) {
130: pa = registry
131: .getPortletApplicationByIdentifier(selectedNode);
132: if (pa != null) {
133: actionRequest
134: .getPortletSession()
135: .removeAttribute(
136: PortletApplicationResources.REQUEST_SELECT_PORTLET,
137: PortletSession.APPLICATION_SCOPE);
138: }
139: } else if (domain.equals("PD_DOMAIN")) {
140: TreeControlNode parent = child.getParent();
141: pa = registry
142: .getPortletApplicationByIdentifier(parent
143: .getName());
144:
145: //set selected tab to portlets tab
146: if (pa != null) {
147: //TODO: do we need to look up the pdef? Could we just pass the child name into setAttribute?
148: String pdefName = child.getName()
149: .substring(
150: pa.getName().length() + 2); //remove pa prefix
151: PortletDefinition pdef = pa
152: .getPortletDefinitionByName(pdefName);
153: actionRequest
154: .getPortletSession()
155: .setAttribute(
156: PortletApplicationResources.REQUEST_SELECT_PORTLET,
157: pdef.getName(),
158: PortletSession.APPLICATION_SCOPE);
159: actionRequest
160: .getPortletSession()
161: .setAttribute(
162: PortletApplicationResources.REQUEST_SELECT_TAB,
163: new TabBean("pa_portlets"),
164: PortletSession.APPLICATION_SCOPE);
165: }
166: } else {
167: //warn about not recognized domain
168: }
169:
170: if (pa != null) {
171: actionRequest
172: .getPortletSession()
173: .setAttribute(
174: PortletApplicationResources.PAM_CURRENT_PA,
175: pa.getName(),
176: PortletSession.APPLICATION_SCOPE);
177: }
178: }
179: }
180: }
181: }
182:
183: private TreeControl buildTree(Collection apps, Locale locale) {
184: TreeControlNode root = new TreeControlNode("ROOT-NODE", null,
185: "J2_ROOT", PortletApplicationResources.PORTLET_URL,
186: null, true, "J2_DOMAIN");
187:
188: TreeControl control = new TreeControl(root);
189:
190: TreeControlNode portletApps = new TreeControlNode("APP_ROOT",
191: null, "APP_ROOT",
192: PortletApplicationResources.PORTLET_URL, null, false,
193: "J2_DOMAIN");
194: root.addChild(portletApps);
195:
196: Iterator it = apps.iterator();
197: while (it.hasNext()) {
198: MutablePortletApplication pa = (MutablePortletApplication) it
199: .next();
200: TreeControlNode appNode = new TreeControlNode(pa.getName(),
201: null, pa.getName(),
202: PortletApplicationResources.PORTLET_URL, null,
203: false, "PA_APP_DOMAIN");
204: portletApps.addChild(appNode);
205:
206: Iterator pdefIter = pa.getPortletDefinitionList()
207: .iterator();
208: while (pdefIter.hasNext()) {
209: PortletDefinitionComposite portlet = (PortletDefinitionComposite) pdefIter
210: .next();
211: TreeControlNode portletNode = new TreeControlNode(pa
212: .getName()
213: + "::" + portlet.getName(), null, portlet
214: .getDisplayNameText(locale),
215: PortletApplicationResources.PORTLET_URL, null,
216: false, "PD_DOMAIN");
217: appNode.addChild(portletNode);
218: }
219: }
220:
221: return control;
222: }
223: }
|