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 Nov 5, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.plugin.olap;
018:
019: import java.net.URLEncoder;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.dom4j.Element;
030: import org.pentaho.messages.Messages;
031: import org.pentaho.messages.util.LocaleHelper;
032: import org.pentaho.plugin.ComponentBase;
033: import org.pentaho.plugin.core.StandardSettings;
034:
035: public class PivotViewComponent extends ComponentBase {
036:
037: public static final String MODE = "mode"; //$NON-NLS-1$
038:
039: public static final String MODEL = "model"; //$NON-NLS-1$
040:
041: public static final String OPTIONS = "options"; //$NON-NLS-1$
042:
043: public static final String CONNECTION = "connection"; //$NON-NLS-1$
044:
045: public static final String TITLE = "title"; //$NON-NLS-1$
046:
047: public static final String URL = "url"; //$NON-NLS-1$
048:
049: public static final String VIEWER = "viewer"; //$NON-NLS-1$
050:
051: public static final String EXECUTE = "execute"; //$NON-NLS-1$
052:
053: public static final String SHOWGRID = "showgrid"; //$NON-NLS-1$
054:
055: public static final String CHARTTYPE = "charttype"; //$NON-NLS-1$
056:
057: public static final String CHARTLOCATION = "chartlocation"; //$NON-NLS-1$
058:
059: public static final String CHARTWIDTH = "chartwidth"; //$NON-NLS-1$
060:
061: public static final String CHARTHEIGHT = "chartheight"; //$NON-NLS-1$
062:
063: public static final String CHARTDRILLTHROUGHENABLED = "chartdrillthroughenabled"; //$NON-NLS-1$
064:
065: public static final String CHARTTITLE = "charttitle"; //$NON-NLS-1$
066:
067: public static final String CHARTTITLEFONTFAMILY = "charttitlefontfamily"; //$NON-NLS-1$
068:
069: public static final String CHARTTITLEFONTSTYLE = "charttitlefontstyle"; //$NON-NLS-1$
070:
071: public static final String CHARTTITLEFONTSIZE = "charttitlefontsize"; //$NON-NLS-1$
072:
073: public static final String CHARTHORIZAXISLABEL = "charthorizaxislabel"; //$NON-NLS-1$
074:
075: public static final String CHARTVERTAXISLABEL = "chartvertaxislabel"; //$NON-NLS-1$
076:
077: public static final String CHARTAXISLABELFONTFAMILY = "chartaxislabelfontfamily"; //$NON-NLS-1$
078:
079: public static final String CHARTAXISLABELFONTSTYLE = "chartaxislabelfontstyle"; //$NON-NLS-1$
080:
081: public static final String CHARTAXISLABELFONTSIZE = "chartaxislabelfontsize"; //$NON-NLS-1$
082:
083: public static final String CHARTAXISTICKFONTFAMILY = "chartaxistickfontfamily"; //$NON-NLS-1$
084:
085: public static final String CHARTAXISTICKFONTSTYLE = "chartaxistickfontstyle"; //$NON-NLS-1$
086:
087: public static final String CHARTAXISTICKFONTSIZE = "chartaxistickfontsize"; //$NON-NLS-1$
088:
089: public static final String CHARTAXISTICKLABELROTATION = "chartaxisticklabelrotation"; //$NON-NLS-1$
090:
091: public static final String CHARTSHOWLEGEND = "chartshowlegend"; //$NON-NLS-1$
092:
093: public static final String CHARTLEGENDLOCATION = "chartlegendlocation"; //$NON-NLS-1$
094:
095: public static final String CHARTLEGENDFONTFAMILY = "chartlegendfontfamily"; //$NON-NLS-1$
096:
097: public static final String CHARTLEGENDFONTSTYLE = "chartlegendfontstyle"; //$NON-NLS-1$
098:
099: public static final String CHARTLEGENDFONTSIZE = "chartlegendfontsize"; //$NON-NLS-1$
100:
101: public static final String CHARTSHOWSLICER = "chartshowslicer"; //$NON-NLS-1$
102:
103: public static final String CHARTSLICERLOCATION = "chartslicerlocation"; //$NON-NLS-1$
104:
105: public static final String CHARTSLICERALIGNMENT = "chartsliceralignment"; //$NON-NLS-1$
106:
107: public static final String CHARTSLICERFONTFAMILY = "chartslicerfontfamily"; //$NON-NLS-1$
108:
109: public static final String CHARTSLICERFONTSTYLE = "chartslicerfontstyle"; //$NON-NLS-1$
110:
111: public static final String CHARTSLICERFONTSIZE = "chartslicerfontsize"; //$NON-NLS-1$
112:
113: public static final String CHARTBACKGROUNDR = "chartbackgroundr"; //$NON-NLS-1$
114:
115: public static final String CHARTBACKGROUNDG = "chartbackgroundg"; //$NON-NLS-1$
116:
117: public static final String CHARTBACKGROUNDB = "chartbackgroundb"; //$NON-NLS-1$
118:
119: public static final String ROLE = "role"; //$NON-NLS-1$
120:
121: public static final String CUBE = "cube"; //$NON-NLS-1$
122:
123: private static final long serialVersionUID = -327755990995067478L;
124:
125: private static final Collection ignoreInputs = Arrays
126: .asList(new String[] { MODE, StandardSettings.SQL_QUERY,
127: StandardSettings.QUERY_NAME, VIEWER });
128:
129: public Log getLogger() {
130: return LogFactory.getLog(PivotViewComponent.class);
131: }
132:
133: protected boolean validateAction() {
134:
135: if (!isDefinedOutput(OPTIONS)) {
136: error(Messages
137: .getErrorString("PivotView.ERROR_0001_OPTIONS_NOT_DEFINED")); //$NON-NLS-1$
138: return false;
139: }
140: if (!isDefinedOutput(MODEL)) {
141: error(Messages
142: .getErrorString("PivotView.ERROR_0002_MODEL_NOT_DEFIEND")); //$NON-NLS-1$
143: return false;
144: }
145: if (!isDefinedOutput(CONNECTION)) {
146: error(Messages
147: .getErrorString("PivotView.ERROR_0003_CONNECTION_NOT_DEFINED")); //$NON-NLS-1$
148: return false;
149: }
150: if (!isDefinedOutput(StandardSettings.MDX_QUERY)) {
151: error(Messages
152: .getErrorString("PivotView.ERROR_0004_MDX_NOT_DEFINED")); //$NON-NLS-1$
153: return false;
154: }
155: if (!isDefinedOutput(TITLE)) {
156: error(Messages
157: .getErrorString("PivotView.ERROR_0007_TITLE_NOT_DEFINED")); //$NON-NLS-1$
158: return false;
159: }
160: if (!isDefinedInput(MODE)) {
161: error(Messages
162: .getErrorString("PivotView.ERROR_0005_MODE_NOT_DEFINED")); //$NON-NLS-1$
163: return false;
164: }
165: if (!isDefinedOutput(URL)) {
166: error(Messages
167: .getErrorString("PivotView.ERROR_0008_URL_NOT_DEFINED")); //$NON-NLS-1$
168: return false;
169: }
170: if (!isDefinedInput(StandardSettings.SQL_QUERY)
171: && !isDefinedInput(StandardSettings.QUERY_NAME)) {
172: error(Messages
173: .getErrorString("PivotView.ERROR_0009_QUERY_NOT_DEFINED")); //$NON-NLS-1$
174: return false;
175: }
176:
177: return true;
178: }
179:
180: protected boolean validateSystemSettings() {
181: return true;
182: }
183:
184: public void done() {
185: }
186:
187: protected boolean executeAction() throws Throwable {
188:
189: Set inputNames = getInputNames();
190: Set outputNames = getOutputNames();
191:
192: String mode = getInputStringValue(MODE);
193: if (!mode.equals(EXECUTE)) {
194: // assume this is a redirect
195: if (!isDefinedOutput(URL)) {
196: // we need the viewer output
197: error(Messages
198: .getString("PivotView.ERROR_0006_VIEWER_NOT_DEFINED")); //$NON-NLS-1$
199: return false;
200: }
201: String viewer = getInputStringValue(VIEWER);
202: if (viewer.indexOf('?') == -1) {
203: viewer += "?solution=" + getSolutionName() + "&path=" + getSolutionPath() + "&action=" + getActionName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
204: } else {
205: viewer += "solution=" + getSolutionName() + "&path=" + getSolutionPath() + "&action=" + getActionName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
206: }
207:
208: for (Iterator it = inputNames.iterator(); it.hasNext();) {
209: String name = (String) it.next();
210: if (!ignoreInputs.contains(name)) {
211: viewer += "&" + name + "=" + URLEncoder.encode(getInputStringValue(name), LocaleHelper.getSystemEncoding()); //$NON-NLS-1$ //$NON-NLS-2$
212: }
213: }
214:
215: setOutputValue(URL, viewer);
216: return true;
217: }
218:
219: String roleName = null;
220: if (isDefinedInput(ROLE)) {
221: roleName = getInputStringValue(ROLE);
222: if (isDefinedOutput(ROLE)) {
223: setOutputValue(ROLE, roleName);
224: }
225: }
226:
227: // process the model
228: String model = getInputStringValue(StandardSettings.DATA_MODEL);
229: // if (model.indexOf("http") != 0) { //$NON-NLS-1$
230: // model = PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + model; //$NON-NLS-1$
231: // }
232: //
233: // Allow fully-qualified model reference to pass un-molested
234: //
235: if (!model.startsWith("solution:") && !model.startsWith("http:")) { //$NON-NLS-1$ //$NON-NLS-2$
236: model = "solution:" + model; //$NON-NLS-1$
237: }
238:
239: setOutputValue(StandardSettings.DATA_MODEL, model);
240:
241: if (isDefinedOutput(CHARTTYPE)) {
242: if (isDefinedInput(CHARTTYPE)) {
243: setOutputValue(CHARTTYPE,
244: getInputStringValue(CHARTTYPE));
245: }
246: }
247:
248: if (isDefinedOutput(SHOWGRID)) {
249: if (isDefinedInput(SHOWGRID)) {
250: setOutputValue(SHOWGRID, getInputStringValue(SHOWGRID));
251: }
252: }
253:
254: if (isDefinedOutput(CHARTLOCATION)) {
255: if (isDefinedInput(CHARTLOCATION)) {
256: setOutputValue(CHARTLOCATION,
257: getInputStringValue(CHARTLOCATION));
258: }
259: }
260:
261: if (isDefinedOutput(CHARTWIDTH)) {
262: if (isDefinedInput(CHARTWIDTH)) {
263: setOutputValue(CHARTWIDTH,
264: getInputStringValue(CHARTWIDTH));
265: }
266: }
267:
268: if (isDefinedOutput(CHARTHEIGHT)) {
269: if (isDefinedInput(CHARTHEIGHT)) {
270: setOutputValue(CHARTHEIGHT,
271: getInputStringValue(CHARTHEIGHT));
272: }
273: }
274:
275: if (isDefinedOutput(CHARTDRILLTHROUGHENABLED)) {
276: if (isDefinedInput(CHARTDRILLTHROUGHENABLED)) {
277: setOutputValue(CHARTDRILLTHROUGHENABLED,
278: getInputStringValue(CHARTDRILLTHROUGHENABLED));
279: }
280: }
281:
282: if (isDefinedOutput(CHARTTITLE)) {
283: if (isDefinedInput(CHARTTITLE)) {
284: setOutputValue(CHARTTITLE,
285: getInputStringValue(CHARTTITLE));
286: }
287: }
288:
289: if (isDefinedOutput(CHARTTITLEFONTFAMILY)) {
290: if (isDefinedInput(CHARTTITLEFONTFAMILY)) {
291: setOutputValue(CHARTTITLEFONTFAMILY,
292: getInputStringValue(CHARTTITLEFONTFAMILY));
293: }
294: }
295:
296: if (isDefinedOutput(CHARTTITLEFONTSTYLE)) {
297: if (isDefinedInput(CHARTTITLEFONTSTYLE)) {
298: setOutputValue(CHARTTITLEFONTSTYLE,
299: getInputStringValue(CHARTTITLEFONTSTYLE));
300: }
301: }
302:
303: if (isDefinedOutput(CHARTTITLEFONTSIZE)) {
304: if (isDefinedInput(CHARTTITLEFONTSIZE)) {
305: setOutputValue(CHARTTITLEFONTSIZE,
306: getInputStringValue(CHARTTITLEFONTSIZE));
307: }
308: }
309:
310: if (isDefinedOutput(CHARTHORIZAXISLABEL)) {
311: if (isDefinedInput(CHARTHORIZAXISLABEL)) {
312: setOutputValue(CHARTHORIZAXISLABEL,
313: getInputStringValue(CHARTHORIZAXISLABEL));
314: }
315: }
316:
317: if (isDefinedOutput(CHARTVERTAXISLABEL)) {
318: if (isDefinedInput(CHARTVERTAXISLABEL)) {
319: setOutputValue(CHARTVERTAXISLABEL,
320: getInputStringValue(CHARTVERTAXISLABEL));
321: }
322: }
323:
324: if (isDefinedOutput(CHARTAXISLABELFONTFAMILY)) {
325: if (isDefinedInput(CHARTAXISLABELFONTFAMILY)) {
326: setOutputValue(CHARTAXISLABELFONTFAMILY,
327: getInputStringValue(CHARTAXISLABELFONTFAMILY));
328: }
329: }
330:
331: if (isDefinedOutput(CHARTAXISLABELFONTSTYLE)) {
332: if (isDefinedInput(CHARTAXISLABELFONTSTYLE)) {
333: setOutputValue(CHARTAXISLABELFONTSTYLE,
334: getInputStringValue(CHARTAXISLABELFONTSTYLE));
335: }
336: }
337:
338: if (isDefinedOutput(CHARTAXISLABELFONTSIZE)) {
339: if (isDefinedInput(CHARTAXISLABELFONTSIZE)) {
340: setOutputValue(CHARTAXISLABELFONTSIZE,
341: getInputStringValue(CHARTAXISLABELFONTSIZE));
342: }
343: }
344:
345: if (isDefinedOutput(CHARTAXISTICKFONTFAMILY)) {
346: if (isDefinedInput(CHARTAXISTICKFONTFAMILY)) {
347: setOutputValue(CHARTAXISTICKFONTFAMILY,
348: getInputStringValue(CHARTAXISTICKFONTFAMILY));
349: }
350: }
351:
352: if (isDefinedOutput(CHARTAXISTICKFONTSTYLE)) {
353: if (isDefinedInput(CHARTAXISTICKFONTSTYLE)) {
354: setOutputValue(CHARTAXISTICKFONTSTYLE,
355: getInputStringValue(CHARTAXISTICKFONTSTYLE));
356: }
357: }
358:
359: if (isDefinedOutput(CHARTAXISTICKFONTSIZE)) {
360: if (isDefinedInput(CHARTAXISTICKFONTSIZE)) {
361: setOutputValue(CHARTAXISTICKFONTSIZE,
362: getInputStringValue(CHARTAXISTICKFONTSIZE));
363: }
364: }
365:
366: if (isDefinedOutput(CHARTAXISTICKLABELROTATION)) {
367: if (isDefinedInput(CHARTAXISTICKLABELROTATION)) {
368: setOutputValue(CHARTAXISTICKLABELROTATION,
369: getInputStringValue(CHARTAXISTICKLABELROTATION));
370: }
371: }
372:
373: if (isDefinedOutput(CHARTSHOWLEGEND)) {
374: if (isDefinedInput(CHARTSHOWLEGEND)) {
375: setOutputValue(CHARTSHOWLEGEND,
376: getInputStringValue(CHARTSHOWLEGEND));
377: }
378: }
379:
380: if (isDefinedOutput(CHARTLEGENDLOCATION)) {
381: if (isDefinedInput(CHARTLEGENDLOCATION)) {
382: setOutputValue(CHARTLEGENDLOCATION,
383: getInputStringValue(CHARTLEGENDLOCATION));
384: }
385: }
386:
387: if (isDefinedOutput(CHARTLEGENDFONTFAMILY)) {
388: if (isDefinedInput(CHARTLEGENDFONTFAMILY)) {
389: setOutputValue(CHARTLEGENDFONTFAMILY,
390: getInputStringValue(CHARTLEGENDFONTFAMILY));
391: }
392: }
393:
394: if (isDefinedOutput(CHARTLEGENDFONTSTYLE)) {
395: if (isDefinedInput(CHARTLEGENDFONTSTYLE)) {
396: setOutputValue(CHARTLEGENDFONTSTYLE,
397: getInputStringValue(CHARTLEGENDFONTSTYLE));
398: }
399: }
400:
401: if (isDefinedOutput(CHARTLEGENDFONTSIZE)) {
402: if (isDefinedInput(CHARTLEGENDFONTSIZE)) {
403: setOutputValue(CHARTLEGENDFONTSIZE,
404: getInputStringValue(CHARTLEGENDFONTSIZE));
405: }
406: }
407:
408: if (isDefinedOutput(CHARTSHOWSLICER)) {
409: if (isDefinedInput(CHARTSHOWSLICER)) {
410: setOutputValue(CHARTSHOWSLICER,
411: getInputStringValue(CHARTSHOWSLICER));
412: }
413: }
414:
415: if (isDefinedOutput(CHARTSLICERLOCATION)) {
416: if (isDefinedInput(CHARTSLICERLOCATION)) {
417: setOutputValue(CHARTSLICERLOCATION,
418: getInputStringValue(CHARTSLICERLOCATION));
419: }
420: }
421:
422: if (isDefinedOutput(CHARTSLICERALIGNMENT)) {
423: if (isDefinedInput(CHARTSLICERALIGNMENT)) {
424: setOutputValue(CHARTSLICERALIGNMENT,
425: getInputStringValue(CHARTSLICERALIGNMENT));
426: }
427: }
428:
429: if (isDefinedOutput(CHARTSLICERFONTFAMILY)) {
430: if (isDefinedInput(CHARTSLICERFONTFAMILY)) {
431: setOutputValue(CHARTSLICERFONTFAMILY,
432: getInputStringValue(CHARTSLICERFONTFAMILY));
433: }
434: }
435:
436: if (isDefinedOutput(CHARTSLICERFONTSTYLE)) {
437: if (isDefinedInput(CHARTSLICERFONTSTYLE)) {
438: setOutputValue(CHARTSLICERFONTSTYLE,
439: getInputStringValue(CHARTSLICERFONTSTYLE));
440: }
441: }
442:
443: if (isDefinedOutput(CHARTSLICERFONTSIZE)) {
444: if (isDefinedInput(CHARTSLICERFONTSIZE)) {
445: setOutputValue(CHARTSLICERFONTSIZE,
446: getInputStringValue(CHARTSLICERFONTSIZE));
447: }
448: }
449:
450: if (isDefinedOutput(CHARTBACKGROUNDR)) {
451: if (isDefinedInput(CHARTBACKGROUNDR)) {
452: setOutputValue(CHARTBACKGROUNDR,
453: getInputStringValue(CHARTBACKGROUNDR));
454: }
455: }
456:
457: if (isDefinedOutput(CHARTBACKGROUNDG)) {
458: if (isDefinedInput(CHARTBACKGROUNDG)) {
459: setOutputValue(CHARTBACKGROUNDG,
460: getInputStringValue(CHARTBACKGROUNDG));
461: }
462: }
463:
464: if (isDefinedOutput(CHARTBACKGROUNDB)) {
465: if (isDefinedInput(CHARTBACKGROUNDB)) {
466: setOutputValue(CHARTBACKGROUNDB,
467: getInputStringValue(CHARTBACKGROUNDB));
468: }
469: }
470:
471: // process the data source connection
472: String dataSource = getInputStringValue(StandardSettings.JNDI);
473: setOutputValue(StandardSettings.CONNECTION,
474: "jdbc/" + dataSource); //$NON-NLS-1$
475:
476: // process the query
477: String queryName = StandardSettings.SQL_QUERY;
478: if (inputNames.contains(StandardSettings.QUERY_NAME)) {
479: queryName = getInputStringValue(StandardSettings.QUERY_NAME);
480: }
481: String query = getInputStringValue(queryName);
482:
483: // if query = "default", generate a query
484: if (query.equals(StandardSettings.DEFAULT)) {
485: // get the default cube. This is only useful if the schema contains more
486: String cube = getInputStringValue(CUBE);
487: // we need to generate a query.
488: query = MondrianModelComponent.getInitialQuery(model,
489: dataSource, cube, roleName);
490: }
491:
492: String mdx = applyInputsToFormat(query);
493: setOutputValue(StandardSettings.MDX_QUERY, mdx);
494:
495: String title = getInputStringValue(TITLE);
496: setOutputValue(TITLE, title);
497:
498: // now process the options
499: ArrayList options = new ArrayList();
500: Element optionsNode = (Element) getComponentDefinition()
501: .selectSingleNode("options"); //$NON-NLS-1$
502: List optionNodes = optionsNode.elements();
503: Iterator optionsIterator = optionNodes.iterator();
504: while (optionsIterator.hasNext()) {
505: Element optionNode = (Element) optionsIterator.next();
506: options.add(optionNode.getName());
507: }
508: if (options.size() > 0) {
509: if (outputNames.contains(OPTIONS)) {
510: setOutputValue(OPTIONS, options);
511: } else {
512: error(Messages
513: .getErrorString("PivotView.ERROR_0001_OPTIONS_NOT_DEFINED")); //$NON-NLS-1$
514: return false;
515: }
516: }
517: return true;
518: }
519:
520: public boolean init() {
521: return true;
522: }
523:
524: }
|