KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > services > DefaultSQLDataSourceResolver


1 package com.calipso.reportgenerator.services;
2
3 import com.calipso.reportgenerator.reportcalculator.IDataSource;
4 import com.calipso.reportgenerator.common.InfoException;
5 import com.calipso.reportgenerator.common.*;
6 import com.calipso.reportgenerator.reportcalculator.*;
7 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
8 import com.calipso.reportgenerator.common.LocalKeyEncrypter;
9 import com.calipso.common.DateEx;
10
11 import java.sql.*;
12 import java.util.*;
13 import java.util.Date JavaDoc;
14
15 /**
16  * Devuelve un ejecutor de SQL por defecto
17  */

18 public class DefaultSQLDataSourceResolver extends SQLDataSourceResolver{
19
20
21   public IDataSource execute(Matrix matrix) throws InfoException {
22     return getDataSource(getSqlText(), matrix);
23   }
24
25   /**
26    * Ejecuta la query, construye el datasource y lo devuelve
27    * @throws InfoException
28    */

29   public IDataSource getDataSource(String JavaDoc SQLText, Matrix matrix) throws InfoException {
30     //Matrix dataSource = null;
31
Statement stmt = null;
32     int colNum;
33     try {
34       ReportManagerLogger.debug(SQLText);
35       stmt = getConnection().createStatement();
36       ResultSet rs = null;
37       System.out.println(LanguageTraslator.traslate("499")+(new Date JavaDoc()).toString());
38       stmt.setFetchSize(getReportGeneratorConfiguration().getFetchSize());
39       rs = stmt.executeQuery(SQLText);
40       System.out.println(LanguageTraslator.traslate("500")+(new Date JavaDoc()).toString());
41       //colNum = getReportSpec().getNotCalculatedDimensions().size()+getReportSpec().getNotCalculatedMetrics().size();//rs.getMetaData().getColumnCount();
42
int dimCount = getReportSpec().getDimensionsByIndex().size();
43       colNum = dimCount + getReportSpec().getMetricsByIndex().size();
44       Vector columnsNames = new Vector();
45       columnsNames.addAll(getReportSpec().getColumnTypesByName().keySet());
46       //dataSource = (Matrix)DataSourceBuilder.buildDataSource(columnsNames, getReportGeneratorConfiguration(), getReportSpec());
47
/*************nueva parte**********/
48       /*if(getReportGeneratorConfiguration().isDatawareHouseEnabled()){
49         createCachedTable(getReportSpec(), dataSource);
50       }else{
51         Object[] collection = new Object[colNum];
52       }
53       /***********************************/

54       System.out.println(LanguageTraslator.traslate("501")+(new Date JavaDoc()).toString());
55       while (rs.next()) {
56         Object JavaDoc[] collection = new Object JavaDoc[colNum];
57         for (int i = 0; i < (colNum); i++) {
58           if(i < dimCount){
59             //Es dimension
60
ReportDimensionSpec dimension = getReportSpec().getDimensionFromIndex(i);
61             if(dimension.getCalculated()){
62               collection[i] = dimension.getValue(collection, this.dataSource.getReportDataSourceSpec());
63             }else{
64               collection[i] = this.dataSource.getValueForDimension(rs.getObject(dimension.getExternalData()), dimension, collection, i);
65             }
66           }else{
67             //Es metrica
68
ReportMetricSpec metric = getReportSpec().getMetricFromIndex(i - dimCount);
69             if(metric.getCalculated()){
70               collection[i] = metric.getValue(collection);
71             }else{
72               collection[i] = this.dataSource.getValueForMetric(rs.getObject(metric.getExternalData()), metric, collection, i);
73             }
74           }
75
76           /*
77           try{
78             type = getColumnType(i);
79             try {
80               defaultValue = rs.getObject(getColumnExternalData(i));// getString(i).trim();
81             }catch(Exception e){
82               try{
83                 defaultValue = rs.getObject(getColumnName(i)); // getString(i).trim();
84               }catch(Exception e1){
85                 throw new InfoException("Columna no encontrada", e1);
86               }
87             }
88             try{
89               switch(type){
90                 case ReportDataType.FLOAT_TYPE:
91                   SharedFloat sharedFloat = SharedFloat.newFrom(new Float(defaultValue.toString().trim()));
92                   collection.add(sharedFloat);
93                   break;
94                 case ReportDataType.INTEGER_TYPE:
95                   SharedInteger sharedInteger = SharedInteger.newFrom(new Integer(defaultValue.toString().trim()));
96                   collection.add(sharedInteger);
97                   break;
98                 case ReportDataType.STRING_TYPE:
99                   collection.add(SharedString.newFrom(defaultValue.toString().trim()));
100                   break;
101                 case ReportDataType.DATETIME_TYPE:
102                 case ReportDataType.DATE_TYPE:
103                   DateEx dateEx;
104                   if(defaultValue instanceof Date){
105                     dateEx = new DateEx((Date)defaultValue);
106                   } else {
107                     dateEx = new DateEx(defaultValue, getReportDataSourceSpec().getPattern(getReportSpec().getDimensionFromIndex(i).getDataType().getType()));
108                   }
109                   SharedDate sharedDate = SharedDate.newFrom(dateEx);
110                   collection.add(sharedDate);
111                   break;
112                 default:
113                   collection.add(SharedString.newFrom(defaultValue.toString().trim()));
114                   break;
115               }
116             }catch(Exception e){
117               collection.add(SharedString.newFrom(defaultValue.toString().trim()));
118             }
119           }catch(Exception e){
120             collection.add(SharedString.newFrom(""));
121           }*/

122         }
123         /****** New ******/
124         matrix.add(collection);
125       }
126     } catch (Exception JavaDoc e) {
127       throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("95"), e);
128     }
129     System.out.println(LanguageTraslator.traslate("502")+(new Date JavaDoc()).toString());
130     return matrix;
131   }
132
133   /*private void createCachedTable(ReportSpec reportSpec, IDataSource dataSource) throws InfoException {
134     //if(dataSource instanceof DatawarehouseMatrix){
135       ((DatawarehouseMatrix)dataSource).createCachedTable(reportSpec);
136     //}
137   } */

