KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > actions > admin > ReportUploadAction


1 /*
2  * Copyright (C) 2002 Erik Swenson - eswenson@opensourcesoft.net
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.actions.admin;
21
22 import java.io.File JavaDoc;
23 import java.util.List JavaDoc;
24
25 import net.sf.jasperreports.engine.JasperCompileManager;
26 import net.sf.jasperreports.engine.design.JRJdtCompiler;
27 import net.sf.jasperreports.engine.util.JRProperties;
28
29 import org.apache.log4j.Logger;
30 import org.efs.openreports.providers.ProviderException;
31 import org.efs.openreports.providers.ReportProvider;
32 import org.efs.openreports.providers.ReportProviderAware;
33
34 import com.opensymphony.webwork.ServletActionContext;
35 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
36 import com.opensymphony.xwork.ActionSupport;
37
38 public class ReportUploadAction extends ActionSupport implements ReportProviderAware
39 {
40     protected static Logger log = Logger.getLogger(ReportUploadAction.class);
41
42     private ReportProvider reportProvider;
43
44     private List JavaDoc reportFileNames;
45
46     private String JavaDoc command;
47
48     public String JavaDoc execute()
49     {
50         try
51         {
52             if ((command != null) && (command.equals("upload")))
53             {
54                 MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
55
56                 File JavaDoc[] files = multiWrapper.getFiles("file");
57                 if (files.length > 0)
58                 {
59                     File JavaDoc reportUploadFile = files[0];
60                     if (reportUploadFile != null)
61                     {
62                         String JavaDoc reportUploadFileName = reportUploadFile.getAbsolutePath();
63                         if ((reportUploadFileName.endsWith(".xml")) || (reportUploadFileName.endsWith(".jrxml")))
64                         {
65                             try
66                             {
67                                 System.setProperty(JRProperties.COMPILER_CLASS, JRJdtCompiler.class.getName());
68                                 JRProperties.setProperty(JRProperties.COMPILER_CLASS, JRJdtCompiler.class.getName());
69                                 
70                                 JasperCompileManager.compileReportToFile(reportUploadFileName);
71                             }
72                             catch (Exception JavaDoc e)
73                             {
74                                 if (e.toString().indexOf("groovy") > -1)
75                                 {
76                                     try
77                                     {
78                                         System.setProperty(JRProperties.COMPILER_CLASS,"net.sf.jasperreports.compilers.JRGroovyCompiler" );
79                                         JRProperties.setProperty(JRProperties.COMPILER_CLASS,"net.sf.jasperreports.compilers.JRGroovyCompiler");
80                                         
81                                         JasperCompileManager.compileReportToFile(reportUploadFileName);
82                                     }
83                                     catch(Exception JavaDoc ex)
84                                     {
85                                         log.error("Failed to compile report: " + reportUploadFileName, e);
86                                         addActionError("Failed to compile report: " + e.toString());
87                                     }
88                                 }
89                                 else
90                                 {
91                                     log.error("Failed to compile report: " + reportUploadFileName, e);
92                                     addActionError("Failed to compile report: " + e.toString());
93                                 }
94                             }
95                             
96                             
97                         }
98                     }
99                 }
100             }
101
102             reportFileNames = reportProvider.getReportFileNames();
103         }
104         catch (ProviderException pe)
105         {
106             addActionError(pe.getMessage());
107         }
108
109         return SUCCESS;
110     }
111
112     public List JavaDoc getReportFileNames()
113     {
114         return reportFileNames;
115     }
116
117     public void setReportProvider(ReportProvider reportProvider)
118     {
119         this.reportProvider = reportProvider;
120     }
121
122     public String JavaDoc getCommand()
123     {
124         return command;
125     }
126
127     public void setCommand(String JavaDoc command)
128     {
129         this.command = command;
130     }
131
132 }
Popular Tags