summaryrefslogtreecommitdiff
path: root/jasper_reports/java/com/nantic/jasperreports/JasperServer.java
blob: cd3e3b27c9fafbf1204eafef831c00890fe4b900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright (c) 2008-2012 NaN Projectes de Programari Lliure, S.L.
                        http://www.NaN-tic.com

WARNING: This program as such is intended to be used by professional
programmers who take the whole responsability of assessing all potential
consequences resulting from its eventual inadequacies and bugs
End users who are looking for a ready-to-use solution with commercial
garantees and support are strongly adviced to contract a Free Software
Service Company

This program is Free Software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package com.nantic.jasperreports;

import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.WebServer;
//import org.apache.xmlrpc.webserver.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
//import org.apache.xml.security.utils.Base64;

import net.sf.jasperreports.engine.JRRewindableDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.data.JRCsvDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;

// Exporters
import net.sf.jasperreports.engine.JRAbstractExporter;
// import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.engine.export.JRXlsExporter;
// import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.export.SimpleTextReportConfiguration;
// import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import net.sf.jasperreports.export.TextReportConfiguration;
import net.sf.jasperreports.engine.export.HtmlExporter;
// import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.export.SimpleHtmlExporterConfiguration;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.engine.export.oasis.JROdtExporter;
import net.sf.jasperreports.engine.export.oasis.JROdsExporter;

import java.text.NumberFormat;
import java.lang.Object;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.ResourceBundle;
import java.util.Hashtable;
import java.io.ByteArrayInputStream;
import java.io.*;
import java.sql.*;
import java.lang.Class;
import java.math.BigDecimal;
import java.io.InputStream;
import java.util.Locale;



public class JasperServer {
    /* Compiles the given .jrxml (inputFile) */
    public Boolean compile( String jrxmlPath ) throws java.lang.Exception {
        File jrxmlFile;
        File jasperFile;

        System.setProperty("jasper.reports.compiler.class", "com.nantic.jasperreports.I18nGroovyCompiler");

        jrxmlFile = new File( jrxmlPath );
        jasperFile = new File( jasperPath( jrxmlPath ) );
        if ( (! jasperFile.exists()) || (jrxmlFile.lastModified() > jasperFile.lastModified()) ) {
            System.out.println( "JasperServer: Compiling " + jrxmlPath ) ;
            JasperCompileManager.compileReportToFile( jrxmlPath, jasperPath( jrxmlPath ) );
            System.out.println( "JasperServer: Compiled.");
        }
        return true;
    }

    /* Returns path where bundle files are expected to be */
    public String bundlePath( String jrxmlPath ) {
        int index;
        index = jrxmlPath.lastIndexOf('.');
        if ( index != -1 )
            return jrxmlPath.substring( 0, index );
        else
            return jrxmlPath;
    }

    /* Returns the path to the .jasper file for the given .jrxml */
    public String jasperPath( String jrxmlPath ) {
        return bundlePath( jrxmlPath ) + ".jasper";
    }

    public int execute( Hashtable connectionParameters, String jrxmlPath, String outputPath, Hashtable<String, Object> parameters) throws java.lang.Exception {
        try {
            return privateExecute( connectionParameters, jrxmlPath, outputPath, parameters );
        } catch (Exception exception) {
            //exception.printStackTrace();
            throw exception;
        }
    }

