001: package com.calipso.reportgenerator.userinterface;
002:
003: import com.calipso.xmleditor.XmlEditorConnectionPane;
004: import com.calipso.xmleditor.DataTypeTableFrame;
005: import com.calipso.xmleditor.XmlEditorDefaultReports;
006: import com.calipso.xmleditor.XmlEditorException;
007: import com.calipso.reportgenerator.common.LanguageTraslator;
008: import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
009: import com.calipso.reportgenerator.common.InfoException;
010: import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
011: import com.calipso.reportgenerator.reportdefinitions.ReportSourceDefinition;
012:
013: import javax.swing.*;
014: import java.util.Map;
015: import java.util.HashMap;
016: import java.util.Vector;
017: import java.sql.*;
018: import java.io.*;
019: import java.awt.event.ActionListener;
020:
021: import org.apache.xalan.serialize.WriterToUTF8;
022: import org.exolab.castor.xml.Marshaller;
023:
024: /**
025: * Calipso Software
026: * User: Breto
027: * Date: 03/01/2006
028: * Time: 12:12:41
029: */
030: public class ReportFromSQL {
031: private XmlEditorConnectionPane connectionPane;
032: private Map typeColumn = new HashMap();
033: private JFrame frame;
034: private ReportGeneratorConfiguration reportGeneratorConfiguration;
035:
036: public ReportFromSQL(JFrame owner,
037: ReportGeneratorConfiguration generatorConfiguration) {
038: frame = owner;
039: reportGeneratorConfiguration = generatorConfiguration;
040: }
041:
042: public void createReportFrame() throws InfoException {
043: XmlEditorConnectionPane connectionPane = getConnectionPane();
044: connectionPane.pack();
045: connectionPane.setVisible(true);
046: if ((!connectionPane.isCancelled())) {
047: if (createReportDefinitionsFromSql(connectionPane
048: .getReportName(), connectionPane.getClassName(),
049: connectionPane.getLocalUrl(), connectionPane
050: .getUser(), String.valueOf(connectionPane
051: .getPassword()), connectionPane
052: .getSqlText())) {
053: JOptionPane.showMessageDialog(frame, LanguageTraslator
054: .traslate("559"));
055: JOptionPane.showMessageDialog(frame, LanguageTraslator
056: .traslate("556"));
057: }
058: }
059: }
060:
061: public boolean createReportDefinitionsFromSql(String reportName,
062: String className, String localUrl, String user,
063: String password, String sql) throws InfoException {
064: Map currentColumnsDimensions;
065: Map currentColumnsMetrics;
066:
067: try {
068: connectionPane.setVisible(false);
069: DataTypeTableFrame dataTypeTableFrame;
070: Class.forName(className);
071: System.out.print(LanguageTraslator.traslate("519"));
072: Connection con = DriverManager.getConnection(localUrl,
073: user, password);
074: Statement stmt = con.createStatement();
075: //stmt.setMaxRows();
076: //stmt.setFetchSize();
077: ResultSet rs = null;
078: try {
079: rs = stmt.executeQuery(prepareSQL(sql));
080: } catch (Exception ee) {
081: throw new InfoException(LanguageTraslator
082: .traslate("551"), ee);
083: }
084: ResultSetMetaData meta = rs.getMetaData();
085:
086: System.out.println(LanguageTraslator.traslate("520"));
087: currentColumnsDimensions = getColumnsDimensionsMap(meta);
088: currentColumnsMetrics = getColumnsMetricsMap(meta);
089: dataTypeTableFrame = new DataTypeTableFrame(frame,
090: typeColumn);
091: dataTypeTableFrame.setVisible(true);
092: if (!dataTypeTableFrame.isCancelled()) {
093: Map map2 = dataTypeTableFrame.getRowsLatestVersion();
094: dataTypeTableFrame.setVisible(false);
095: currentColumnsDimensions = getColumnsDimensionsMap(map2);
096: currentColumnsMetrics = getColumnsMetricsMap(map2);
097: }
098:
099: if ((currentColumnsDimensions.size() != 0)
100: || (currentColumnsMetrics.size() != 0)) {
101: writeReports(reportName, currentColumnsDimensions,
102: currentColumnsMetrics, prepareSQL(sql),
103: className, localUrl, user, password);
104: File xmlFile = new File(reportGeneratorConfiguration
105: .getSourceReportDefinitionsPath()
106: + "/"
107: + XmlEditorDefaultReports
108: .getReportId(reportName) + ".xml");
109: return true;
110: }
111: } catch (Exception e) {
112: throw new InfoException(LanguageTraslator.traslate("565"),
113: e);
114: }
115: return false;
116: }
117:
118: private String prepareSQL(String sql) {
119: return sql.replaceAll("\n", " ");
120: }
121:
122: private XmlEditorConnectionPane getConnectionPane() {
123: if (connectionPane == null) {
124: connectionPane = new XmlEditorConnectionPane(frame, true,
125: reportGeneratorConfiguration.getValues());
126: }
127: return connectionPane;
128: }
129:
130: private Map getColumnsDimensionsMap(ResultSetMetaData meta)
131: throws InfoException {
132: Map result = new HashMap();
133: int cantidad = 0;
134:
135: try {
136: cantidad = meta.getColumnCount();
137: for (int i = 0; i < cantidad; i++) {
138: if (!(isMetric(meta.getColumnType(i + 1)))) {
139: System.out.println(LanguageTraslator
140: .traslate("280")
141: + meta.getColumnName(i + 1));
142: result.put(meta.getColumnName(i + 1), new Integer(
143: meta.getColumnType(i + 1)));
144: Vector data = new Vector(3);
145: data.add(meta.getColumnName(i + 1));
146: data.add("Dimension");
147: data.add(new Integer(meta.getColumnType(i + 1)));
148: ; // ver si es necesario q devuelva int ...
149: typeColumn.put(new Integer(i + 1), data);
150: }
151: }
152: } catch (Exception e) {
153: throw new InfoException(LanguageTraslator.traslate("566"),
154: e);
155: }
156: return result;
157: }
158:
159: private Map getColumnsMetricsMap(ResultSetMetaData meta)
160: throws InfoException {
161: Map result = new HashMap();
162: int cantidad = 0;
163: try {
164: cantidad = meta.getColumnCount();
165: for (int i = 0; i < cantidad; i++) {
166: if (isMetric(meta.getColumnType(i + 1))) {
167: System.out.println(LanguageTraslator
168: .traslate("529")
169: + meta.getColumnName(i + 1));
170: result.put(meta.getColumnName(i + 1), new Integer(
171: meta.getColumnType(i + 1)));
172: Vector data = new Vector(3);
173: data.add(meta.getColumnName(i + 1));
174: data.add("Metric");
175: data.add(new Integer(meta.getColumnType(i + 1))); // ver si es necesario q devuelva int ...
176: typeColumn.put(new Integer(i + 1), data);
177: }
178: }
179: } catch (Exception e) {
180: throw new InfoException(LanguageTraslator.traslate("567"),
181: e);
182: }
183: return result;
184: }
185:
186: /**
187: * Verifica que el tipo de dato de la columna sea acumulable(metrica)
188: * @param typeColumn
189: * @return
190: */
191:
192: private boolean isMetric(int typeColumn) {
193:
194: switch (typeColumn) {
195: case Types.TINYINT:
196: case Types.SMALLINT:
197: case Types.INTEGER:
198: case Types.BIGINT:
199: case Types.FLOAT:
200: case Types.DOUBLE:
201: case Types.REAL:
202: case Types.DECIMAL:
203: case Types.NUMERIC:
204: return true;
205: default:
206: return false;
207: }
208: }
209:
210: private Map getColumnsDimensionsMap(Map map) {
211: Map mapAux = new HashMap();
212: for (int i = 0; i < (this .typeColumn.keySet().size()); i++) {
213: Vector vector = (Vector) map.get(new Integer(1 + i));
214: if (vector != null) {
215: String columnName;
216: Vector vec = new Vector(2);
217: Integer columnType;
218: String extData;
219: if (vector.get(1).equals("Dimension")) {
220: columnName = (String) vector.get(0);
221: columnType = getIdType((String) vector.get(2));
222: extData = (String) vector.get(3);
223: vec.add(columnType);
224: vec.add(extData);
225: mapAux.put(columnName, vec);
226: }
227: }
228: }
229: return mapAux;
230: }
231:
232: private Integer getIdType(String s) {
233: if (s.equalsIgnoreCase("DECIMAL")) {
234: return new Integer(3);
235: }
236: if (s.equalsIgnoreCase("INTEGER")) {
237: return new Integer(4);
238: }
239: if (s.equalsIgnoreCase("FLOAT")) {
240: return new Integer(6);
241: }
242: if (s.equalsIgnoreCase("BOOLEAN")) {
243: return new Integer(16);
244: }
245: if (s.equalsIgnoreCase("DATETIME")) {
246: return new Integer(91);
247: } else {
248: return new Integer(12);
249: }
250: }
251:
252: private Map getColumnsMetricsMap(Map map) {
253: Map mapAux = new HashMap();
254: for (int i = 0; i < (this .typeColumn.keySet().size()); i++) {
255: Vector vector = (Vector) map.get(new Integer(1 + i));
256: if (vector != null) {
257: String columnName;
258: Vector vec = new Vector(2);
259: Integer columnType;
260: String extData;
261: if (vector.get(1).equals("Metric")) {
262: columnName = (String) vector.get(0);
263: columnType = getIdType((String) vector.get(2));
264: extData = (String) vector.get(3);
265: vec.add(columnType);
266: vec.add(extData);
267: mapAux.put(columnName, vec);
268: }
269: }
270: }
271: return mapAux;
272: }
273:
274: /**
275: * Con los datos obtenidos de la query genera los archivos XML que seran la especificacion del reporte.
276: * @param reportName
277: * @param dimensionMap
278: * @param metricMap
279: * @param query
280: * @param className
281: * @param localUrl
282: * @param user
283: * @param password
284: */
285: private void writeReports(String reportName, Map dimensionMap,
286: Map metricMap, String query, String className,
287: String localUrl, String user, String password)
288: throws InfoException {
289: System.out.println(LanguageTraslator.traslate("521"));
290: String reportPath = writeReportDefinition(reportName,
291: dimensionMap, metricMap);
292: System.out.println("reportName:" + reportName
293: + "dimensionMap: " + dimensionMap.size() + "tostring:"
294: + dimensionMap.toString() + "metricMap:"
295: + metricMap.toString());
296: System.out.println(LanguageTraslator.traslate("522"));
297: reportPath = writeReportSourceDefinition(reportName,
298: dimensionMap, metricMap, query, className, localUrl,
299: user, password);
300: }
301:
302: /**
303: * Genera en base a los datos obtenidos una ReportDefinition estandar.
304: * @param reportName
305: * @param dimensionMap
306: * @param metricMap
307: * @return
308: */
309: private String writeReportDefinition(String reportName,
310: Map dimensionMap, Map metricMap) throws InfoException {
311: ReportDefinition report = XmlEditorDefaultReports
312: .getReportDefinition(reportGeneratorConfiguration,
313: reportName, dimensionMap, metricMap);
314: System.out.println("report.toString():" + report.toString());
315: System.out.println("report.toString():"
316: + report.getDimensionDefinitions()
317: .getDimensionDefinition().length
318: + ":"
319: + report.getDimensionDefinitions()
320: .enumerateDimensionDefinition().toString());
321: String fullPath = reportGeneratorConfiguration
322: .getSourceReportDefinitionsPath()
323: + "/"
324: + XmlEditorDefaultReports.getReportId(reportName)
325: + ".xml";
326: try {
327: System.out.println(LanguageTraslator.traslate("525"));
328: Writer wr = new FileWriter(fullPath);
329: report.marshal(wr);
330: wr.flush();
331: wr.close();
332: } catch (Exception e) {
333: e.printStackTrace();
334: throw new InfoException(LanguageTraslator.traslate("568"),
335: e);
336: }
337: return fullPath;
338: }
339:
340: private String writeReportSourceDefinition(String reportName,
341: Map dimensionMap, Map metricMap, String query,
342: String className, String localUrl, String user,
343: String password) throws InfoException {
344: ReportSourceDefinition report = XmlEditorDefaultReports
345: .getReportSourceDefinition(reportName, dimensionMap,
346: metricMap, query, className, localUrl, user,
347: password);
348: String fullPath = reportGeneratorConfiguration
349: .getSourceReportSourceDefinitionsPath()
350: + "/"
351: + XmlEditorDefaultReports.getSourceId(reportName)
352: + ".xml";
353: try {
354: System.out.println(LanguageTraslator.traslate("524"));
355: Writer wr = new FileWriter(fullPath);
356: report.marshal(wr);
357: wr.flush();
358: wr.close();
359: } catch (Exception e) {
360: throw new InfoException(LanguageTraslator.traslate("569"),
361: e);
362: }
363: return fullPath;
364: }
365:
366: }
|