KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianDrillThroughTableModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.mondrian;
14
15 import java.sql.Connection JavaDoc;
16 import java.sql.DriverManager JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.ResultSetMetaData JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Statement JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.sql.DataSource JavaDoc;
28
29 import mondrian.rolap.RolapConnectionProperties;
30 import mondrian.olap.MemoryLimitExceededException;
31 import mondrian.util.MemoryMonitor;
32 import mondrian.util.MemoryMonitorFactory;
33
34 import org.apache.log4j.Logger;
35
36 import com.tonbeller.wcf.table.AbstractTableModel;
37 import com.tonbeller.wcf.table.DefaultTableRow;
38 import com.tonbeller.wcf.table.TableRow;
39
40 /**
41  * A wcf table model for drill through data,
42  * requires an sql query and connection information to be set.
43  */

44
45 public class MondrianDrillThroughTableModel extends AbstractTableModel {
46     private static Logger logger = Logger.getLogger(MondrianDrillThroughTableModel.class);
47     private String JavaDoc title = "Drill Through Table";
48     private String JavaDoc caption = "";
49     private String JavaDoc sql = "";
50     private String JavaDoc jdbcUser;
51     private String JavaDoc jdbcUrl;
52     private String JavaDoc jdbcPassword;
53     private String JavaDoc jdbcDriver;
54     private String JavaDoc dataSourceName;
55     
56     private DataSource JavaDoc dataSource;
57     private static Context JavaDoc jndiContext;
58
59     private boolean ready = false;
60     
61     private TableRow[] rows = new TableRow[0];
62     private String JavaDoc [] columnTitles = new String JavaDoc[0];
63
64     public MondrianDrillThroughTableModel() {
65     }
66
67     public int getRowCount() {
68         if ( !ready ) {
69             executeQuery();
70         }
71         return rows.length;
72     }
73
74     public TableRow getRow(int rowIndex) {
75         if ( !ready ) {
76             executeQuery();
77         }
78         return rows[rowIndex];
79     }
80
81     public String JavaDoc getTitle() {
82         return title;
83     }
84     /**
85      * @return
86      */

87     public String JavaDoc getSql() {
88         return sql;
89     }
90
91     /**
92      * @param sql
93      */

94     public void setSql(String JavaDoc sql) {
95         this.sql = sql;
96         this.ready = false;
97     }
98
99     /**
100      * @param title
101      */

102     public void setTitle(String JavaDoc title) {
103         this.title = title;
104     }
105
106     /**
107      * wcf table component calls this method from it's constructor
108      * to get the number of columns
109      *
110      */

111     public int getColumnCount() {
112         if ( !ready ) {
113             executeQuery();
114         }
115         return columnTitles.length;
116     }
117
118     public String JavaDoc getColumnTitle(int columnIndex) {
119         if ( !ready ) {
120             executeQuery();
121         }
122         return columnTitles[columnIndex];
123     }
124
125     /**
126      * execute sql query
127      * @throws Exception
128      */

129     private void executeQuery() {
130         Connection JavaDoc con=null;
131         class Listener implements MemoryMonitor.Listener {
132             String JavaDoc oomMsg;
133             Listener() {
134             }
135             public void memoryUsageNotification(long used, long max) {
136                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(200);
137                 buf.append("OutOfMemory used=");
138                 buf.append(used);
139                 buf.append(", max=");
140                 buf.append(max);
141                 if (dataSourceName != null) {
142                     buf.append(" for data source: ");
143                     buf.append(dataSourceName);
144                 } else if (jdbcUrl != null) {
145                     buf.append(" for jcbc URL: ");
146                     buf.append(jdbcUrl);
147                 }
148                 this.oomMsg = buf.toString();
149             }
150             void check() throws MemoryLimitExceededException {
151                 if (oomMsg != null) {
152                     throw new MemoryLimitExceededException(oomMsg);
153                 }
154             }
155         }
156         Listener listener = new Listener();
157         MemoryMonitor mm = MemoryMonitorFactory.getMemoryMonitor();
158         try {
159             mm.addListener(listener);
160
161             con = getConnection();
162             Statement JavaDoc s = con.createStatement();
163             ResultSet JavaDoc rs = s.executeQuery(sql);
164             ResultSetMetaData JavaDoc md = rs.getMetaData();
165             int numCols = md.getColumnCount();
166             columnTitles = new String JavaDoc[numCols];
167
168             // check for OutOfMemory
169
listener.check();
170
171             // set column headings
172
for ( int i = 0; i < numCols; i++ ) {
173                 // columns are 1 based
174
columnTitles[i] = md.getColumnName(i+1);
175             }
176             title = title.concat(" for "+columnTitles[columnTitles.length-1]);
177             // loop through rows
178
List JavaDoc tempRows = new ArrayList JavaDoc();
179             while (rs.next()) {
180                 Object JavaDoc[] row = new Object JavaDoc[numCols];
181                 // loop on columns, 1 based
182
for ( int i = 0; i < numCols; i++ ) {
183                     row[i] = rs.getObject(i+1);
184                 }
185                 tempRows.add(new DefaultTableRow(row));
186
187                 // check for OutOfMemory
188
listener.check();
189             }
190             rs.close();
191             rows = (TableRow[]) tempRows.toArray(new TableRow[0]);
192         } catch (Exception JavaDoc e) {
193             e.printStackTrace();
194             logger.error("?", e);
195             // problem occured, set table model to zero size
196
rows = new TableRow[1];
197             columnTitles = new String JavaDoc[1];
198             columnTitles[0] = "An error occured";
199             Object JavaDoc[] row = new Object JavaDoc[1];
200             row[0] = e.toString();
201             rows[0] = new DefaultTableRow(row);
202             ready=false;
203             return;
204         } finally {
205             try {
206                 con.close();
207             } catch (Exception JavaDoc e1) {
208                 // ignore
209
}
210             mm.removeListener(listener);
211         }
212         ready = true;
213     }
214     
215     /**
216      * get sql connection
217      * @return
218      * @throws SQLException
219      */

220     private Connection JavaDoc getConnection() throws SQLException JavaDoc {
221         if (dataSourceName == null) {
222
223             if (jdbcUrl == null) {
224                 throw new RuntimeException JavaDoc(
225                         "Mondrian Connect string '" +
226                         "' must contain either '" +
227                         RolapConnectionProperties.Jdbc +
228                         "' or '" +
229                         RolapConnectionProperties.DataSource + "'");
230             }
231             return DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
232         } else {
233             return getDataSource().getConnection();
234         }
235     }
236
237     private DataSource JavaDoc getDataSource() {
238         if (dataSource == null) {
239             // Get connection from datasource.
240
try {
241                 dataSource = (DataSource JavaDoc) getJndiContext().lookup(dataSourceName);
242             } catch (NamingException JavaDoc e) {
243                 throw new RuntimeException JavaDoc(
244                     "Error while looking up data source (" +
245                     dataSourceName + ")", e);
246             }
247         }
248         return dataSource;
249     }
250     
251     private Context JavaDoc getJndiContext() throws NamingException JavaDoc {
252         if (jndiContext == null) {
253             jndiContext = new InitialContext JavaDoc();
254         }
255         return jndiContext;
256     }
257     /**
258      * @return
259      */

260     public String JavaDoc getJdbcDriver() {
261         return jdbcDriver;
262     }
263
264     /**
265      * @param jdbcDriver
266      */

267     public void setJdbcDriver(String JavaDoc jdbcDriver) {
268         this.jdbcDriver = jdbcDriver;
269     }
270
271     /**
272      * @return
273      */

274     public String JavaDoc getJdbcPassword() {
275         return jdbcPassword;
276     }
277
278     /**
279      * @param jdbcPassword
280      */

281     public void setJdbcPassword(String JavaDoc jdbcPassword) {
282         this.jdbcPassword = jdbcPassword;
283     }
284
285     /**
286      * @return
287      */

288     public String JavaDoc getJdbcUrl() {
289         return jdbcUrl;
290     }
291
292     /**
293      * @param jdbcUrl
294      */

295     public void setJdbcUrl(String JavaDoc jdbcUrl) {
296         this.jdbcUrl = jdbcUrl;
297     }
298
299     /**
300      * @return
301      */

302     public String JavaDoc getJdbcUser() {
303         return jdbcUser;
304     }
305
306     /**
307      * @param jdbcUser
308      */

309     public void setJdbcUser(String JavaDoc jdbcUser) {
310         this.jdbcUser = jdbcUser;
311     }
312
313     /**
314      * @return
315      */

316     public String JavaDoc getCaption() {
317         return caption;
318     }
319
320     /**
321      * @param caption
322      */

323     public void setCaption(String JavaDoc caption) {
324         this.caption = caption;
325     }
326
327     /**
328      * @return
329      */

330     public String JavaDoc getDataSourceName() {
331         return dataSourceName;
332     }
333
334     /**
335      * @param string
336      */

337     public void setDataSourceName(String JavaDoc string) {
338         dataSourceName = string;
339     }
340
341 }
342
Popular Tags