KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > data > CincomMDXFieldsProvider


1 /*
2  *
3  * Copyright (C) 2005, 2006 CINCOM SYSTEMS, INC.
4  * All Rights Reserved
5  * www.cincom.com
6  *
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed WITHOUT ANY WARRANTY; and without the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
19  * or write to:
20  *
21  * Free Software Foundation, Inc.,
22  * 59 Temple Place - Suite 330,
23  * Boston, MA USA 02111-1307
24  *
25  *
26  * CincomMDXFieldsProvider.java
27  *
28  * Created on December 18, 2006, 2:28 PM
29  *
30  * To change this template, choose Tools | Template Manager
31  * and open the template in the editor.
32  */

33
34 package it.businesslogic.ireport.data;
35
36 import it.businesslogic.ireport.FieldsProviderEditor;
37 import it.businesslogic.ireport.IReportConnection;
38 import it.businesslogic.ireport.connection.JRXMLADataSourceConnection;
39 import it.businesslogic.ireport.gui.ReportQueryDialog;
40 import java.util.Map JavaDoc;
41 import net.sf.jasperreports.engine.JRDataset;
42 import net.sf.jasperreports.engine.JRException;
43
44 import net.sf.jasperreports.engine.JRField;
45 import net.sf.jasperreports.engine.design.JRDesignField;
46
47 /**
48  *
49  * @author gtoffoli
50  */