    public int privateExecute( Hashtable connectionParameters, String jrxmlPath, String outputPath, Hashtable<String, Object> parameters) throws java.lang.Exception {

        JasperReport report = null;
        byte[] result = null;
        JasperPrint jasperPrint = null;
        InputStream in = null;
        int index;

        // Ensure report is compiled
        compile( jrxmlPath );

        report = (JasperReport) JRLoader.loadObjectFromFile( jasperPath( jrxmlPath ) );

        // Add SUBREPORT_DIR parameter
        index = jrxmlPath.lastIndexOf('/');
        if ( index != -1 )
            parameters.put( "SUBREPORT_DIR", jrxmlPath.substring( 0, index+1 ) );

        // Declare it outside the parameters loop because we'll use it when we will create the data source.
        Translator translator = null;

        // Fill in report parameters
        JRParameter[] reportParameters = report.getParameters();
        for( int j=0; j < reportParameters.length; j++ ){
            JRParameter jparam = reportParameters[j];
            if ( jparam.getValueClassName().equals( "java.util.Locale" ) ) {
                // REPORT_LOCALE
                if ( ! parameters.containsKey( jparam.getName() ) )
                    continue;
                String[] locales = ((String)parameters.get( jparam.getName() )).split( "_" );

                Locale locale;
                if ( locales.length == 1 )
                    locale = new Locale( locales[0] );
                else
                    locale = new Locale( locales[0], locales[1] );

                parameters.put( jparam.getName(), locale );

                // Initialize translation system
                // SQL reports will need to declare the TRANSLATOR paramter for translations to work.
                // CSV/XML based ones will not need that because we will integrate the translator
                // with the CsvMultiLanguageDataSource.
                translator = new Translator( bundlePath(jrxmlPath), locale );
                parameters.put( "TRANSLATOR", translator );

            } else if( jparam.getValueClassName().equals( "java.lang.BigDecimal" )){
                Object param = parameters.get( jparam.getName());
                parameters.put( jparam.getName(), new BigDecimal( (Double) parameters.get(jparam.getName() ) ) );
            }
        }

        if ( connectionParameters.containsKey("subreports") ) {
            Object[] subreports = (Object[]) connectionParameters.get("subreports");
            for (int i = 0; i < subreports.length; i++ ) {
                Map m = (Map)subreports[i];

                // Ensure subreport is compiled
                String jrxmlFile = (String)m.get("jrxmlFile");
                if ( ! jrxmlFile.equals( "DATASET" ) )
                    compile( (String)m.get("jrxmlFile") );

                // Create DataSource for subreport
                CsvMultiLanguageDataSource dataSource = new CsvMultiLanguageDataSource( (String)m.get("dataFile"), "utf-8", translator );
                System.out.println( "JasperServer: Adding parameter '" + ( (String)m.get("parameter") ) + "' with datasource '" + ( (String)m.get("dataFile") ) + "'" );

                parameters.put( (String)m.get("parameter"), dataSource );
            }
        }

        System.out.println( "JasperServer: Filling report..." );

        // Fill in report
        String language;
        if ( report.getQuery() == null )
            language = "";
        else
            language = report.getQuery().getLanguage();

        if( language.equalsIgnoreCase( "XPATH")  ){
            // If available, use a CSV file because it's faster to process.
            // Otherwise we'll use an XML file.
            if ( connectionParameters.containsKey("csv") ) {
                CsvMultiLanguageDataSource dataSource = new CsvMultiLanguageDataSource( (String)connectionParameters.get("csv"), "utf-8", translator );
                jasperPrint = JasperFillManager.fillReport( report, parameters, dataSource );
            } else {
                JRXmlDataSource dataSource = new JRXmlDataSource( (String)connectionParameters.get("xml"), "/data/record" );
                dataSource.setDatePattern( "yyyy-MM-dd HH:mm:ss" );
                dataSource.setNumberPattern( "#######0.##" );
                dataSource.setLocale( Locale.ENGLISH );
                jasperPrint = JasperFillManager.fillReport( report, parameters, dataSource );
            }
        } else if( language.equalsIgnoreCase( "SQL")  ) {
            Connection connection = getConnection( connectionParameters );
            jasperPrint = JasperFillManager.fillReport( report, parameters, connection );
        } else {
            JREmptyDataSource dataSource = new JREmptyDataSource();
            jasperPrint = JasperFillManager.fillReport( report, parameters, dataSource );
        }

        // Create output file
        File outputFile = new File( outputPath );
        JRAbstractExporter exporter;

        String output;
        if ( connectionParameters.containsKey( "output" ) )
            output = (String)connectionParameters.get("output");
        else
            output = "pdf";

        System.out.println( "JasperServer: Exporting..." );
        if ( output.equalsIgnoreCase( "html" ) ) {
            // exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
            // exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
            // exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
            // exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
            // exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
            exporter = new HtmlExporter();
            SimpleHtmlExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
            configuration.setHtmlHeader("");
            configuration.setBetweenPagesHtml("");
            configuration.setHtmlFooter("");
            SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();
            reportExportConfiguration.setRemoveEmptySpaceBetweenRows(true);
            exporter.setConfiguration(reportExportConfiguration);
            exporter.setConfiguration(configuration);
            exporter.setExporterOutput(new SimpleHtmlExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "csv" ) ) {
            exporter = new JRCsvExporter();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "xls" ) ) {
            // exporter = new JRXlsExporter();
            // exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
            // exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,Boolean.TRUE);
            // exporter.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET, new Integer(65000));
            // exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
            exporter = new JRXlsExporter();
            SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
            configuration.setRemoveEmptySpaceBetweenRows(true);
            configuration.setRemoveEmptySpaceBetweenColumns(true);
            configuration.setMaxRowsPerSheet(Integer.valueOf(65000));
            configuration.setDetectCellType(true);
            exporter.setConfiguration(configuration);
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "rtf" ) ) {
            exporter = new JRRtfExporter();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "odt" ) ) {
            exporter = new JROdtExporter();
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "ods" ) ) {
            exporter = new JROdsExporter();
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
        } else if ( output.equalsIgnoreCase( "txt" ) ) {
            // exporter.setParameter(TextReportConfiguration.getPageWidthInChars(), new Integer(80));
            // exporter.setParameter(TextReportConfiguration.getPageHeightInChars(), new Integer(150));
            exporter = new JRTextExporter();
            SimpleTextReportConfiguration configuration = new SimpleTextReportConfiguration();
            configuration.setPageWidthInChars(Integer.valueOf(80));
            configuration.setPageHeightInChars(Integer.valueOf(150));
            exporter.setConfiguration(configuration);
            exporter.setExporterOutput(new SimpleWriterExporterOutput(outputFile));
        } else {
            exporter = new JRPdfExporter();
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
        }
        // exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        // exporter.setParameter(JRExporterParameter.OUTPUT_FILE, outputFile);
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

        exporter.exportReport();
        System.out.println( "JasperServer: Exported." );
        return jasperPrint.getPages().size();
    }

    public static Connection getConnection( Hashtable datasource ) throws java.lang.ClassNotFoundException, java.sql.SQLException {
        Connection connection;
        Class.forName("org.postgresql.Driver");
        connection = DriverManager.getConnection( (String)datasource.get("dsn"), (String)datasource.get("user"),
        (String)datasource.get("password") );
        connection.setAutoCommit(true);
        return connection;
    }

    public static void main (String [] args) {
        try {
            int port = 8090;
            if ( args.length > 0 ) {
                port = java.lang.Integer.parseInt( args[0] );
            }
            java.net.InetAddress localhost = java.net.Inet4Address.getByName("localhost");
            System.out.println("JasperServer: Attempting to start XML-RPC Server at " + localhost.toString() + ":" + port + "...");
            WebServer server = new WebServer( port, localhost );
            XmlRpcServer xmlRpcServer = server.getXmlRpcServer();

            PropertyHandlerMapping phm = new PropertyHandlerMapping();
            phm.addHandler("Report", JasperServer.class);
            xmlRpcServer.setHandlerMapping(phm);

            server.start();
            System.out.println("JasperServer: Started successfully.");
            System.out.println("JasperServer: Accepting requests. (Halt program to stop.)");
        } catch (Exception exception) {
            System.err.println("Jasper Server: " + exception);
        }
    }
}