KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > ReportLayoutGenerator


1 package com.calipso.reportgenerator.userinterface;
2
3 import com.calipso.reportgenerator.common.*;
4 import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
5 import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType;
6 import com.calipso.reportgenerator.services.FileSystemResolver;
7 import com.calipso.xmleditor.XMLWriter;
8
9 import javax.swing.*;
10 import java.util.HashMap JavaDoc;
11 import java.io.*;
12
13 import org.exolab.castor.xml.Marshaller;
14 import org.exolab.castor.xml.Unmarshaller;
15 import org.apache.commons.vfs.FileSystemManager;
16 import org.apache.commons.vfs.FileObject;
17
18 /**
19  * Calipso Software
20  * User: Breto
21  * Date: 10/01/2006
22  * Time: 14:53:47
23  */

24 public class ReportLayoutGenerator {
25
26   ReportDefinition reportDefinition;
27   ReportGeneratorConfiguration reportGeneratorConfiguration;
28   IReportManager reportManager;
29   JFrame reportViewerFrame;
30
31   public ReportDefinition getReportDefinition() {
32     return reportDefinition;
33   }
34
35   public ReportGeneratorConfiguration getReportGeneratorConfiguration() {
36     return reportGeneratorConfiguration;
37   }
38
39   public IReportManager getReportManager() {
40     return reportManager;
41   }
42
43   public ReportLayoutGenerator(ReportDefinition reportDefinition, ReportGeneratorConfiguration reportGeneratorConfiguration, IReportManager reportManager, JFrame reportViewerFrame) {
44     this.reportDefinition = reportDefinition;
45     this.reportGeneratorConfiguration = reportGeneratorConfiguration;
46     this.reportManager = reportManager;
47     this.reportViewerFrame = reportViewerFrame;
48   }
49
50   /**
51    * Genera un report layout a partir de un Report Definition
52    */

53   protected void generateReportLayout() throws InfoException {
54         try{
55           String JavaDoc layoutName = reportDefinition.getId();
56           reportDefinition.setLayoutDesign("RL_"+layoutName);
57           saveReportDefinition();
58           reportManager.saveReportDefinition(reportDefinition);
59           ReportResult reportResult = getReportResult();
60           writeReportLayout(reportResult);
61           JOptionPane.showMessageDialog(reportViewerFrame, LanguageTraslator.traslate("515")+" : RL_"+layoutName);
62           JOptionPane.showMessageDialog(reportViewerFrame, LanguageTraslator.traslate("556"));
63           JOptionPane.showMessageDialog(reportViewerFrame, LanguageTraslator.traslate("558"));
64         }catch (Exception JavaDoc e){
65           throw new InfoException(LanguageTraslator.traslate("516"),e);
66         }
67
68
69 }
70
71   private void saveReportDefinition() throws InfoException {
72     FileWriter writer = null;
73     try {
74       writer = new FileWriter(getReportDefinitionFile());
75       Marshaller.marshal(reportDefinition, writer);
76       writer.flush();
77       writer.close();
78     } catch (Exception JavaDoc e) {
79       throw new InfoException(LanguageTraslator.traslate("552"),e);
80     }
81   }
82
83   private File getReportDefinitionFile() throws InfoException {
84     String JavaDoc pathReportDefinition = "";
85     try {
86         FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(reportGeneratorConfiguration);
87         FileObject fileObject = fileSystemManager.resolveFile(reportGeneratorConfiguration.getSourceReportDefinitionsPath());
88         String JavaDoc fileName;
89         for (int i = 0; i < fileObject.getChildren().length; i++) {
90           fileName = fileObject.getChildren()[i].getName().getBaseName();
91           if(!fileName.endsWith(".xml")) {
92               continue;
93           }
94           String JavaDoc reportDefinitionIDViewer;
95           reportDefinitionIDViewer = ((ReportDefinition) Unmarshaller.unmarshal(ReportDefinition.class, new FileReader(reportGeneratorConfiguration.getSourceReportDefinitionsPath() + "/" + fileName))).getId();
96           if (reportDefinition.getId().equalsIgnoreCase(reportDefinitionIDViewer)){
97               pathReportDefinition = reportGeneratorConfiguration.getSourceReportDefinitionsPath() + "/" + fileName;
98           }
99         }
100         return new File(pathReportDefinition);
101     }catch ( Exception JavaDoc e ){
102       throw new InfoException(LanguageTraslator.traslate("553"),e);
103     }
104   }
105
106
107   private ReportResult getReportResult() throws InfoException {
108     ReportResult result = null;
109     try{
110         if(reportDefinition.getReportType()!= ReportDefinitionReportTypeType.STATICSQL){
111           result = getReportManager().ExecReportQuery(reportDefinition.getId(), new HashMap JavaDoc());
112         }else if(reportDefinition.getReportType()!=ReportDefinitionReportTypeType.STATICSQL){
113           int reportHandle = getReportManager().PrepareReport(reportDefinition.getId());
114           result = getReportManager().ExecReportQuery(reportHandle, new HashMap JavaDoc());
115         }
116     }catch (Exception JavaDoc e){
117       throw new InfoException(LanguageTraslator.traslate("554"),e);
118     }
119     return result;
120   }
121
122
123   private String JavaDoc writeReportLayout(ReportResult result) throws InfoException {
124     String JavaDoc layoutName = "";
125     try{
126       ReportSpec reportSpec = result.getReportSpec();
127       ReportLayoutBuilder builder = new ReportLayoutBuilder(getReportGeneratorConfiguration(), result, reportSpec);
128       IJasperDefinition jasper = builder.buildDefaulJasperDefinition();
129       layoutName = "RL_"+reportDefinition.getId() + ".xml";
130       new XMLWriter(jasper.getJasperDefinition(true)).saveDocument(reportGeneratorConfiguration.getSourceReportLayoutPath() + "/" + layoutName);
131     }catch (Exception JavaDoc e){
132       throw new InfoException(LanguageTraslator.traslate("555"),e);
133     }
134     return layoutName;
135   }
136
137 }
138
Popular Tags