1 16 17 package org.pentaho.ui; 18 19 import java.io.File ; 20 import java.io.OutputStream ; 21 import java.util.ArrayList ; 22 import java.util.StringTokenizer ; 23 24 import org.pentaho.core.connection.IPentahoConnection; 25 import org.pentaho.core.connection.IPentahoResultSet; 26 import org.pentaho.core.runtime.IRuntimeContext; 27 import org.pentaho.core.services.BaseRequestHandler; 28 import org.pentaho.core.session.IPentahoSession; 29 import org.pentaho.core.solution.IParameterProvider; 30 import org.pentaho.core.solution.SimpleOutputHandler; 31 import org.pentaho.core.system.PentahoSystem; 32 import org.pentaho.core.ui.SimpleUrlFactory; 33 import org.pentaho.core.util.TemplateUtil; 34 import org.pentaho.core.util.UIUtil; 35 import org.pentaho.data.connection.sql.SQLConnection; 36 import org.pentaho.messages.Messages; 37 import org.pentaho.plugin.jfreechart.CategoryDatasetChartDefinition; 38 import org.pentaho.ui.component.DashboardWidgetComponent; 39 import org.pentaho.ui.component.charting.CategoryDatasetChartComponent; 40 import org.pentaho.ui.component.charting.PieDatasetChartComponent; 41 import org.pentaho.util.logging.ILogger; 42 43 public class ChartHelper { 44 45 public static boolean doAction(String solutionName, String actionPath, String actionName, String processId, IParameterProvider parameterProvider, OutputStream outputStream, IPentahoSession userSession, ArrayList messages, ILogger logger) { 46 47 SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, false); 48 BaseRequestHandler requestHandler = new BaseRequestHandler(userSession, null, outputHandler, parameterProvider, null); 49 50 requestHandler.setProcessId( processId ); 51 requestHandler.setAction(actionPath, actionName); 52 requestHandler.setSolutionName(solutionName); 53 54 IRuntimeContext runtime = null; 55 int status = IRuntimeContext.RUNTIME_STATUS_FAILURE; 56 try { 57 runtime = requestHandler.handleActionRequest(0, 0); 58 if( runtime != null ) { 59 status = runtime.getStatus(); 60 } 61 } finally { 62 if (runtime != null) { 63 runtime.dispose(); 64 } 65 } 66 return status == IRuntimeContext.RUNTIME_STATUS_SUCCESS; 67 } 68 69 public static IRuntimeContext doAction(String solutionName, String actionPath, String actionName, String processId, IParameterProvider parameterProvider, IPentahoSession userSession, ArrayList messages, ILogger logger) { 70 71 SimpleOutputHandler outputHandler = new SimpleOutputHandler(null, false); 72 BaseRequestHandler requestHandler = new BaseRequestHandler(userSession, null, outputHandler, parameterProvider, null); 73 74 requestHandler.setProcessId( processId ); 75 requestHandler.setAction(actionPath, actionName); 76 requestHandler.setSolutionName(solutionName); 77 78 IRuntimeContext runtime = null; 79 try { 80 runtime = requestHandler.handleActionRequest(0, 0); 81 } catch (Throwable t) { 82 } 83 return runtime; 84 } 85 86 public static boolean doPieChart(String solutionName, String actionPath, String actionName, IParameterProvider parameterProvider, StringBuffer outputStream, IPentahoSession userSession, ArrayList messages, ILogger logger) { 87 88 boolean result = true; 89 String outerParams = parameterProvider.getStringParameter("outer-params", null); String innerParam = parameterProvider.getStringParameter("inner-param", null); 92 String urlDrillTemplate = parameterProvider.getStringParameter("drill-url", null); String imageUrl = parameterProvider.getStringParameter("image-url", null); if (imageUrl == null) { 95 imageUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 96 } 97 98 if (urlDrillTemplate == null) { 99 urlDrillTemplate = ""; } 101 102 SimpleUrlFactory urlFactory = new SimpleUrlFactory(urlDrillTemplate); 103 104 PieDatasetChartComponent chartComponent = null; 105 try { 106 String chartDefinitionStr = solutionName + File.separator + actionPath + File.separator + actionName; 107 chartComponent = new PieDatasetChartComponent(chartDefinitionStr, urlFactory, messages); 108 if( logger != null ) { 109 chartComponent.setLoggingLevel( logger.getLoggingLevel() ); 110 } 111 chartComponent.validate(userSession, null); 112 chartComponent.setUrlTemplate(urlDrillTemplate); 113 if (outerParams != null) { 114 StringTokenizer tokenizer = new StringTokenizer (outerParams, ";"); while (tokenizer.hasMoreTokens()) { 116 chartComponent.addOuterParamName(tokenizer.nextToken()); 117 } 118 } 119 chartComponent.setParamName(innerParam); 120 121 chartComponent.setDataAction(chartDefinitionStr); 122 123 chartComponent.setParameterProvider("request", parameterProvider); 125 String content = chartComponent.getContent("text/html"); 127 if (content == null || content.equals("")) { content = " "; } 130 if (content == null) { 131 StringBuffer buffer = new StringBuffer (); 132 UIUtil.formatErrorMessage("text/html", Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, buffer); content = buffer.toString(); 134 result = false; 135 } 136 137 outputStream.append(content); 138 139 } finally { 140 if (chartComponent != null) 141 chartComponent.dispose(); 142 } 143 return result; 144 145 } 146 147 public static boolean doDial(String solutionName, String actionPath, String actionName, IParameterProvider parameterProvider, StringBuffer outputStream, IPentahoSession userSession, ArrayList messages, ILogger logger) { 148 149 boolean result = true; 150 String linkUrl = parameterProvider.getStringParameter("drill-url", null); String imageUrl = parameterProvider.getStringParameter("image-url", null); if (imageUrl == null) { 153 imageUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 154 } 155 156 if (linkUrl == null) { 157 linkUrl = ""; } 159 160 int width = (int) parameterProvider.getLongParameter("image-width", 150); int height = (int) parameterProvider.getLongParameter("image-height", 150); 163 SimpleUrlFactory urlFactory = new SimpleUrlFactory(linkUrl); 164 165 DashboardWidgetComponent widget = null; 166 try { 167 widget = new DashboardWidgetComponent(DashboardWidgetComponent.TYPE_DIAL, solutionName + File.separator + actionPath + File.separator + actionName, width, height, urlFactory, messages); 168 if( logger != null ) { 169 widget.setLoggingLevel( logger.getLoggingLevel() ); 170 } 171 widget.validate(userSession, null); 172 173 widget.setParameterProvider("request", parameterProvider); 175 double value = Double.parseDouble(parameterProvider.getStringParameter("value", "0")); widget.setValue(value); 177 178 String title = parameterProvider.getStringParameter("title", ""); widget.setTitle(title); 180 181 String content = widget.getContent("text/html"); 183 if (content == null) { 184 StringBuffer buffer = new StringBuffer (); 185 UIUtil.formatErrorMessage("text/html", Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, buffer); content = buffer.toString(); 187 result = false; 188 } 189 190 if (content == null || content.equals("")) { content = " "; } 193 outputStream.append(content); 194 195 } finally { 196 if (widget != null) 197 widget.dispose(); 198 } 199 return result; 200 201 } 202 203 public static boolean doChart(String solutionName, String actionPath, String actionName, IParameterProvider parameterProvider, StringBuffer outputStream, IPentahoSession userSession, ArrayList messages, ILogger logger) { 204 205 boolean result = true; 206 String outerParams = parameterProvider.getStringParameter("outer-params", null); String innerParam = parameterProvider.getStringParameter("inner-param", null); 209 String urlDrillTemplate = parameterProvider.getStringParameter("drill-url", null); String imageUrl = parameterProvider.getStringParameter("image-url", null); if (imageUrl == null) { 212 imageUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 213 } 214 215 if (urlDrillTemplate == null) { 216 urlDrillTemplate = ""; } 218 219 int width = (int) parameterProvider.getLongParameter("image-width", 150); int height = (int) parameterProvider.getLongParameter("image-height", 150); 222 SimpleUrlFactory urlFactory = new SimpleUrlFactory(urlDrillTemplate); 223 224 CategoryDatasetChartComponent chartComponent = null; 225 try { 226 String content = null; 227 try { 228 String chartDefinitionStr = solutionName + File.separator + actionPath + File.separator + actionName; 229 String chartTypeStr = parameterProvider.getStringParameter("chart-type", null); int chartType = CategoryDatasetChartDefinition.getChartType(chartTypeStr); 231 chartComponent = new CategoryDatasetChartComponent(chartType, chartDefinitionStr, width, height, urlFactory, messages); 232 if( logger != null ) { 233 chartComponent.setLoggingLevel( logger.getLoggingLevel() ); 234 } 235 chartComponent.validate(userSession, null); 236 chartComponent.setDataAction(chartDefinitionStr); 237 chartComponent.setUrlTemplate(urlDrillTemplate); 238 239 String connectionName = parameterProvider.getStringParameter("connection", null); String query = parameterProvider.getStringParameter("query", null); String dataAction = parameterProvider.getStringParameter("data-process", null); String seriesName = parameterProvider.getStringParameter("series-name", null); chartComponent.setSeriesName( seriesName ); 244 IPentahoConnection connection = null; 245 try { 246 chartComponent.setParamName(innerParam); 247 chartComponent.setParameterProvider("request", parameterProvider); if (connectionName != null && query != null) { 249 connection = new SQLConnection(connectionName, logger); 250 try { 251 query = TemplateUtil.applyTemplate(query, TemplateUtil.parametersToProperties(parameterProvider), null); 252 IPentahoResultSet results = connection.executeQuery(query); 253 chartComponent.setValues(results); 254 } finally { 255 } 256 chartComponent.setUrlTemplate(urlDrillTemplate); 257 if (outerParams != null) { 258 StringTokenizer tokenizer = new StringTokenizer (outerParams, ";"); while (tokenizer.hasMoreTokens()) { 260 chartComponent.addOuterParamName(tokenizer.nextToken()); 261 } 262 } 263 } else if (dataAction != null) { 264 String actionInfo[] = PentahoSystem.parseActionString(dataAction); 265 if (actionInfo != null && actionInfo.length == 3) { 266 chartComponent.setDataAction(actionInfo[0], actionInfo[1], actionInfo[2], "rule-result"); } 268 } else { 269 } 271 content = chartComponent.getContent("text/html"); 273 } finally { 274 if (connection != null) { 275 connection.close(); 276 } 277 } 278 279 280 } catch (Exception e) { 281 } 283 try { 284 if (content == null) { 285 StringBuffer buffer = new StringBuffer (); 286 UIUtil.formatErrorMessage("text/html", Messages.getErrorString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, buffer); content = buffer.toString(); 288 result = false; 289 } 290 outputStream.append(content); 291 } catch (Exception e) { 292 293 } 294 } finally { 295 if (chartComponent != null) 296 chartComponent.dispose(); 297 } 298 return result; 299 300 } 301 302 } 303 | Popular Tags |