KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > portlet > ActionPortlet


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Sep 22, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.portlet;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import javax.portlet.ActionRequest;
22 import javax.portlet.ActionResponse;
23 import javax.portlet.PortletException;
24 import javax.portlet.PortletPreferences;
25 import javax.portlet.RenderRequest;
26 import javax.portlet.RenderResponse;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.pentaho.core.runtime.IBackgroundExecution;
30 import org.pentaho.core.solution.IOutputHandler;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.core.util.IUITemplater;
33 import org.pentaho.messages.Messages;
34 import org.pentaho.ui.component.ActionComponent;
35
36 public class ActionPortlet extends ViewPortlet {
37
38     private final static String JavaDoc ACTION = "action"; //$NON-NLS-1$
39

40     private static final Log portletLogger = LogFactory.getLog(ActionPortlet.class);
41
42     public Log getLogger() {
43         return portletLogger;
44     }
45
46     public void processPortletAction(ActionRequest request, ActionResponse response, PentahoPortletSession userSession) throws PortletException, IOException JavaDoc {
47         // TODO Auto-generated method stub
48

49     }
50
51     public void doPortletView(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, IOException JavaDoc {
52
53         // we don't want to use a portlet url factory as we are probably
54
// generating a popup window
55
// TODO make this configurable
56

57         PortletRequestParameterProvider requestParameters = new PortletRequestParameterProvider(request);
58
59         doPortletView(IOutputHandler.OUTPUT_TYPE_DEFAULT, requestParameters, request, response, userSession);
60
61     }
62
63     public void doPortletView(int outputPreference, PortletRequestParameterProvider requestParameters, RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws IOException JavaDoc {
64
65         // we don't want to use a portlet url factory as we are probably
66
// generating a popup window
67
// TODO make this configurable
68

69         PortletUrlFactory urlFactory = new PortletUrlFactory(response, request.getWindowState(), request.getPortletMode());
70         // SimpleUrlFactory urlFactory = new SimpleUrlFactory(
71
// PentahoSystem.getApplicationContext().getBaseUrl() + "ViewAction?" );
72
// //$NON-NLS-1$
73
PortletPreferences prefs = request.getPreferences();
74         PortletSessionParameterProvider sessionParameters = new PortletSessionParameterProvider(userSession);
75
76         String JavaDoc actionString = request.getParameter(ACTION);
77         if (actionString == null) {
78             // get the default value from the preferences
79
actionString = prefs.getValue(ACTION, null);
80             if (actionString != null) {
81                 requestParameters.setParameter(ACTION, actionString);
82             }
83         }
84         String JavaDoc backgroundExecution = request.getParameter("background"); //$NON-NLS-1$
85
if (backgroundExecution == null) {
86           backgroundExecution = prefs.getValue("background", null); //$NON-NLS-1$
87
if (backgroundExecution != null) {
88             requestParameters.setParameter("background", backgroundExecution); //$NON-NLS-1$
89
}
90         }
91         String JavaDoc solutionName = null;
92         String JavaDoc actionName = null;
93         String JavaDoc actionPath = null;
94         if (actionString != null) {
95             String JavaDoc info[] = PentahoSystem.parseActionString(actionString);
96             if (info != null && info.length == 3) {
97                 solutionName = info[0];
98                 actionPath = info[1];
99                 actionName = info[2];
100             }
101
102         }
103         if ("true".equals(backgroundExecution)) { //$NON-NLS-1$
104
IBackgroundExecution backgroundExecutionHandler = PentahoSystem.getBackgroundExecutionHandler(userSession);
105           if (backgroundExecutionHandler != null) {
106             PortletRequestParameterProvider parameterProvider = new PortletRequestParameterProvider(request);
107             parameterProvider.setParameter("solution", solutionName); //$NON-NLS-1$
108
parameterProvider.setParameter("path", actionPath); //$NON-NLS-1$
109
parameterProvider.setParameter("action", actionName); //$NON-NLS-1$
110
String JavaDoc backgroundResponse = backgroundExecutionHandler.backgroundExecuteAction(userSession, parameterProvider);
111             String JavaDoc intro = ""; //$NON-NLS-1$
112
String JavaDoc footer = ""; //$NON-NLS-1$
113
IUITemplater templater = PentahoSystem.getUITemplater( userSession );
114             if( templater != null ) {
115               String JavaDoc sections[] = templater.breakTemplate( "template-dialog.html", "", userSession ); //$NON-NLS-1$ //$NON-NLS-2$
116
if( sections != null && sections.length > 0 ) {
117                 intro = sections[0];
118               }
119               if( sections != null && sections.length > 1 ) {
120                 footer = sections[1];
121               }
122             } else {
123               intro = Messages.getString( "UI.ERROR_0002_BAD_TEMPLATE_OBJECT" ); //$NON-NLS-1$
124
}
125             PrintWriter JavaDoc writer = response.getWriter();
126             writer.print(intro);
127             writer.print(backgroundResponse);
128             writer.print(footer);
129             return;
130           }
131         }
132         
133         if (actionString == null || solutionName == null || actionPath == null || actionName == null) {
134             error(Messages.getString("ActionPortlet.ERROR_0001_COULD_NOT_PARSE_ACTION")); //$NON-NLS-1$
135
PrintWriter JavaDoc writer = response.getWriter();
136             writer.print(Messages.getString("ActionPortlet.ERROR_0001_COULD_NOT_PARSE_ACTION")); //$NON-NLS-1$
137
return;
138
139         }
140
141         ActionComponent component = new ActionComponent(solutionName, actionPath, actionName, null, outputPreference, urlFactory, null);
142         component.setParameterProvider("request", requestParameters); //$NON-NLS-1$
143
component.setParameterProvider("session", sessionParameters); //$NON-NLS-1$
144
component.validate(userSession, null);
145
146         String JavaDoc content = component.getContent("text/html"); //$NON-NLS-1$
147
if (content == null || content.equals("")) { //$NON-NLS-1$
148
content = " "; //$NON-NLS-1$
149
}
150         response.setContentType("text/html"); //$NON-NLS-1$
151
PrintWriter JavaDoc writer = response.getWriter();
152         writer.print(content);
153
154     }
155
156 }
157
Popular Tags