KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > charting > AbstractChartComponent


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 Nov 17, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.ui.component.charting;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.lang.reflect.Array JavaDoc;
24 import java.net.URLEncoder JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.pentaho.core.connection.IPentahoResultSet;
33 import org.pentaho.core.runtime.IActionParameter;
34 import org.pentaho.core.runtime.IRuntimeContext;
35 import org.pentaho.core.solution.ISolutionEngine;
36 import org.pentaho.core.solution.SimpleOutputHandler;
37 import org.pentaho.core.system.PentahoSystem;
38 import org.pentaho.core.ui.IPentahoUrlFactory;
39 import org.pentaho.core.util.TemplateUtil;
40 import org.pentaho.messages.util.LocaleHelper;
41 import org.pentaho.ui.XmlComponent;
42 import org.pentaho.util.logging.ILogger;
43
44 public abstract class AbstractChartComponent extends XmlComponent {
45
46     protected static final String JavaDoc CHART_NODE_NAME = "chart"; //$NON-NLS-1$
47
protected static final String JavaDoc URLTEMPLE_NODE_NAME = "url-template"; //$NON-NLS-1$
48
protected static final String JavaDoc PARAM2_NODE_NAME = "series-name"; //$NON-NLS-1$
49

50     public static final int FILENAME_INDEX = 0;
51
52     public static final int FILENAME_WITHOUT_EXTENSION_INDEX = 1;
53
54     protected String JavaDoc definitionPath;
55
56     protected int width = -1;
57
58     protected int height = -1;
59
60     protected String JavaDoc title;
61
62     protected Object JavaDoc values;
63
64     protected boolean byRow = true;
65
66     protected String JavaDoc solution;
67
68     protected String JavaDoc actionPath;
69
70     protected String JavaDoc actionName;
71
72     protected String JavaDoc actionOutput;
73
74     protected String JavaDoc instanceId = null;
75
76     protected IRuntimeContext context;
77
78     protected String JavaDoc urlTemplate = null;
79
80     protected List JavaDoc outerParamNames = new ArrayList JavaDoc();
81
82     protected String JavaDoc paramName; // Assumes 1 parameter subclasses may need more
83

84     protected static int chartCount = 0;
85
86     protected static Log logger = null;
87
88     public AbstractChartComponent(String JavaDoc definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
89         this(urlFactory, messages);
90         this.definitionPath = definitionPath;
91         this.width = width;
92         this.height = height;
93         String JavaDoc info[] = PentahoSystem.parseActionString(definitionPath);
94         if (info != null && info.length == 3) {
95             setSourcePath(info[0] + File.separator + info[2]);
96         }
97     }
98
99     /**
100      * @param definitionPath
101      * @param urlFactory
102      * @param messages
103      */

104     public AbstractChartComponent(String JavaDoc definitionPath, IPentahoUrlFactory urlFactory, ArrayList JavaDoc messages) {
105         this(urlFactory, messages);
106         this.definitionPath = definitionPath;
107         String JavaDoc info[] = PentahoSystem.parseActionString(definitionPath);
108         if (info != null && info.length == 3) {
109             setSourcePath(info[0] + File.separator + info[2]);
110         }
111     }
112
113     public AbstractChartComponent(IPentahoUrlFactory urlFactory, List JavaDoc messages) {
114         super(urlFactory, messages, null);
115         logger = LogFactory.getLog(this.getClass());
116     }
117
118     /**
119      * @param chartDefinition
120      * String that represents a file in the solution to create the
121      * chart from.
122      * @return
123      */

124     public abstract boolean setDataAction(String JavaDoc chartDefinition);
125
126     /**
127      * Sets the action to be executed to get the data for the pies
128      *
129      * @param solution
130      * @param actionPath
131      * @param actionName
132      * @param actionOutput
133      */

134     public void setDataAction(String JavaDoc solution, String JavaDoc actionPath, String JavaDoc actionName, String JavaDoc actionOutput) {
135         this.solution = solution;
136         this.actionPath = actionPath;
137         this.actionName = actionName;
138         this.actionOutput = actionOutput;
139     }
140
141     /**
142      * Gets a IPentahoResultSet from the action output
143      *
144      * @return IPentahoResultSet
145      */

146     public IPentahoResultSet getActionData() {
147         // create an instance of the solution engine to execute the specified
148
// action
149

150         ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(getSession());
151         solutionEngine.setLoggingLevel(ILogger.DEBUG);
152         solutionEngine.init(getSession());
153
154         HashMap JavaDoc parameterProviders = getParameterProviders();
155
156         OutputStream JavaDoc outputStream = null;
157         SimpleOutputHandler outputHandler = null;
158         outputHandler = new SimpleOutputHandler(outputStream, false);
159
160         ArrayList JavaDoc messages = new ArrayList JavaDoc();
161         String JavaDoc processId = this.getClass().getName();
162         context = solutionEngine.execute(solution, actionPath, actionName, processId, false, true, instanceId, false, parameterProviders, outputHandler, null, urlFactory, messages);
163
164         if( context == null ) {
165             // this went badly wrong
166
return null;
167         }
168         
169         if (actionOutput != null) {
170             if (context.getOutputNames().contains(actionOutput)) {
171                 IActionParameter output = context.getOutputParameter(actionOutput);
172                 IPentahoResultSet results = output.getValueAsResultSet();
173                 if (results != null) {
174                     results = results.memoryCopy();
175                 }
176                 return results;
177             } else {
178                 // this is an error
179
return null;
180             }
181         } else {
182             try {
183                 // return the first list that we find...
184
Iterator JavaDoc it = context.getOutputNames().iterator();
185                 while (it.hasNext()) {
186                     IActionParameter output = (IActionParameter) it.next();
187                     if (output.getType().equalsIgnoreCase(IActionParameter.TYPE_RESULT_SET)) {
188                         IPentahoResultSet results = output.getValueAsResultSet();
189                         if (results != null) {
190                             results = results.memoryCopy();
191                         }
192                         return results;
193                     }
194                 }
195             } catch (Exception JavaDoc e) {
196
197             }
198         }
199         return null;
200     }
201
202     public Log getLogger() {
203         return logger;
204     }
205
206     /**
207      * @return String that represents the file path to a temporary file
208      */

209     protected String JavaDoc[] createTempFile() {
210         // create temporary file names
211
String JavaDoc solutionDir = "system/tmp/"; //$NON-NLS-1$
212
String JavaDoc fileNamePrefix = "tmp_chart_"; //$NON-NLS-1$
213
String JavaDoc extension = ".png"; //$NON-NLS-1$
214
String JavaDoc fileName = null;
215         String JavaDoc filePathWithoutExtension = null;
216         try {
217             File JavaDoc file = File.createTempFile(fileNamePrefix, extension, new File JavaDoc(PentahoSystem.getApplicationContext().getFileOutputPath(solutionDir)));
218             // file.deleteOnExit();
219
fileName = file.getName();
220             filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.'));
221         } catch (IOException JavaDoc e) {
222             // TODO Auto-generated catch block
223
e.printStackTrace();
224         }
225         String JavaDoc[] value = new String JavaDoc[2];
226         value[FILENAME_INDEX] = fileName;
227         value[FILENAME_WITHOUT_EXTENSION_INDEX] = filePathWithoutExtension;
228
229         return value;
230     }
231
232     protected void applyOuterURLTemplateParam() {
233         if (outerParamNames == null) {
234             return;
235         }
236         Iterator JavaDoc iter = outerParamNames.iterator();
237         while (iter.hasNext()) {
238             String JavaDoc outerParamName = iter.next().toString();
239             Object JavaDoc value = null;
240             if (context != null && context.getInputNames().contains(outerParamName)) {
241                 value = context.getInputParameterValue(outerParamName);
242             }
243             if (value == null) {
244                 return;
245             }
246             try {
247                 if (value.getClass().isArray()) {
248                     if ( Array.getLength( value ) > 0 ) {
249                         String JavaDoc[] encodedVals = new String JavaDoc[ Array.getLength( value ) ];
250                         for ( int i = 0; i < Array.getLength( value ); ++i ) {
251                             encodedVals[i] = URLEncoder.encode( Array.get( value, i ).toString(), LocaleHelper.getSystemEncoding() );
252                         }
253                         // TODO Sleeze Alert!!! This is a temporary hack for making the
254
// URLs generated support multiple selections. A JIRA case PLATFORM-393
255
// has been generated to address this issue.
256
//
257
// For now, applyTemplate looks for an "&" or "?" preceding and following the param, uses all
258
// the characters between them as the template and repeats it once for each param value
259
// separating them with '&'
260
urlTemplate = TemplateUtil.applyTemplate(urlTemplate, outerParamName, encodedVals);
261                     }
262                 }
263                 else {
264                     String JavaDoc encodedVal = URLEncoder.encode( value.toString(), LocaleHelper.getSystemEncoding() );
265                     urlTemplate = TemplateUtil.applyTemplate(urlTemplate, outerParamName, encodedVal);
266                 }
267                 
268                 //encodedVal = URLEncoder.encode(stringVal, LocaleHelper.getSystemEncoding()); //$NON-NLS-1$
269
} catch (UnsupportedEncodingException JavaDoc e) {
270                 // TODO Auto-generated catch block
271
e.printStackTrace();
272             }
273         }
274     }
275
276     /**
277      *
278      */

279     public void dispose() {
280         if (context != null)
281             context.dispose();
282     }
283
284     /**
285      * @return Returns the actionName.
286      */

287     public String JavaDoc getActionName() {
288         return actionName;
289     }
290
291     /**
292      * @param actionName
293      * The actionName to set.
294      */

295     public void setActionName(String JavaDoc actionName) {
296         this.actionName = actionName;
297     }
298
299     /**
300      * @return Returns the actionOutput.
301      */

302     public String JavaDoc getActionOutput() {
303         return actionOutput;
304     }
305
306     /**
307      * @param actionOutput
308      * The actionOutput to set.
309      */

310     public void setActionOutput(String JavaDoc actionOutput) {
311         this.actionOutput = actionOutput;
312     }
313
314     /**
315      * @return Returns the actionPath.
316      */

317     public String JavaDoc getActionPath() {
318         return actionPath;
319     }
320
321     /**
322      * @param actionPath
323      * The actionPath to set.
324      */

325     public void setActionPath(String JavaDoc actionPath) {
326         this.actionPath = actionPath;
327     }
328
329     /**
330      * @return Returns the context.
331      */

332     public IRuntimeContext getContext() {
333         return context;
334     }
335
336     /**
337      * @param context
338      * The context to set.
339      */

340     public void setContext(IRuntimeContext context) {
341         this.context = context;
342     }
343
344     /**
345      * @return Returns the definitionPath.
346      */

347     public String JavaDoc getDefinitionPath() {
348         return definitionPath;
349     }
350
351     /**
352      * @param definitionPath
353      * The definitionPath to set.
354      */

355     public void setDefinitionPath(String JavaDoc definitionPath) {
356         this.definitionPath = definitionPath;
357     }
358
359     /**
360      * @return Returns the height.
361      */

362     public int getHeight() {
363         return height;
364     }
365
366     /**
367      * @param height
368      * The height to set.
369      */

370     public void setHeight(int height) {
371         this.height = height;
372     }
373
374     /**
375      * @return Returns the instanceId.
376      */

377     public String JavaDoc getInstanceId() {
378         return instanceId;
379     }
380
381     /**
382      * @param instanceId
383      * The instanceId to set.
384      */

385     public void setInstanceId(String JavaDoc instanceId) {
386         this.instanceId = instanceId;
387     }
388
389     /**
390      * @return Returns the solution.
391      */

392     public String JavaDoc getSolution() {
393         return solution;
394     }
395
396     /**
397      * @param solution
398      * The solution to set.
399      */

400     public void setSolution(String JavaDoc solution) {
401         this.solution = solution;
402     }
403
404     /**
405      * @return Returns the title.
406      */

407     public String JavaDoc getTitle() {
408         return title;
409     }
410
411     /**
412      * @param title
413      * The title to set.
414      */

415     public void setTitle(String JavaDoc title) {
416         this.title = title;
417     }
418
419     /**
420      * @return Returns the urlTemplate.
421      */

422     public String JavaDoc getUrlTemplate() {
423         return urlTemplate;
424     }
425
426     /**
427      * @param urlTemplate
428      * The urlTemplate to set.
429      */

430     public void setUrlTemplate(String JavaDoc urlTemplate) {
431         this.urlTemplate = urlTemplate;
432     }
433
434     /**
435      * @return Returns the values.
436      */

437     public Object JavaDoc getValues() {
438         return values;
439     }
440
441     /**
442      * @param values
443      * The values to set.
444      */

445     public void setValues(Object JavaDoc values) {
446         this.values = values;
447     }
448
449     /**
450      * @return Returns the width.
451      */

452     public int getWidth() {
453         return width;
454     }
455
456     /**
457      * @param width
458      * The width to set.
459      */

460     public void setWidth(int width) {
461         this.width = width;
462     }
463
464     /**
465      * @param logger
466      * The logger to set.
467      */

468     public void setLogger(Log logger) {
469         AbstractChartComponent.logger = logger;
470     }
471
472     /**
473      * @return Returns the byRow.
474      */

475     public boolean isByRow() {
476         return byRow;
477     }
478
479     /**
480      * @param byRow
481      * The byRow to set.
482      */

483     public void setByRow(boolean byRow) {
484         this.byRow = byRow;
485     }
486
487     /**
488      * @return Returns the paramName.
489      */

490     public String JavaDoc getParamName() {
491         return paramName;
492     }
493
494     /**
495      * @param paramName
496      * The paramName to set.
497      */

498     public void setParamName(String JavaDoc paramName) {
499         this.paramName = paramName;
500     }
501
502     /**
503      * @return Returns the outerParamNames.
504      */

505     public List JavaDoc getOuterParamNames() {
506         return outerParamNames;
507     }
508
509     /**
510      * @param outerParamName
511      * The outerParamNames name to add to the outParamNames list.
512      */

513     public void addOuterParamName(String JavaDoc outerParamName) {
514         outerParamNames.add(outerParamName);
515     }
516
517 }
518
Popular Tags