001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Sep 22, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.portlet;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021:
022: import javax.portlet.ActionRequest;
023: import javax.portlet.ActionResponse;
024: import javax.portlet.PortletException;
025: import javax.portlet.PortletPreferences;
026: import javax.portlet.PortletSession;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.pentaho.core.runtime.IBackgroundExecution;
034: import org.pentaho.core.solution.HttpRequestParameterProvider;
035: import org.pentaho.core.solution.HttpSessionParameterProvider;
036: import org.pentaho.core.solution.IOutputHandler;
037: import org.pentaho.core.subscribe.SubscriptionHelper;
038: import org.pentaho.core.system.PentahoSystem;
039: import org.pentaho.core.util.IUITemplater;
040: import org.pentaho.messages.Messages;
041: import org.pentaho.ui.component.ActionComponent;
042:
043: public class ActionPortlet extends ViewPortlet {
044:
045: private final static String ACTION = "action"; //$NON-NLS-1$
046:
047: private static final Log portletLogger = LogFactory
048: .getLog(ActionPortlet.class);
049:
050: public Log getLogger() {
051: return portletLogger;
052: }
053:
054: public void processPortletAction(ActionRequest request,
055: ActionResponse response, PentahoPortletSession userSession)
056: throws PortletException, IOException {
057:
058: PortletRequestParameterProvider requestParameters = new PortletRequestParameterProvider(
059: request);
060: String solutionName = requestParameters.getStringParameter(
061: "solution", null); //$NON-NLS-1$
062: String actionPath = requestParameters.getStringParameter(
063: "path", null); //$NON-NLS-1$
064: String actionName = requestParameters.getStringParameter(
065: "action", null); //$NON-NLS-1$
066: String subscribeAction = requestParameters.getStringParameter(
067: "subscribe", null); //$NON-NLS-1$
068: if (subscribeAction != null) {
069: String key = "pentaho-message-" + getPortletName(); //$NON-NLS-1$
070: if ("save".equals(subscribeAction)) { //$NON-NLS-1$
071: String actionReference = solutionName
072: + "/" + actionPath + "/" + actionName; //$NON-NLS-1$ //$NON-NLS-2$
073: String result = SubscriptionHelper
074: .saveSubscription(requestParameters,
075: actionReference, userSession);
076: userSession.setAttribute(key, result,
077: PortletSession.PORTLET_SCOPE);
078: } else if ("delete".equals(subscribeAction)) { //$NON-NLS-1$
079: String name = requestParameters.getStringParameter(
080: "subscribe-name", null); //$NON-NLS-1$
081: String result = SubscriptionHelper.deleteSubscription(
082: name, userSession);
083: userSession.setAttribute(key, result,
084: PortletSession.PORTLET_SCOPE);
085: } else if ("edit".equals(subscribeAction)) { //$NON-NLS-1$
086: // TODO
087: } else if ("archive".equals(subscribeAction)) { //$NON-NLS-1$
088: String name = requestParameters.getStringParameter(
089: "subscribe-name", null); //$NON-NLS-1$
090: PortletSessionParameterProvider sessionParameters = new PortletSessionParameterProvider(
091: userSession);
092: String result = SubscriptionHelper
093: .createSubscriptionArchive(name, userSession,
094: null, sessionParameters);
095: userSession.setAttribute(key, result,
096: PortletSession.PORTLET_SCOPE);
097: } else if ("delete-archived".equals(subscribeAction)) { //$NON-NLS-1$
098: String name = requestParameters.getStringParameter(
099: "subscribe-name", null); //$NON-NLS-1$
100: int pos = name.lastIndexOf(':');
101: if (pos != -1) {
102: String fileId = name.substring(pos + 1);
103: name = name.substring(0, pos);
104: String result = SubscriptionHelper
105: .deleteSubscriptionArchive(name, fileId,
106: userSession);
107: userSession.setAttribute(key, result,
108: PortletSession.PORTLET_SCOPE);
109: }
110: }
111:
112: }
113:
114: }
115:
116: public void doPortletView(RenderRequest request,
117: RenderResponse response, PentahoPortletSession userSession)
118: throws PortletException, IOException {
119:
120: // we don't want to use a portlet url factory as we are probably
121: // generating a popup window
122: // TODO make this configurable
123:
124: String key = "pentaho-message-" + getPortletName(); //$NON-NLS-1$
125: String message = (String) userSession.getAttribute(key,
126: PortletSession.PORTLET_SCOPE);
127: int outputPreference = IOutputHandler.OUTPUT_TYPE_DEFAULT;
128: PortletRequestParameterProvider requestParameters = new PortletRequestParameterProvider(
129: request);
130:
131: String subscribeAction = requestParameters.getStringParameter(
132: "subscribe", null); //$NON-NLS-1$
133: if ("edit".equals(subscribeAction)) { //$NON-NLS-1$
134: // TODO
135: // get the action information from the subscription
136: String name = requestParameters.getStringParameter(
137: "subscribe-name", null); //$NON-NLS-1$
138: message = SubscriptionHelper.getSubscriptionParameters(
139: name, requestParameters, userSession);
140: outputPreference = IOutputHandler.OUTPUT_TYPE_PARAMETERS;
141: }
142:
143: if (message != null) {
144: response.setContentType("text/html"); //$NON-NLS-1$
145: PrintWriter writer = response.getWriter();
146: writer
147: .println("<span class=\"portlet-font\">Message: " + message + "</span>"); //$NON-NLS-1$ //$NON-NLS-2$
148: userSession.removeAttribute(
149: "pentaho-message", PortletSession.PORTLET_SCOPE); //$NON-NLS-1$
150: }
151:
152: doPortletView(outputPreference, requestParameters, request,
153: response, userSession);
154: }
155:
156: public void doPortletView(int outputPreference,
157: PortletRequestParameterProvider requestParameters,
158: RenderRequest request, RenderResponse response,
159: PentahoPortletSession userSession) throws IOException {
160:
161: // we don't want to use a portlet url factory as we are probably
162: // generating a popup window
163: // TODO make this configurable
164:
165: PortletUrlFactory urlFactory = new PortletUrlFactory(response,
166: request.getWindowState(), request.getPortletMode());
167: // SimpleUrlFactory urlFactory = new SimpleUrlFactory(
168: // PentahoSystem.getApplicationContext().getBaseUrl() + "ViewAction?" );
169: // //$NON-NLS-1$
170: PortletPreferences prefs = request.getPreferences();
171: PortletPreferencesParameterProvider portletPrefsParameters = new PortletPreferencesParameterProvider(
172: prefs);
173: PortletSessionParameterProvider sessionParameters = new PortletSessionParameterProvider(
174: userSession);
175:
176: String actionString = request.getParameter(ACTION);
177: if (actionString == null) {
178: // get the default value from the preferences
179: actionString = prefs.getValue(ACTION, null);
180: if (actionString != null) {
181: requestParameters.setParameter(ACTION, actionString);
182: //portletPrefsParameters.setParameter(ACTION, actionString);
183: }
184: }
185: String backgroundExecution = request.getParameter("background"); //$NON-NLS-1$
186: if (backgroundExecution == null) {
187: backgroundExecution = prefs.getValue("background", null); //$NON-NLS-1$
188: if (backgroundExecution != null) {
189: requestParameters.setParameter(
190: "background", backgroundExecution); //$NON-NLS-1$
191: }
192: }
193: // TODO sbarkdull, not sure the next 4 lines are needed
194: // String solutionName = request.getParameter("solution");
195: // String actionName = actionString;
196: // String actionPath = request.getParameter("path");
197: // if (actionString != null && solutionName == null && actionPath == null ) {
198:
199: String solutionName = null;
200: String actionName = null;
201: String actionPath = null;
202: if (actionString != null) {
203: PentahoSystem.ActionInfo info = PentahoSystem
204: .parseActionString(actionString);
205: if (info != null) {
206: solutionName = info.getSolutionName();
207: actionPath = info.getPath();
208: actionName = info.getActionName();
209: }
210:
211: }
212:
213: if (actionString == null || solutionName == null
214: || actionPath == null || actionName == null) {
215: error(Messages
216: .getString("ActionPortlet.ERROR_0001_COULD_NOT_PARSE_ACTION")); //$NON-NLS-1$
217: PrintWriter writer = response.getWriter();
218: response.setContentType("text/html"); //$NON-NLS-1$
219: writer
220: .print(Messages
221: .getString("ActionPortlet.ERROR_0001_COULD_NOT_PARSE_ACTION")); //$NON-NLS-1$
222: return;
223:
224: }
225: if ("true".equals(backgroundExecution)) { //$NON-NLS-1$
226: IBackgroundExecution backgroundExecutionHandler = PentahoSystem
227: .getBackgroundExecutionHandler(userSession);
228: if (backgroundExecutionHandler != null) {
229: PortletRequestParameterProvider parameterProvider = new PortletRequestParameterProvider(
230: request);
231: parameterProvider
232: .setParameter("solution", solutionName); //$NON-NLS-1$
233: parameterProvider.setParameter("path", actionPath); //$NON-NLS-1$
234: parameterProvider.setParameter("action", actionName); //$NON-NLS-1$
235: String backgroundResponse = backgroundExecutionHandler
236: .backgroundExecuteAction(userSession,
237: parameterProvider);
238: String intro = ""; //$NON-NLS-1$
239: String footer = ""; //$NON-NLS-1$
240: IUITemplater templater = PentahoSystem
241: .getUITemplater(userSession);
242: if (templater != null) {
243: String sections[] = templater.breakTemplate(
244: "template-dialog.html", "", userSession); //$NON-NLS-1$ //$NON-NLS-2$
245: if (sections != null && sections.length > 0) {
246: intro = sections[0];
247: }
248: if (sections != null && sections.length > 1) {
249: footer = sections[1];
250: }
251: } else {
252: intro = Messages
253: .getString("UI.ERROR_0002_BAD_TEMPLATE_OBJECT"); //$NON-NLS-1$
254: }
255: PrintWriter writer = response.getWriter();
256: writer.print(intro);
257: writer.print(backgroundResponse);
258: writer.print(footer);
259: return;
260: }
261: }
262:
263: ActionComponent component = new ActionComponent(solutionName,
264: actionPath, actionName, null, outputPreference,
265: urlFactory, null);
266: component.setParameterProvider(
267: HttpRequestParameterProvider.SCOPE_REQUEST,
268: requestParameters);
269: component.setParameterProvider(
270: HttpSessionParameterProvider.SCOPE_SESSION,
271: sessionParameters);
272: component
273: .setParameterProvider(
274: PortletPreferencesParameterProvider.SCOPE_PORTLET_PREFERENCES,
275: portletPrefsParameters);
276: component.validate(userSession, null);
277:
278: String content = component.getContent("text/html"); //$NON-NLS-1$
279:
280: if (StringUtils.isEmpty(content)) {
281: content = " "; //$NON-NLS-1$
282: }
283: response.setContentType("text/html"); //$NON-NLS-1$
284: PrintWriter writer = response.getWriter();
285: writer.print(content);
286:
287: }
288:
289: }
|