KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > XMLA_DrillThroughTableModel


1 /*
2  * Created on 27/01/2005
3  */

4 package com.tonbeller.jpivot.xmla;
5
6 /**
7  * @author stflourd
8  */

9
10
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import com.tonbeller.wcf.table.AbstractTableModel;
21 import com.tonbeller.wcf.table.DefaultTableRow;
22 import com.tonbeller.wcf.table.TableRow;
23 import com.tonbeller.jpivot.olap.model.Axis;
24 import com.tonbeller.jpivot.olap.model.*;
25 /**
26  * A wcf table model for drill through data,
27  * requires an sql query and connection information to be set.
28  */

29
30 public class XMLA_DrillThroughTableModel extends AbstractTableModel {
31     private static Logger logger = Logger.getLogger(XMLA_DrillThroughTableModel.class);
32     private String JavaDoc title = "Drill Through Table";
33     private String JavaDoc caption = "";
34     private String JavaDoc dataSourceName;
35     private int cellOrdinal;
36     /**
37      * @return Returns the cellOrdinal.
38      */

39     public int getCellOrdinal() {
40         return cellOrdinal;
41     }
42     /**
43      * @param cellOrdinal The cellOrdinal to set.
44      */

45     public void setCellOrdinal(int cellOrdinal) {
46         this.cellOrdinal = cellOrdinal;
47     }
48     XMLA_Model model;
49     XMLA_Model drillModel;
50     XMLA_Result drillResult;
51     /**
52      * @return Returns the model.
53      */

54     public XMLA_Model getModel() {
55         return model;
56     }
57     /**
58      * @param model The model to set.
59      */

60     public void setModel(XMLA_Model model) {
61         this.model = model;
62     }
63 // private static Context jndiContext;
64

65     private boolean ready = false;
66     
67     private TableRow[] rows = new TableRow[0];
68     private String JavaDoc [] columnTitles = new String JavaDoc[0];
69
70     public XMLA_DrillThroughTableModel() {
71     }
72
73     public int getRowCount() {
74         if ( !ready ) {
75             executeQuery();
76         }
77         return rows.length;
78     }
79
80     public TableRow getRow(int rowIndex) {
81         if ( !ready ) {
82             executeQuery();
83         }
84         return rows[rowIndex];
85     }
86
87     public String JavaDoc getTitle() {
88         return title;
89     }
90
91     /**
92      * @param title
93      */

94     public void setTitle(String JavaDoc title) {
95         this.title = title;
96     }
97
98     /**
99      * wcf table component calls this method from it's constructor
100      * to get the number of columns
101      *
102      */

103     public int getColumnCount() {
104         if ( !ready ) {
105             executeQuery();
106         }
107         return columnTitles.length;
108     }
109
110     public String JavaDoc getColumnTitle(int columnIndex) {
111         if ( !ready ) {
112             executeQuery();
113         }
114         return columnTitles[columnIndex];
115     }
116
117     // create a drillthrough MDX to query the cube with
118
private String JavaDoc buildDrillThroughMdx(XMLA_Result res) {
119         Axis[] axes = res.getAxes();
120         StringBuffer JavaDoc mdxBuf = new StringBuffer JavaDoc(" Drillthrough Select ");
121         int nXPositions = axes[0].getPositions().size();
122         int posIdx = 0;
123         for (int i = 0; i < axes.length; i++) {
124
125             // axis 0 ==> x-Coordinate
126
// axis 1 ==> y-Coordinate
127
if (i==0) {
128                 posIdx = cellOrdinal - ((cellOrdinal / nXPositions) * nXPositions);
129             } else {
130                 posIdx = cellOrdinal / nXPositions;
131             }
132
133             XMLA_Position position = (XMLA_Position) axes[i].getPositions().get(posIdx);
134             Member[] positionMembers = position.getMembers();
135             if (i ==1) {
136                 mdxBuf.append(",");
137             }
138             mdxBuf.append("{(");
139             for (int j = 0; j < positionMembers.length; j++) {
140                 XMLA_Member member = (XMLA_Member)positionMembers[j];
141                 if (j>0) {
142                   mdxBuf.append(",");
143                 }
144                 mdxBuf.append(member.getUniqueName());
145             }
146             mdxBuf.append(")}");
147             if (i == 0) {
148                 mdxBuf.append(" on Columns");
149             } else {
150                 mdxBuf.append(" on Rows");
151                 
152             }
153         }
154
155         mdxBuf.append(" from [" + this.model.getCube() +"]");
156         
157         Axis slicer = res.getSlicer();
158         // note at this stage we only deal with position 0
159
List JavaDoc slicerList = slicer.getPositions();
160         XMLA_Position slicerPosition = (XMLA_Position) slicerList.get(0);
161         Member[] slicerMembers = slicerPosition.getMembers();
162         for (int j = 0; j < slicerMembers.length; j++) {
163             XMLA_Member member = (XMLA_Member)slicerMembers[j];
164             if (j == 0) {
165                  mdxBuf.append(" Where ( ");
166             } else if (j>0) {
167                   mdxBuf.append(" , ");
168                 }
169             mdxBuf.append(member.getUniqueName());
170
171         }
172         
173         if (slicerMembers.length > 0 ) {
174               mdxBuf.append(")");
175             
176         }
177         return mdxBuf.toString();
178         
179         
180     }
181     
182     /**
183      * execute
184      * @throws Exception
185      */

186     private void executeQuery() {
187
188         XMLA_Result res = null;
189         try {
190          res = (XMLA_Result) model.getResult();
191
192         String JavaDoc mdx = buildDrillThroughMdx(res);
193         if (drillModel == null) {
194             drillModel = new XMLA_Model();
195             drillModel.setCatalog(model.getCatalog());
196             drillModel.setDataSource(model.getDataSource());
197             drillModel.setMdxQuery(mdx);
198             drillModel.setID("Drill" + model.getID());
199             drillModel.setUri(model.getUri());
200             drillModel.setUser(model.getUser());
201             drillModel.setPassword(model.getPassword());
202 // drillModel.initializeDrillThrough();
203
drillModel.initialize();
204         }
205         
206     
207         drillResult = (XMLA_Result) drillModel.getDrillResult();
208
209         // populate column header
210
// note the use of a Map for the column headers, this is because
211
// the xml returned for the rows contains variable number of columns
212
Map JavaDoc headerMap = drillResult.getDrillHeader();
213         int numCols = headerMap.size();
214         columnTitles = new String JavaDoc[numCols];
215         Set JavaDoc headerSet = headerMap.entrySet();
216         Iterator JavaDoc headerSetIt = headerSet.iterator();
217         while (headerSetIt.hasNext()) {
218             Map.Entry JavaDoc e = (Map.Entry JavaDoc) headerSetIt.next();;
219             columnTitles[((Integer JavaDoc)e.getValue()).intValue()] = e.getKey().toString();
220         }
221
222         // populate rows
223
List JavaDoc dataRows = drillResult.getDrillRows();
224         List JavaDoc tempRows = new ArrayList JavaDoc();
225         Iterator JavaDoc dataRowIt = dataRows.iterator();
226         while (dataRowIt.hasNext()) {
227             Object JavaDoc[] row = new Object JavaDoc[numCols];
228             Map JavaDoc columnMap = (HashMap JavaDoc)dataRowIt.next();
229             Set JavaDoc columnSet = columnMap.entrySet();
230             Iterator JavaDoc colSetIt = columnSet.iterator();
231             while (colSetIt.hasNext()) {
232                 Map.Entry JavaDoc e = (Map.Entry JavaDoc) colSetIt.next();
233                 String JavaDoc value = e.getValue().toString();
234                 String JavaDoc colName = e.getKey().toString();
235                 int colNo = ((Integer JavaDoc)headerMap.get(colName)).intValue();
236                 row[colNo] = value;
237             }
238             tempRows.add(new DefaultTableRow(row));
239         }
240         rows = (TableRow[]) tempRows.toArray(new TableRow[0]);
241         } catch (Exception JavaDoc e) {
242         e.printStackTrace();
243         logger.error("?", e);
244         rows = new TableRow[1];
245         columnTitles = new String JavaDoc[1];
246         columnTitles[0] = "An error occured";
247         Object JavaDoc[] row = new Object JavaDoc[1];
248         row[0] = e.toString();
249         rows[0] = new DefaultTableRow(row);
250         ready=false;
251         return;
252     } finally {
253         try {
254         } catch (Exception JavaDoc e1) {
255             // ignore
256
}
257     }
258     ready = true;
259     }
260     
261     /**
262      * @return
263      */

264     public String JavaDoc getCaption() {
265         return caption;
266     }
267
268     /**
269      * @param caption
270      */

271     public void setCaption(String JavaDoc caption) {
272         this.caption = caption;
273     }
274
275     /**
276      * @return
277      */

278     public String JavaDoc getDataSourceName() {
279         return dataSourceName;
280     }
281
282     /**
283      * @param string
284      */

285     public void setDataSourceName(String JavaDoc string) {
286         dataSourceName = string;
287     }
288
289 }
290
291
Popular Tags