001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.processor;
020:
021: import java.io.IOException;
022: import java.io.Writer;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import com.nabhinc.portal.core.PortalConstants;
029: import com.nabhinc.portal.core.PortalUtil;
030: import com.nabhinc.portal.core.SessionCache;
031: import com.nabhinc.portal.model.CompositeRenderable;
032: import com.nabhinc.portal.model.NavigationalLayout;
033: import com.nabhinc.portal.model.PortalApplication;
034: import com.nabhinc.portal.model.PortalApplicationView;
035: import com.nabhinc.portal.model.PortalConfiguration;
036: import com.nabhinc.portal.model.PortalPageState;
037: import com.nabhinc.portal.model.PortletWindow;
038: import com.nabhinc.portal.model.Renderable;
039: import com.nabhinc.util.StringUtil;
040:
041: /**
042: * Adds a portlet to a column in RenderableMap
043: *
044: * @author Padmanabh Dabke
045: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
046: */
047: public class AddPortletProcessor extends EditPagePortalActionProcessor {
048: public static final String OPTION_TYPE_PARAM = "option_type";
049: public static final String URL_PARAM = "option_url";
050: public static final String LABEL_PARAM = "option_label";
051: public static final String LEVEL_PARAM = "option_level";
052: public static final String ADD_TYPE_HEADER = "add_header";
053: public static final String ADD_TYPE_URL = "add_url";
054: public static final String ADD_TYPE_RENDERABLE = "add_content";
055: public static final String PORTLET_NAME_PARAM = "portlet_name";
056: public static final String HIDE_TITLE_PARAM = "hide_title";
057: public static final String PORTLET_TEMPLATE_PARAM = "portlet_template";
058:
059: public void process(HttpServletRequest request,
060: HttpServletResponse response, SessionCache sCache,
061: PortalApplicationView portalAppView,
062: PortalPageState pageState, int startIndex,
063: String[] portalParams, String displayMode,
064: String targetWindowId, boolean isAJAXRequest)
065: throws ServletException, IOException {
066:
067: if (!isAJAXRequest) {
068: response.sendRedirect(pageState.getPageURL());
069: return;
070: } else if (request.getParameter("is_submit") == null) {
071: request.getRequestDispatcher("/admin/new_portlet.jsp")
072: .forward(request, response);
073: return;
074: }
075:
076: String portletName = request.getParameter(PORTLET_NAME_PARAM);
077: String colId = request.getParameter("renderable_id");
078: CompositeRenderable targetR = null;
079: if ("none".equals(colId))
080: targetR = pageState.portalPage.getRenderable();
081: else
082: targetR = (CompositeRenderable) pageState.portalPage
083: .getRenderable(colId);
084: Renderable r = null;
085: String optionLabel = request.getParameter(LABEL_PARAM);
086: PortletWindow pWindow = null;
087: PortalApplication portalApp = portalAppView
088: .getPortalApplication();
089: if (request.getParameter(ADD_TYPE_HEADER) != null) {
090: ((NavigationalLayout) targetR).addHeader(optionLabel);
091: } else if (request.getParameter(ADD_TYPE_URL) != null) {
092: ((NavigationalLayout) targetR).addExternalLink(optionLabel,
093: request.getParameter(URL_PARAM));
094: } else {
095: if (portletName.endsWith(".jsp")) {
096: try {
097: r = PortalConfiguration.getInstance()
098: .getRenderableForTemplate(portletName);
099: } catch (Exception e) {
100: throw new ServletException(
101: "Failed to create a composite renderable instance.",
102: e);
103: }
104: r.setTemplate(portletName);
105: targetR.addContent(r, optionLabel);
106: r.computeTemplatePath(request.getSession()
107: .getServletContext(), portalApp.getPath(),
108: portalApp.getTheme());
109: r.setPortalPage(pageState.portalPage);
110:
111: } else {
112: pWindow = new PortletWindow(pageState.portalPage,
113: portletName);
114: String windowTemplate = request
115: .getParameter(PORTLET_TEMPLATE_PARAM);
116: if (StringUtil.isNotNullOrEmpty(windowTemplate))
117: pWindow.setTemplate(windowTemplate);
118: if (request
119: .getParameter(AddPortletProcessor.HIDE_TITLE_PARAM) == null)
120: pWindow.setTitleShown(true);
121: else
122: pWindow.setTitleShown(false);
123: // Must add content before invoking computeTemplatePath since the parent container sets the template.
124: if (displayMode
125: .equals(PortalConstants.DISPLAY_MODE_DESKTOP))
126: targetR.addPortletWindow(pWindow);
127: else
128: targetR.addContent(pWindow, optionLabel);
129: pWindow.computeTemplatePath(request.getSession()
130: .getServletContext(), portalApp.getPath(),
131: portalApp.getTheme(), targetR
132: .getComponentTemplate());
133: r = pWindow;
134: }
135: }
136: if (isAJAXRequest) {
137: Writer w = response.getWriter();
138: if (r == null) {
139: w.write("ok");
140: } else {
141: if (pWindow != null) {
142: PortalUtil.renderPortletWindow(request, response,
143: pWindow, portalApp, displayMode, false);
144: } else {
145: request
146: .setAttribute(
147: PortalConstants.CURRENT_RENDERABLE_ATTRIBUTE,
148: r);
149: w.write("<div id=\"");
150: w.write(PortalConstants.PORTLET_NODE_ID_PREFIX);
151: w.write(r.getNumericId());
152: w.write("\">");
153: w.flush();
154: request.getRequestDispatcher(r.getTemplatePath())
155: .include(request, response);
156: w.flush();
157: w.write("</div>");
158: }
159:
160: }
161: } else {
162: ViewProcessor.getInstance().process(request, response,
163: sCache, portalAppView, pageState, startIndex,
164: portalParams, displayMode, targetWindowId,
165: isAJAXRequest);
166: }
167: }
168:
169: }
|