KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > olap > PivotViewComponent


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 5, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.olap;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.dom4j.Element;
28 import org.pentaho.core.system.PentahoSystem;
29 import org.pentaho.messages.Messages;
30 import org.pentaho.plugin.ComponentBase;
31 import org.pentaho.plugin.core.StandardSettings;
32
33 public class PivotViewComponent extends ComponentBase {
34
35     private static final String JavaDoc MODE = "mode"; //$NON-NLS-1$
36

37     private static final String JavaDoc MODEL = "model"; //$NON-NLS-1$
38

39     private static final String JavaDoc OPTIONS = "options"; //$NON-NLS-1$
40

41     private static final String JavaDoc CONNECTION = "connection"; //$NON-NLS-1$
42

43     private static final String JavaDoc TITLE = "title"; //$NON-NLS-1$
44

45     private static final String JavaDoc URL = "url"; //$NON-NLS-1$
46

47     private static final String JavaDoc VIEWER = "viewer"; //$NON-NLS-1$
48

49     private static final String JavaDoc EXECUTE = "execute"; //$NON-NLS-1$
50

51     private static final String JavaDoc CHARTTYPE = "charttype"; //$NON-NLS-1$
52

53     private static final String JavaDoc CHARTLOCATION = "chartlocation"; //$NON-NLS-1$
54

55     private static final String JavaDoc ROLE = "role"; //$NON-NLS-1$
56

57     private static final long serialVersionUID = -327755990995067478L;
58     
59     private static final Collection JavaDoc ignoreInputs = Arrays.asList(
60       new String JavaDoc[] { MODE, StandardSettings.SQL_QUERY, StandardSettings.QUERY_NAME, VIEWER }
61     );
62
63     public Log getLogger() {
64         return LogFactory.getLog(PivotViewComponent.class);
65     }
66
67     protected boolean validateAction() {
68
69         if (!isDefinedOutput(OPTIONS)) {
70             error(Messages.getErrorString("PivotView.ERROR_0001_OPTIONS_NOT_DEFINED")); //$NON-NLS-1$
71
return false;
72         }
73         if (!isDefinedOutput(MODEL)) {
74             error(Messages.getErrorString("PivotView.ERROR_0002_MODEL_NOT_DEFIEND")); //$NON-NLS-1$
75
return false;
76         }
77         if (!isDefinedOutput(CONNECTION)) {
78             error(Messages.getErrorString("PivotView.ERROR_0003_CONNECTION_NOT_DEFINED")); //$NON-NLS-1$
79
return false;
80         }
81         if (!isDefinedOutput(StandardSettings.MDX_QUERY)) {
82             error(Messages.getErrorString("PivotView.ERROR_0004_MDX_NOT_DEFINED")); //$NON-NLS-1$
83
return false;
84         }
85         if (!isDefinedOutput(TITLE)) {
86             error(Messages.getErrorString("PivotView.ERROR_0007_TITLE_NOT_DEFINED")); //$NON-NLS-1$
87
return false;
88         }
89         if (!isDefinedInput(MODE)) {
90             error(Messages.getErrorString("PivotView.ERROR_0005_MODE_NOT_DEFINED")); //$NON-NLS-1$
91
return false;
92         }
93         if (!isDefinedOutput(URL)) {
94             error(Messages.getErrorString("PivotView.ERROR_0008_URL_NOT_DEFINED")); //$NON-NLS-1$
95
return false;
96         }
97         if (!isDefinedInput(StandardSettings.SQL_QUERY) && !isDefinedInput(StandardSettings.QUERY_NAME)) {
98             error(Messages.getErrorString("PivotView.ERROR_0009_QUERY_NOT_DEFINED")); //$NON-NLS-1$
99
return false;
100         }
101
102         return true;
103     }
104
105     protected boolean validateSystemSettings() {
106         return true;
107     }
108
109     public void done() {
110     }
111
112     protected boolean executeAction() throws Throwable JavaDoc {
113
114         Set JavaDoc inputNames = getInputNames();
115         Set JavaDoc outputNames = getOutputNames();
116
117         String JavaDoc mode = getInputStringValue(MODE);
118         if (!mode.equals(EXECUTE)) {
119             // assume this is a redirect
120
if (!isDefinedOutput(URL)) {
121                 // we need the viewer output
122
error(Messages.getString("PivotView.ERROR_0006_VIEWER_NOT_DEFINED")); //$NON-NLS-1$
123
return false;
124             }
125             String JavaDoc viewer = getInputStringValue(VIEWER);
126             if (viewer.indexOf('?') == -1) {
127                 viewer += "?solution=" + getSolutionName() + "&path=" + getSolutionPath() + "&action=" + getActionName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
128
} else {
129                 viewer += "solution=" + getSolutionName() + "&path=" + getSolutionPath() + "&action=" + getActionName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
130
}
131             
132             for ( Iterator JavaDoc it = inputNames.iterator(); it.hasNext(); ) {
133                 String JavaDoc name = (String JavaDoc)it.next();
134                 if ( !ignoreInputs.contains( name ) ) {
135                     viewer += "&" + name + "=" + getInputStringValue( name ); //$NON-NLS-1$ //$NON-NLS-2$
136
}
137             }
138
139             setOutputValue(URL, viewer);
140             return true;
141         }
142
143         String JavaDoc roleName = null;
144         if (isDefinedInput(ROLE)) {
145           roleName = getInputStringValue(ROLE);
146           if (isDefinedOutput(ROLE)) {
147             setOutputValue(ROLE, roleName);
148           }
149         }
150         
151         // process the model
152
String JavaDoc model = getInputStringValue(StandardSettings.DATA_MODEL);
153         if (model.indexOf("http") != 0) { //$NON-NLS-1$
154
model = PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + model; //$NON-NLS-1$
155
}
156         setOutputValue(StandardSettings.DATA_MODEL, model);
157
158         if (isDefinedOutput(CHARTTYPE)) {
159             if (isDefinedInput(CHARTTYPE)) {
160                 setOutputValue(CHARTTYPE, getInputStringValue(CHARTTYPE));
161             }
162         }
163
164         if (isDefinedOutput(CHARTLOCATION)) {
165             if (isDefinedInput(CHARTLOCATION)) {
166                 setOutputValue(CHARTLOCATION, getInputStringValue(CHARTLOCATION));
167             }
168         }
169
170         // process the data source connection
171
String JavaDoc dataSource = getInputStringValue(StandardSettings.JNDI);
172         setOutputValue(StandardSettings.CONNECTION, "jdbc/" + dataSource); //$NON-NLS-1$
173

174         // process the query
175
String JavaDoc queryName = StandardSettings.SQL_QUERY;
176         if (inputNames.contains(StandardSettings.QUERY_NAME)) {
177             queryName = getInputStringValue(StandardSettings.QUERY_NAME);
178         }
179         String JavaDoc query = getInputStringValue(queryName);
180
181         if (query == null) {
182
183         }
184         if (query.equals(StandardSettings.DEFAULT)) {
185             // we need to generate a query.
186
query = MondrianModelComponent.getInitialQuery(model, dataSource, null, roleName);
187         }
188
189         String JavaDoc mdx = applyInputsToFormat(query);
190         setOutputValue(StandardSettings.MDX_QUERY, mdx);
191
192         String JavaDoc title = getInputStringValue(TITLE);
193         setOutputValue(TITLE, title);
194
195         // now process the options
196
ArrayList JavaDoc options = new ArrayList JavaDoc();
197         Element optionsNode = (Element) getComponentDefinition().selectSingleNode("options"); //$NON-NLS-1$
198
List JavaDoc optionNodes = optionsNode.elements();
199         Iterator JavaDoc optionsIterator = optionNodes.iterator();
200         while (optionsIterator.hasNext()) {
201             Element optionNode = (Element) optionsIterator.next();
202             options.add(optionNode.getName());
203         }
204         if (options.size() > 0) {
205             if (outputNames.contains(OPTIONS)) {
206                 setOutputValue(OPTIONS, options);
207             } else {
208                 error(Messages.getErrorString("PivotView.ERROR_0001_OPTIONS_NOT_DEFINED")); //$NON-NLS-1$
209
return false;
210             }
211         }
212         return true;
213     }
214
215     public boolean init() {
216         return true;
217     }
218
219 }
220
Popular Tags