138
139   /*private String getColumnExternalData(int i) {
140     if (getReportSpec().getNotCalculatedDimensions().size() > i){
141       return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getExternalData();
142     } else{
143       return ((ReportMetricSpec)getReportSpec().getNotCalculatedMetrics().toArray()[i-(getReportSpec().getNotCalculatedDimensions().size())]).getExternalData();
144     }
145   }
146
147   private String getColumnName(int i) {
148     if (getReportSpec().getNotCalculatedDimensions().size() > i){
149       return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getName();
150     } else{
151       return ((ReportMetricSpec)getReportSpec().getNotCalculatedMetrics().toArray()[i-(getReportSpec().getNotCalculatedDimensions().size())]).getName();
152     }
153   }*/

154
155
156   /*private int getColumnType(int i) {
157     return getColumnTypes()[i];
158     if (getReportSpec().getNotCalculatedDimensions().size() > i){
159       return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getDataType().getType();
160     } else{
161       return ReportDataType.FLOAT_TYPE;
162     }
163   }*/

164
165
166   /**
167    * Retorna una connection de base de dato dependiendo del contexto donde se ejecuta. Local o distribuido.
168    * @return
169    * @throws InfoException
170    */

171   protected Connection getConnection() throws InfoException {
172     Connection con = null;
173 /* if(!getReportGeneratorConfiguration().getIsDistributed()) {*/
174       try {
175         Class.forName(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationClassName"));
176         con = DriverManager.getConnection(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationLocalUrl"), getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationUser"), getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationPassword"));
177       } catch (Exception JavaDoc e) {
178         try {
179           String JavaDoc user = LocalKeyEncrypter.decrypt(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationUser"));
180           String JavaDoc pass =LocalKeyEncrypter.decrypt(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationPassword"));
181           Class.forName(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationClassName"));
182           con = DriverManager.getConnection(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationLocalUrl"), user, pass);
183         } catch (ClassNotFoundException JavaDoc e1) {
184           throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("95"), e1);
185         } catch (SQLException e2) {
186           throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("95"), e2);
187         } catch (Exception JavaDoc e3) {
188           throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("95"));
189         }
190       }
191 /* } else {
192       try {
193         Context jndiCntx = new InitialContext();
194         javax.sql.DataSource ds = (javax.sql.DataSource)jndiCntx.lookup(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationDistributedUrl"));
195         con = ds.getConnection();
196       } catch(Exception e) {
197         throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("362"), e);
198       }
199     }
200 */
return con;
201   }
202
203 }
204
Popular Tags