51 public class CincomMDXFieldsProvider extends MDXFieldsProvider {
52     
53     private final String JavaDoc axis0 = "Axis0";
54     private final String JavaDoc axis1 = "Axis1";
55     private final String JavaDoc slicer = "SlicerAxis";
56     private final char leftPar='[';
57     private final char rightPar=']';
58     public static java.util.ArrayList JavaDoc dimensionsList = new java.util.ArrayList JavaDoc();
59     private boolean foundMeasure; // to handle queries with no explicit measure in them
60

61     /** Creates a new instance of CincomMDXFieldsProvider */
62     public CincomMDXFieldsProvider() {
63         super();
64     }
65     
66     public boolean supportsGetFieldsOperation() {
67         return true;
68     }
69     
70     public boolean supportsAutomaticQueryExecution() {
71         return true;
72     }
73     
74     public boolean hasEditorComponent() {
75         return false;
76     }
77     
78     public FieldsProviderEditor getEditorComponent(ReportQueryDialog reportQueryDialog) {
79         
80         return null;
81     }
82     
83     public JRField[] getFields(IReportConnection con, JRDataset reportDataset, Map JavaDoc parameters) throws JRException, UnsupportedOperationException JavaDoc {
84         /**
85              * Copyright (C) 2006 CINCOM SYSTEMS, INC.
86              * All Rights Reserved
87              * www.cincom.com
88              */

89             if (con instanceof it.businesslogic.ireport.connection.JRXMLADataSourceConnection)
90             {
91                 try {
92                     String JavaDoc query = reportDataset.getQuery().getText();
93                    
94                      java.util.ArrayList JavaDoc v_fields = getFields(query, (JRXMLADataSourceConnection)con);
95                    
96                      // Translate
97
JRField[] final_fields = new JRField[v_fields.size()];
98                     for (int i=0; i<v_fields.size(); ++i)
99                     {
100                         XmlaFieldNode f = (XmlaFieldNode)v_fields.get(i);
101                         JRDesignField field = new JRDesignField();
102                         field.setName( f.getName() );
103                         field.setValueClassName( f.getClassType() );
104                         field.setDescription( f.getDescription() );
105                         final_fields[i] = field;
106                     }
107
108                     return final_fields;
109                 
110                      
111                 } catch (Exception JavaDoc ex)
112                 {
113                     throw new JRException(ex);
114                 }
115             }
116             else
117             {
118                      throw new JRException("The active connection is not of type XMLA. Activate an XMLA connection first.");
119             }
120     }
121     
122     
123     /**
124      * Returns the Fields discovered for the MDX Query
125      * @param : Query String
126      * @param : status is added to return the appropriate field values
127      * Reason : to handle the Wizard Dialog and ReportQuery Dialog issue
128      * ReportQueryDialog looks for a jTable / ArrayList
129      * WizardDialog looks for a jList / List
130      * status == true implies that the call is from WizardDialog
131      * false imples that the call is from ReportQueryDialog.
132      *
133      */

134     public java.util.ArrayList JavaDoc getFields(String JavaDoc query, JRXMLADataSourceConnection con) throws java.lang.Exception JavaDoc
135     {
136         foundMeasure = false;
137         it.businesslogic.ireport.data.XmlaFieldNode fld2;
138         java.util.ArrayList JavaDoc fields = new java.util.ArrayList JavaDoc();
139         rex.metadata.ServerMetadata smd =
140                 new rex.metadata.ServerMetadata(con.getUrl(),null);
141         if (smd.isValidUrl() == false) {
142             return null;
143         }
144         rex.xmla.RexXMLAExecuteProperties rexProperties = new rex.xmla.RexXMLAExecuteProperties();
145         rexProperties.setDataSourceInfo(con.getDatasource());
146         rexProperties.setCatalog(con.getCatalog());
147         this.dimensionsList.clear();
148         try {
149             rex.metadata.ExecuteResult eResult =
150                     new rex.metadata.ExecuteResult(smd.execute(query,rexProperties),null);
151             if (!addMDXAxisColumns(fields, eResult, axis0)){
152                 return null;
153             }
154             if (!addMDXAxisColumns(fields, eResult, axis1)){
155                 return null;
156             }
157             if (!addMDXAxisColumns(fields, eResult, slicer)){
158                 return null;
159             }
160             //if no measure was explicitly parsed, then add one to pick up the default (ALL) measure:
161
if (!foundMeasure) {
162                 fld2 = new it.businesslogic.ireport.data.XmlaFieldNode(
163                         "DefaultMeasure", 2);
164                 fld2.setDescription("DefaultMeasure");
165           
166                 fields.add(fld2);
167             }
168             eResult = null;
169             rexProperties = null;
170             smd = null;
171         }
172
173        catch(Exception JavaDoc e){
174            throw e;
175        }
176         return fields;
177     }
178     
179     /**
180      * Adds all the Dimensions and measures found on Rows, Columns and Slicer axis
181      *
182      * @param fields : All the dimension / measures discovered are added and returned
183      * @param : status is added to return the appropriate field values
184      * Reason : to handle the Wizard Dialog and ReportQuery Dialog issue
185      * ReportQueryDialog looks for a jTable / ArrayList
186      * WizardDialog looks for a jList / List
187      * status == true implies that the call is from WizardDialog
188      * false imples that the call is from ReportQueryDialog.
189      *
190      */

191     private boolean addMDXAxisColumns(java.util.ArrayList JavaDoc fields,
192             rex.metadata.ExecuteResult eResult,
193             String JavaDoc axisName)
194     {
195         java.util.HashSet JavaDoc fSet = new java.util.HashSet JavaDoc();
196         
197         
198         int axisNo=-1;
199         if (eResult == null || fields == null || axisName == null ){
200             return false;
201         }
202         if ((axisName.compareTo(axis0) != 0) &&
203                 (axisName.compareTo(axis1) !=0) &&
204                 (axisName.compareTo(slicer) !=0)){
205             return false;
206         }
207         rex.metadata.resultelements.Axis axis =
208                 eResult.getAxis(axisName);
209         if (axis == null){
210             return false;
211         }
212         rex.metadata.resultelements.HierarchyInfo hierInfo;
213         rex.metadata.resultelements.Tuple tuple;
214         
215         
216         if (axisName.compareTo(axis0) == 0) {
217             axisNo = 0;
218         } else {
219             axisNo = 1;
220         }
221         tuple = null;
222         
223         it.businesslogic.ireport.data.XmlaFieldNode fld;
224         String JavaDoc longName= "";
225         String JavaDoc shortName = "";
226        
227         int hierarchyCount = axis.getHierarchyInfoCount();
228         for (int hierIndex=0;hierIndex<hierarchyCount;hierIndex++) {
229             hierInfo = axis.getHierarchyInfoAt(hierIndex);
230             if (hierInfo == null){
231                 return false;
232             }
233            
234             int tupleCount = axis.getTupleCount(); // ham 9/11/06
235
for (int tupleIndex=0; tupleIndex< tupleCount; tupleIndex++){
236                 tuple = axis.getTupleAt(tupleIndex);
237                 // following loop for multiple dimensions in one (slicer) tuple
238
// references within this loop to getMemberAt(0) were changed to getMemberAt(tupleMemberIndex)
239
int tupleMemberCount = tuple.getMemberCount();
240                 for (int tupleMemberIndex=0;tupleMemberIndex<tupleMemberCount;tupleMemberIndex++) {
241                     if (tuple.getMemberAt(tupleMemberIndex).isMeasure()) {
242                         foundMeasure = true;
243                         
244                         longName= tuple.getMemberAt(tupleMemberIndex).getUniqueName();
245                        
246                         shortName= longName.substring(longName.lastIndexOf(leftPar)+1,
247                                 longName.lastIndexOf(rightPar));
248                         
249                         
250                         // following IF and adding it to FSET added for duplicate fields in query problem
251

252                         
253                         if (!fSet.contains(shortName)) {
254                             
255                             
256                             fld = new it.businesslogic.ireport.data.XmlaFieldNode(
257                                     shortName, axisNo);
258                             fld.setDescription(longName);
259                             fSet.add(shortName);
260                             
261                             // Checking the status and add the appropirte values to fields
262
//if (status.booleanValue()){ //this incase of WizardDialog
263
fields.add(fld);
264                             //} else{ //case of ReportQueryDialog
265
// fields.add(new Object[]{fld,fld.getClassType(),fld.getDescription()});
266
//}
267
}
268                     } else{
269                         
270                         longName= tuple.getMemberAt(tupleMemberIndex).getLname();
271                       
272                         shortName= longName.substring(longName.lastIndexOf(leftPar)+1,
273                                 longName.lastIndexOf(rightPar));
274                            if (!fSet.contains(shortName)) {
275                             fld = new it.businesslogic.ireport.data.XmlaFieldNode(
276                                     shortName,axisNo);
277                             fld.setDescription(longName);
278                             fSet.add(shortName);
279                             
280                             // Checking the status and add the appropirte values to fields
281

282                             //if (status.booleanValue()){ //this incase of WizardDialog
283
fields.add(fld);
284                               
285                                 if(!this.dimensionsList.contains(fld)){
286                                     this.dimensionsList.add(fld);
287                                 }
288                                 
289                             //} else{ //case of ReportQueryDialog
290
// fields.add(new Object[]{fld,fld.getClassType(),fld.getDescription()});
291

292                             // if(!this.dimensionsList.contains(fld)){
293
// this.dimensionsList.add(new Object[]{fld,fld.getClassType(),fld.getDescription()});
294
// }
295
//}
296

297                         }
298                     }
299                 }
300             }
301             
302         }
303         return true;
304     }
305     /**
306      * Helper method to return the dimensions discoverd in the MDX Query
307      */

308     public static java.util.ArrayList JavaDoc getDimensions(){
309         return dimensionsList;
310     }
311 }
312
Popular Tags