KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2002 Erik Swenson - erik@oreports.com
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.util.*;
23
24 import com.opensymphony.xwork.ActionContext;
25 import com.opensymphony.xwork.ActionSupport;
26
27 import org.apache.log4j.Logger;
28 import org.efs.openreports.ORStatics;
29 import org.efs.openreports.objects.*;
30 import org.efs.openreports.providers.*;
31 import org.efs.openreports.util.LocalStrings;
32 import org.efs.openreports.util.ORUtil;
33
34 public class EditReportAction
35     extends ActionSupport
36     implements ReportProviderAware, DataSourceProviderAware, ChartProviderAware, ParameterProviderAware
37 {
38     protected static Logger log = Logger.getLogger(EditReportAction.class);
39
40     private String JavaDoc command;
41     
42     private boolean submitOk;
43     private boolean submitValidate;
44
45     private int id;
46     private String JavaDoc name;
47     private String JavaDoc description;
48     private String JavaDoc file;
49     private String JavaDoc query;
50     private int dataSourceId;
51     private int reportChartId;
52
53     private boolean pdfExportEnabled;
54     private boolean htmlExportEnabled;
55     private boolean csvExportEnabled;
56     private boolean xlsExportEnabled;
57     private boolean rtfExportEnabled;
58     private boolean textExportEnabled;
59     private boolean excelExportEnabled;
60     private boolean virtual;
61     private boolean hidden;
62     
63     private Report report;
64
65     private ReportProvider reportProvider;
66     private DataSourceProvider dataSourceProvider;
67     private ChartProvider chartProvider;
68     private ParameterProvider parameterProvider;
69     
70     private ReportParameterValue[] parameterValues;
71
72     public String JavaDoc execute()
73     {
74         try
75         {
76             if (command.equals("edit"))
77             {
78                 report = reportProvider.getReport(new Integer JavaDoc(id));
79             }
80             else
81             {
82                 report = new Report();
83             }
84
85             if (command.equals("edit") && !submitOk && !submitValidate)
86             {
87                 name = report.getName();
88                 description = report.getDescription();
89                 file = report.getFile();
90                 query = report.getQuery();
91                 id = report.getId().intValue();
92                 pdfExportEnabled = report.isPdfExportEnabled();
93                 csvExportEnabled = report.isCsvExportEnabled();
94                 htmlExportEnabled = report.isHtmlExportEnabled();
95                 xlsExportEnabled = report.isXlsExportEnabled();
96                 rtfExportEnabled = report.isRtfExportEnabled();
97                 textExportEnabled = report.isTextExportEnabled();
98                 excelExportEnabled = report.isExcelExportEnabled();
99                 virtual = report.isVirtualizationEnabled();
100                 hidden = report.isHidden();
101                 if (report.getDataSource() != null)
102                 {
103                     dataSourceId = report.getDataSource().getId().intValue();
104                 }
105                 if (report.getReportChart() != null)
106                 {
107                     reportChartId = report.getReportChart().getId().intValue();
108                 }
109             }
110
111             if (!submitOk && !submitValidate)
112                 return INPUT;
113             
114             if (name == null || name.trim().length() < 1)
115             {
116                 addActionError(LocalStrings.getString(LocalStrings.ERROR_REPORT_INVALID));
117                 return INPUT;
118             }
119
120             report.setName(name);
121             report.setDescription(description);
122             report.setFile(file);
123             report.setQuery(query);
124             report.setCsvExportEnabled(csvExportEnabled);
125             report.setHtmlExportEnabled(htmlExportEnabled);
126             report.setPdfExportEnabled(pdfExportEnabled);
127             report.setXlsExportEnabled(xlsExportEnabled);
128             report.setRtfExportEnabled(new Boolean JavaDoc(rtfExportEnabled));
129             report.setTextExportEnabled(new Boolean JavaDoc(textExportEnabled));
130             report.setExcelExportEnabled(new Boolean JavaDoc(excelExportEnabled));
131             report.setVirtualizationEnabled(new Boolean JavaDoc(virtual));
132             report.setHidden(new Boolean JavaDoc(hidden));
133             
134             if ((query == null || query.trim().length() < 1)
135                 && !csvExportEnabled
136                 && !htmlExportEnabled
137                 && !pdfExportEnabled
138                 && !xlsExportEnabled
139                 && !rtfExportEnabled
140                 && !textExportEnabled
141                 && !excelExportEnabled)
142             {
143                 report.setPdfExportEnabled(true);
144             }
145
146             if (dataSourceId != -1)
147             {
148                 report.setDataSource(
149                     dataSourceProvider.getDataSource(
150                         new Integer JavaDoc(dataSourceId)));
151             }
152             else
153             {
154                 report.setDataSource(null);
155             }
156
157             if (reportChartId != -1)
158             {
159                 report.setReportChart(
160                     chartProvider.getReportChart(new Integer JavaDoc(reportChartId)));
161             }
162             else
163             {
164                 report.setReportChart(null);
165             }
166
167             if (submitValidate)
168             {
169                 if (report.getQuery() == null || report.getQuery().length() < 1) return INPUT;
170                 
171                 Map map = new HashMap();
172                 if (query.toUpperCase().indexOf("$P") > -1)
173                 {
174                     ReportUser reportUser = (ReportUser) ActionContext.getContext().getSession().get(ORStatics.REPORT_USER);
175                     map = ORUtil.buildQueryParameterMap(reportUser, query, parameterProvider);
176                 }
177                 
178                 //FIXME validate query as parameter...
179
ReportParameter reportParameter = new ReportParameter();
180                 reportParameter.setData(query);
181                 reportParameter.setType(ReportParameter.QUERY_PARAM);
182                 reportParameter.setDataSource(report.getDataSource());
183                 reportParameter.setClassName("java.lang.String");
184                 
185                 parameterValues = parameterProvider.getParamValues(reportParameter, map);
186
187                 return INPUT;
188             }
189
190             if (command.equals("edit"))
191             {
192                 reportProvider.updateReport(report);
193             }
194
195             if (command.equals("add"))
196             {
197                 reportProvider.insertReport(report);
198             }
199             
200             return SUCCESS;
201         }
202         catch (Exception JavaDoc e)
203         {
204             addActionError(e.getMessage());
205             return INPUT;
206         }
207     }
208
209     public String JavaDoc getCommand()
210     {
211         return command;
212     }
213
214     public void setCommand(String JavaDoc command)
215     {
216         this.command = command;
217     }
218
219     public int getDataSourceId()
220     {
221         return dataSourceId;
222     }
223
224     public String JavaDoc getDescription()
225     {
226         return description;
227     }
228
229     public String JavaDoc getFile()
230     {
231         return file;
232     }
233
234     public String JavaDoc getName()
235     {
236         return name;
237     }
238
239     public void setDataSourceId(int dataSourceId)
240     {
241         this.dataSourceId = dataSourceId;
242     }
243
244     public void setDescription(String JavaDoc description)
245     {
246         this.description = description;
247     }
248
249     public void setFile(String JavaDoc file)
250     {
251         this.file = file;
252     }
253
254     public void setName(String JavaDoc name)
255     {
256         this.name = name;
257     }
258
259     public List getDataSources()
260     {
261         try
262         {
263             return dataSourceProvider.getDataSources();
264         }
265         catch (Exception JavaDoc e)
266         {
267             addActionError(e.toString());
268             return null;
269         }
270     }
271
272     public List getReportFileNames()
273     {
274         try
275         {
276             return reportProvider.getReportFileNames();
277         }
278         catch (Exception JavaDoc e)
279         {
280             addActionError(e.toString());
281             return null;
282         }
283     }
284
285     public int getId()
286     {
287         return id;
288     }
289
290     public void setId(int id)
291     {
292         this.id = id;
293     }
294
295     public List getParametersInReport()
296     {
297         if (report == null || report.getParameters() == null)
298             return null;
299
300         List list = report.getParameters();
301         Collections.sort(list);
302
303         return list;
304     }
305
306     public List getReportParameters()
307     {
308         try
309         {
310             return parameterProvider.getAvailableParameters(report);
311         }
312         catch (Exception JavaDoc e)
313         {
314             addActionError(e.toString());
315             return null;
316         }
317     }
318     
319     public Report getReport()
320     {
321         return report;
322     }
323     
324     public void setDataSourceProvider(DataSourceProvider dataSourceProvider)
325     {
326         this.dataSourceProvider = dataSourceProvider;
327     }
328
329     public void setReportProvider(ReportProvider reportProvider)
330     {
331         this.reportProvider = reportProvider;
332     }
333
334     public int getReportChartId()
335     {
336         return reportChartId;
337     }
338
339     public void setReportChartId(int reportChartId)
340     {
341         this.reportChartId = reportChartId;
342     }
343
344     public List getReportCharts()
345     {
346         try
347         {
348             return chartProvider.getReportCharts();
349         }
350         catch (Exception JavaDoc e)
351         {
352             addActionError(e.toString());
353             return null;
354         }
355     }
356
357     public void setChartProvider(ChartProvider chartProvider)
358     {
359         this.chartProvider = chartProvider;
360     }
361
362     public boolean isCsvExportEnabled()
363     {
364         return csvExportEnabled;
365     }
366
367     public void setCsvExportEnabled(boolean csvExportEnabled)
368     {
369         this.csvExportEnabled = csvExportEnabled;
370     }
371
372     public boolean isHtmlExportEnabled()
373     {
374         return htmlExportEnabled;
375     }
376
377     public void setHtmlExportEnabled(boolean htmlExportEnabled)
378     {
379         this.htmlExportEnabled = htmlExportEnabled;
380     }
381
382     public boolean isPdfExportEnabled()
383     {
384         return pdfExportEnabled;
385     }
386
387     public void setPdfExportEnabled(boolean pdfExportEnabled)
388     {
389         this.pdfExportEnabled = pdfExportEnabled;
390     }
391
392     public boolean isXlsExportEnabled()
393     {
394         return xlsExportEnabled;
395     }
396
397     public void setXlsExportEnabled(boolean xlsExportEnabled)
398     {
399         this.xlsExportEnabled = xlsExportEnabled;
400     }
401
402     public String JavaDoc getQuery()
403     {
404         return query;
405     }
406
407     public void setQuery(String JavaDoc query)
408     {
409         this.query = query;
410     }
411     
412     public void setSubmitOk(String JavaDoc submitOk)
413     {
414         if (submitOk != null) this.submitOk = true;
415     }
416     
417     public void setSubmitValidate(String JavaDoc submitValidate)
418     {
419         if (submitValidate != null) this.submitValidate = true;
420     }
421
422     public void setParameterProvider(ParameterProvider parameterProvider)
423     {
424         this.parameterProvider = parameterProvider;
425     }
426     
427     public ReportParameterValue[] getParameterValues()
428     {
429         return parameterValues;
430     }
431
432     public boolean isExcelExportEnabled()
433     {
434         return excelExportEnabled;
435     }
436
437     public void setExcelExportEnabled(boolean excelExportEnabled)
438     {
439         this.excelExportEnabled = excelExportEnabled;
440     }
441
442     public boolean isRtfExportEnabled()
443     {
444         return rtfExportEnabled;
445     }
446
447     public void setRtfExportEnabled(boolean rtfExportEnabled)
448     {
449         this.rtfExportEnabled = rtfExportEnabled;
450     }
451
452     public boolean isTextExportEnabled()
453     {
454         return textExportEnabled;
455     }
456
457     public void setTextExportEnabled(boolean textExportEnabled)
458     {
459         this.textExportEnabled = textExportEnabled;
460     }
461     
462     public boolean isHidden()
463     {
464         return hidden;
465     }
466
467     public void setHidden(boolean hidden)
468     {
469         this.hidden = hidden;
470     }
471
472     public boolean isVirtual()
473     {
474         return virtual;
475     }
476
477     public void setVirtual(boolean virtual)
478     {
479         this.virtual = virtual;
480     }
481 }
Popular Tags