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 Apr 17, 2006
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui;
018:
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.StringTokenizer;
022:
023: import org.pentaho.core.session.IPentahoSession;
024: import org.pentaho.core.solution.HttpRequestParameterProvider;
025: import org.pentaho.core.solution.IParameterProvider;
026: import org.pentaho.core.system.PentahoSystem;
027: import org.pentaho.core.ui.SimpleUrlFactory;
028: import org.pentaho.core.util.UIUtil;
029: import org.pentaho.messages.Messages;
030: import org.pentaho.ui.component.charting.FlashChartComponent;
031: import org.pentaho.util.logging.ILogger;
032:
033: /**
034: * This class provides wrapper functions to make it easier to generate flash
035: * charts within a dashboard environment.
036: */
037: public class FlashChartHelper {
038:
039: /**
040: * doFlashChart generates a flash media player chart. It provides a simple
041: * wrapper around the com.pentaho.ui.charting.FlashChartComponent
042: *
043: * @param solutionName the solution name
044: * @param actionPath the action path
045: * @param chartName the xml file describing the chart
046: * @param parameterProvider the collection of parameters to customize the chart
047: * @param outputStream the output string buffer for the content
048: * @param userSession the user session object
049: * @param messages a collection to store error and logging messages
050: * @param logger logging object
051: *
052: * @return true if successful
053: */
054: public static boolean doFlashChart(String solutionName,
055: String actionPath, String chartName,
056: IParameterProvider parameterProvider,
057: StringBuffer outputStream, IPentahoSession userSession,
058: ArrayList messages, ILogger logger) {
059:
060: boolean result = true;
061: String outerParams = parameterProvider.getStringParameter(
062: "outer-params", null); //$NON-NLS-1$
063: String innerParam = parameterProvider.getStringParameter(
064: "inner-param", null); //$NON-NLS-1$
065:
066: String urlDrillTemplate = parameterProvider.getStringParameter(
067: "drill-url", null); //$NON-NLS-1$
068: String imageUrl = parameterProvider.getStringParameter(
069: "image-url", null); //$NON-NLS-1$
070: if (imageUrl == null) {
071: imageUrl = PentahoSystem.getApplicationContext()
072: .getBaseUrl();
073: }
074:
075: if (urlDrillTemplate == null) {
076: urlDrillTemplate = ""; //$NON-NLS-1$
077: }
078:
079: int width = (int) parameterProvider.getLongParameter(
080: "image-width", 150); //$NON-NLS-1$
081: int height = (int) parameterProvider.getLongParameter(
082: "image-height", 150); //$NON-NLS-1$
083: int topX = (int) parameterProvider.getLongParameter("topX", -1); //$NON-NLS-1$
084:
085: SimpleUrlFactory urlFactory = new SimpleUrlFactory(
086: urlDrillTemplate);
087:
088: FlashChartComponent chartComponent = null;
089: try {
090: String chartDefinitionStr = solutionName + File.separator
091: + actionPath + File.separator + chartName;
092: chartComponent = new FlashChartComponent(
093: chartDefinitionStr, width, height, urlFactory,
094: messages);
095: if (logger != null) {
096: chartComponent
097: .setLoggingLevel(logger.getLoggingLevel());
098: }
099: chartComponent.validate(userSession, null);
100: chartComponent.setUrlTemplate(urlDrillTemplate);
101: if (outerParams != null) {
102: StringTokenizer tokenizer = new StringTokenizer(
103: outerParams, ";"); //$NON-NLS-1$
104: while (tokenizer.hasMoreTokens()) {
105: chartComponent.addOuterParamName(tokenizer
106: .nextToken());
107: }
108: }
109: chartComponent.setParamName(innerParam);
110: chartComponent.setTopX(topX);
111:
112: chartComponent.setDataAction(chartDefinitionStr);
113:
114: chartComponent.setParameterProvider(
115: HttpRequestParameterProvider.SCOPE_REQUEST,
116: parameterProvider);
117:
118: String content = chartComponent.getContent("text/html"); //$NON-NLS-1$
119:
120: if (content == null || content.equals("")) { //$NON-NLS-1$
121: content = " "; //$NON-NLS-1$
122: }
123: if (content == null) {
124: StringBuffer buffer = new StringBuffer();
125: UIUtil
126: .formatErrorMessage(
127: "text/html", Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, buffer); //$NON-NLS-1$ //$NON-NLS-2$
128: content = buffer.toString();
129: result = false;
130: }
131:
132: outputStream.append(content);
133:
134: } finally {
135: if (chartComponent != null)
136: chartComponent.dispose();
137: }
138: return result;
139:
140: }
141:
142: /**
143: * doFlashDial generates a flash media player dial. It provides a simple
144: * wrapper around the com.pentaho.ui.charting.FlashChartComponent
145: *
146: * @param solutionName the solution name
147: * @param actionPath the action path
148: * @param chartName the xml file describing the chart
149: * @param parameterProvider the collection of parameters to customize the chart
150: * @param outputStream the output string buffer for the content
151: * @param userSession the user session object
152: * @param messages a collection to store error and logging messages
153: * @param logger logging object
154: *
155: * @return true if successful
156: */
157: public static boolean doFlashDial(String solutionName,
158: String actionPath, String actionName,
159: IParameterProvider parameterProvider,
160: StringBuffer outputStream, IPentahoSession userSession,
161: ArrayList messages, ILogger logger) {
162:
163: boolean result = true;
164: String urlDrillTemplate = parameterProvider.getStringParameter(
165: "drill-url", null); //$NON-NLS-1$
166: String imageUrl = parameterProvider.getStringParameter(
167: "image-url", null); //$NON-NLS-1$
168: if (imageUrl == null) {
169: imageUrl = PentahoSystem.getApplicationContext()
170: .getBaseUrl();
171: }
172:
173: if (urlDrillTemplate == null) {
174: urlDrillTemplate = ""; //$NON-NLS-1$
175: }
176:
177: int width = (int) parameterProvider.getLongParameter(
178: "image-width", 150); //$NON-NLS-1$
179: int height = (int) parameterProvider.getLongParameter(
180: "image-height", 150); //$NON-NLS-1$
181: int topX = (int) parameterProvider.getLongParameter("topX", -1); //$NON-NLS-1$
182:
183: SimpleUrlFactory urlFactory = new SimpleUrlFactory(
184: urlDrillTemplate);
185:
186: FlashChartComponent chartComponent = null;
187: try {
188: String chartDefinitionStr = solutionName + File.separator
189: + actionPath + File.separator + actionName;
190: chartComponent = new FlashChartComponent(
191: chartDefinitionStr, width, height, urlFactory,
192: messages);
193: if (logger != null) {
194: chartComponent
195: .setLoggingLevel(logger.getLoggingLevel());
196: }
197: chartComponent.validate(userSession, null);
198: chartComponent.setUrlTemplate(urlDrillTemplate);
199: chartComponent.setTopX(topX);
200:
201: chartComponent.setDataAction(chartDefinitionStr);
202:
203: chartComponent.setParameterProvider(
204: HttpRequestParameterProvider.SCOPE_REQUEST,
205: parameterProvider);
206:
207: String content = chartComponent.getDialContent("text/html"); //$NON-NLS-1$
208:
209: if (content == null || content.equals("")) { //$NON-NLS-1$
210: content = " "; //$NON-NLS-1$
211: }
212:
213: if (content == null) {
214: StringBuffer buffer = new StringBuffer();
215: UIUtil
216: .formatErrorMessage(
217: "text/html", Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, buffer); //$NON-NLS-1$ //$NON-NLS-2$
218: content = buffer.toString();
219: result = false;
220: }
221:
222: outputStream.append(content);
223:
224: } finally {
225: if (chartComponent != null)
226: chartComponent.dispose();
227: }
228: return result;
229: }
230: }
|