001: package com.calipso.reportgenerator.services;
002:
003: import com.calipso.reportgenerator.reportcalculator.IDataSource;
004: import com.calipso.reportgenerator.common.InfoException;
005: import com.calipso.reportgenerator.common.*;
006: import com.calipso.reportgenerator.reportcalculator.*;
007: import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
008: import com.calipso.reportgenerator.common.LocalKeyEncrypter;
009: import com.calipso.common.DateEx;
010:
011: import java.sql.*;
012: import java.util.*;
013: import java.util.Date;
014:
015: /**
016: * Devuelve un ejecutor de SQL por defecto
017: */
018: public class DefaultSQLDataSourceResolver extends SQLDataSourceResolver {
019:
020: public IDataSource execute(Matrix matrix) throws InfoException {
021: return getDataSource(getSqlText(), matrix);
022: }
023:
024: /**
025: * Ejecuta la query, construye el datasource y lo devuelve
026: * @throws InfoException
027: */
028: public IDataSource getDataSource(String SQLText, Matrix matrix)
029: throws InfoException {
030: //Matrix dataSource = null;
031: Statement stmt = null;
032: int colNum;
033: try {
034: ReportManagerLogger.debug(SQLText);
035: stmt = getConnection().createStatement();
036: ResultSet rs = null;
037: System.out.println(LanguageTraslator.traslate("499")
038: + (new Date()).toString());
039: stmt.setFetchSize(getReportGeneratorConfiguration()
040: .getFetchSize());
041: rs = stmt.executeQuery(SQLText);
042: System.out.println(LanguageTraslator.traslate("500")
043: + (new Date()).toString());
044: //colNum = getReportSpec().getNotCalculatedDimensions().size()+getReportSpec().getNotCalculatedMetrics().size();//rs.getMetaData().getColumnCount();
045: int dimCount = getReportSpec().getDimensionsByIndex()
046: .size();
047: colNum = dimCount
048: + getReportSpec().getMetricsByIndex().size();
049: Vector columnsNames = new Vector();
050: columnsNames.addAll(getReportSpec().getColumnTypesByName()
051: .keySet());
052: //dataSource = (Matrix)DataSourceBuilder.buildDataSource(columnsNames, getReportGeneratorConfiguration(), getReportSpec());
053: /*************nueva parte**********/
054: /*if(getReportGeneratorConfiguration().isDatawareHouseEnabled()){
055: createCachedTable(getReportSpec(), dataSource);
056: }else{
057: Object[] collection = new Object[colNum];
058: }
059: /***********************************/
060: System.out.println(LanguageTraslator.traslate("501")
061: + (new Date()).toString());
062: while (rs.next()) {
063: Object[] collection = new Object[colNum];
064: for (int i = 0; i < (colNum); i++) {
065: if (i < dimCount) {
066: //Es dimension
067: ReportDimensionSpec dimension = getReportSpec()
068: .getDimensionFromIndex(i);
069: if (dimension.getCalculated()) {
070: collection[i] = dimension.getValue(
071: collection, this .dataSource
072: .getReportDataSourceSpec());
073: } else {
074: collection[i] = this .dataSource
075: .getValueForDimension(
076: rs.getObject(dimension
077: .getExternalData()),
078: dimension, collection, i);
079: }
080: } else {
081: //Es metrica
082: ReportMetricSpec metric = getReportSpec()
083: .getMetricFromIndex(i - dimCount);
084: if (metric.getCalculated()) {
085: collection[i] = metric.getValue(collection);
086: } else {
087: collection[i] = this .dataSource
088: .getValueForMetric(
089: rs.getObject(metric
090: .getExternalData()),
091: metric, collection, i);
092: }
093: }
094:
095: /*
096: try{
097: type = getColumnType(i);
098: try {
099: defaultValue = rs.getObject(getColumnExternalData(i));// getString(i).trim();
100: }catch(Exception e){
101: try{
102: defaultValue = rs.getObject(getColumnName(i)); // getString(i).trim();
103: }catch(Exception e1){
104: throw new InfoException("Columna no encontrada", e1);
105: }
106: }
107: try{
108: switch(type){
109: case ReportDataType.FLOAT_TYPE:
110: SharedFloat sharedFloat = SharedFloat.newFrom(new Float(defaultValue.toString().trim()));
111: collection.add(sharedFloat);
112: break;
113: case ReportDataType.INTEGER_TYPE:
114: SharedInteger sharedInteger = SharedInteger.newFrom(new Integer(defaultValue.toString().trim()));
115: collection.add(sharedInteger);
116: break;
117: case ReportDataType.STRING_TYPE:
118: collection.add(SharedString.newFrom(defaultValue.toString().trim()));
119: break;
120: case ReportDataType.DATETIME_TYPE:
121: case ReportDataType.DATE_TYPE:
122: DateEx dateEx;
123: if(defaultValue instanceof Date){
124: dateEx = new DateEx((Date)defaultValue);
125: } else {
126: dateEx = new DateEx(defaultValue, getReportDataSourceSpec().getPattern(getReportSpec().getDimensionFromIndex(i).getDataType().getType()));
127: }
128: SharedDate sharedDate = SharedDate.newFrom(dateEx);
129: collection.add(sharedDate);
130: break;
131: default:
132: collection.add(SharedString.newFrom(defaultValue.toString().trim()));
133: break;
134: }
135: }catch(Exception e){
136: collection.add(SharedString.newFrom(defaultValue.toString().trim()));
137: }
138: }catch(Exception e){
139: collection.add(SharedString.newFrom(""));
140: }*/
141: }
142: /****** New ******/
143: matrix.add(collection);
144: }
145: } catch (Exception e) {
146: throw new InfoException(
147: com.calipso.reportgenerator.common.LanguageTraslator
148: .traslate("95"), e);
149: }
150: System.out.println(LanguageTraslator.traslate("502")
151: + (new Date()).toString());
152: return matrix;
153: }
154:
155: /*private void createCachedTable(ReportSpec reportSpec, IDataSource dataSource) throws InfoException {
156: //if(dataSource instanceof DatawarehouseMatrix){
157: ((DatawarehouseMatrix)dataSource).createCachedTable(reportSpec);
158: //}
159: } */
160:
161: /*private String getColumnExternalData(int i) {
162: if (getReportSpec().getNotCalculatedDimensions().size() > i){
163: return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getExternalData();
164: } else{
165: return ((ReportMetricSpec)getReportSpec().getNotCalculatedMetrics().toArray()[i-(getReportSpec().getNotCalculatedDimensions().size())]).getExternalData();
166: }
167: }
168:
169: private String getColumnName(int i) {
170: if (getReportSpec().getNotCalculatedDimensions().size() > i){
171: return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getName();
172: } else{
173: return ((ReportMetricSpec)getReportSpec().getNotCalculatedMetrics().toArray()[i-(getReportSpec().getNotCalculatedDimensions().size())]).getName();
174: }
175: }*/
176:
177: /*private int getColumnType(int i) {
178: return getColumnTypes()[i];
179: if (getReportSpec().getNotCalculatedDimensions().size() > i){
180: return ((ReportDimensionSpec)getReportSpec().getNotCalculatedDimensions().toArray()[i]).getDataType().getType();
181: } else{
182: return ReportDataType.FLOAT_TYPE;
183: }
184: }*/
185:
186: /**
187: * Retorna una connection de base de dato dependiendo del contexto donde se ejecuta. Local o distribuido.
188: * @return
189: * @throws InfoException
190: */
191: protected Connection getConnection() throws InfoException {
192: Connection con = null;
193: /* if(!getReportGeneratorConfiguration().getIsDistributed()) {*/
194: try {
195: Class.forName(getDataSourceDefinitionConnectionString()
196: .getValue("DatabaseConfigurationClassName"));
197: con = DriverManager.getConnection(
198: getDataSourceDefinitionConnectionString().getValue(
199: "DatabaseConfigurationLocalUrl"),
200: getDataSourceDefinitionConnectionString().getValue(
201: "DatabaseConfigurationUser"),
202: getDataSourceDefinitionConnectionString().getValue(
203: "DatabaseConfigurationPassword"));
204: } catch (Exception e) {
205: try {
206: String user = LocalKeyEncrypter
207: .decrypt(getDataSourceDefinitionConnectionString()
208: .getValue("DatabaseConfigurationUser"));
209: String pass = LocalKeyEncrypter
210: .decrypt(getDataSourceDefinitionConnectionString()
211: .getValue(
212: "DatabaseConfigurationPassword"));
213: Class.forName(getDataSourceDefinitionConnectionString()
214: .getValue("DatabaseConfigurationClassName"));
215: con = DriverManager
216: .getConnection(
217: getDataSourceDefinitionConnectionString()
218: .getValue(
219: "DatabaseConfigurationLocalUrl"),
220: user, pass);
221: } catch (ClassNotFoundException e1) {
222: throw new InfoException(
223: com.calipso.reportgenerator.common.LanguageTraslator
224: .traslate("95"), e1);
225: } catch (SQLException e2) {
226: throw new InfoException(
227: com.calipso.reportgenerator.common.LanguageTraslator
228: .traslate("95"), e2);
229: } catch (Exception e3) {
230: throw new InfoException(
231: com.calipso.reportgenerator.common.LanguageTraslator
232: .traslate("95"));
233: }
234: }
235: /* } else {
236: try {
237: Context jndiCntx = new InitialContext();
238: javax.sql.DataSource ds = (javax.sql.DataSource)jndiCntx.lookup(getDataSourceDefinitionConnectionString().getValue("DatabaseConfigurationDistributedUrl"));
239: con = ds.getConnection();
240: } catch(Exception e) {
241: throw new InfoException(com.calipso.reportgenerator.common.LanguageTraslator.traslate("362"), e);
242: }
243: }
244: */return con;
245: }
246:
247: }
|