001: package com.calipso.reportgenerator.userinterface;
002:
003: import com.calipso.reportgenerator.common.*;
004: import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
005: import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType;
006: import com.calipso.reportgenerator.services.FileSystemResolver;
007: import com.calipso.xmleditor.XMLWriter;
008:
009: import javax.swing.*;
010: import java.util.HashMap;
011: import java.io.*;
012:
013: import org.exolab.castor.xml.Marshaller;
014: import org.exolab.castor.xml.Unmarshaller;
015: import org.apache.commons.vfs.FileSystemManager;
016: import org.apache.commons.vfs.FileObject;
017:
018: /**
019: * Calipso Software
020: * User: Breto
021: * Date: 10/01/2006
022: * Time: 14:53:47
023: */
024: public class ReportLayoutGenerator {
025:
026: ReportDefinition reportDefinition;
027: ReportGeneratorConfiguration reportGeneratorConfiguration;
028: IReportManager reportManager;
029: JFrame reportViewerFrame;
030:
031: public ReportDefinition getReportDefinition() {
032: return reportDefinition;
033: }
034:
035: public ReportGeneratorConfiguration getReportGeneratorConfiguration() {
036: return reportGeneratorConfiguration;
037: }
038:
039: public IReportManager getReportManager() {
040: return reportManager;
041: }
042:
043: public ReportLayoutGenerator(ReportDefinition reportDefinition,
044: ReportGeneratorConfiguration reportGeneratorConfiguration,
045: IReportManager reportManager, JFrame reportViewerFrame) {
046: this .reportDefinition = reportDefinition;
047: this .reportGeneratorConfiguration = reportGeneratorConfiguration;
048: this .reportManager = reportManager;
049: this .reportViewerFrame = reportViewerFrame;
050: }
051:
052: /**
053: * Genera un report layout a partir de un Report Definition
054: */
055: protected void generateReportLayout() throws InfoException {
056: try {
057: String layoutName = reportDefinition.getId();
058: reportDefinition.setLayoutDesign("RL_" + layoutName);
059: saveReportDefinition();
060: reportManager.saveReportDefinition(reportDefinition);
061: ReportResult reportResult = getReportResult();
062: writeReportLayout(reportResult);
063: JOptionPane.showMessageDialog(reportViewerFrame,
064: LanguageTraslator.traslate("515") + " : RL_"
065: + layoutName);
066: JOptionPane.showMessageDialog(reportViewerFrame,
067: LanguageTraslator.traslate("556"));
068: JOptionPane.showMessageDialog(reportViewerFrame,
069: LanguageTraslator.traslate("558"));
070: } catch (Exception e) {
071: throw new InfoException(LanguageTraslator.traslate("516"),
072: e);
073: }
074:
075: }
076:
077: private void saveReportDefinition() throws InfoException {
078: FileWriter writer = null;
079: try {
080: writer = new FileWriter(getReportDefinitionFile());
081: Marshaller.marshal(reportDefinition, writer);
082: writer.flush();
083: writer.close();
084: } catch (Exception e) {
085: throw new InfoException(LanguageTraslator.traslate("552"),
086: e);
087: }
088: }
089:
090: private File getReportDefinitionFile() throws InfoException {
091: String pathReportDefinition = "";
092: try {
093: FileSystemManager fileSystemManager = FileSystemResolver
094: .getFileSystemManager(reportGeneratorConfiguration);
095: FileObject fileObject = fileSystemManager
096: .resolveFile(reportGeneratorConfiguration
097: .getSourceReportDefinitionsPath());
098: String fileName;
099: for (int i = 0; i < fileObject.getChildren().length; i++) {
100: fileName = fileObject.getChildren()[i].getName()
101: .getBaseName();
102: if (!fileName.endsWith(".xml")) {
103: continue;
104: }
105: String reportDefinitionIDViewer;
106: reportDefinitionIDViewer = ((ReportDefinition) Unmarshaller
107: .unmarshal(
108: ReportDefinition.class,
109: new FileReader(
110: reportGeneratorConfiguration
111: .getSourceReportDefinitionsPath()
112: + "/" + fileName)))
113: .getId();
114: if (reportDefinition.getId().equalsIgnoreCase(
115: reportDefinitionIDViewer)) {
116: pathReportDefinition = reportGeneratorConfiguration
117: .getSourceReportDefinitionsPath()
118: + "/" + fileName;
119: }
120: }
121: return new File(pathReportDefinition);
122: } catch (Exception e) {
123: throw new InfoException(LanguageTraslator.traslate("553"),
124: e);
125: }
126: }
127:
128: private ReportResult getReportResult() throws InfoException {
129: ReportResult result = null;
130: try {
131: if (reportDefinition.getReportType() != ReportDefinitionReportTypeType.STATICSQL) {
132: result = getReportManager().ExecReportQuery(
133: reportDefinition.getId(), new HashMap());
134: } else if (reportDefinition.getReportType() != ReportDefinitionReportTypeType.STATICSQL) {
135: int reportHandle = getReportManager().PrepareReport(
136: reportDefinition.getId());
137: result = getReportManager().ExecReportQuery(
138: reportHandle, new HashMap());
139: }
140: } catch (Exception e) {
141: throw new InfoException(LanguageTraslator.traslate("554"),
142: e);
143: }
144: return result;
145: }
146:
147: private String writeReportLayout(ReportResult result)
148: throws InfoException {
149: String layoutName = "";
150: try {
151: ReportSpec reportSpec = result.getReportSpec();
152: ReportLayoutBuilder builder = new ReportLayoutBuilder(
153: getReportGeneratorConfiguration(), result,
154: reportSpec);
155: IJasperDefinition jasper = builder
156: .buildDefaulJasperDefinition();
157: layoutName = "RL_" + reportDefinition.getId() + ".xml";
158: new XMLWriter(jasper.getJasperDefinition(true))
159: .saveDocument(reportGeneratorConfiguration
160: .getSourceReportLayoutPath()
161: + "/" + layoutName);
162: } catch (Exception e) {
163: throw new InfoException(LanguageTraslator.traslate("555"),
164: e);
165: }
166: return layoutName;
167: }
168:
169: }
|