KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > actions > ReportScheduleAction


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 under the terms of the
5  * GNU General Public License as published by the Free Software Foundation; either version 2 of the
6  * License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
10  * the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along with this program; if
13  * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
14  * 02111-1307, USA.
15  *
16  */

17
18 package org.efs.openreports.actions;
19
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.efs.openreports.ORStatics;
26 import org.efs.openreports.objects.Report;
27 import org.efs.openreports.objects.ReportAlert;
28 import org.efs.openreports.objects.ReportSchedule;
29 import org.efs.openreports.objects.ReportUser;
30 import org.efs.openreports.objects.ReportUserAlert;
31 import org.efs.openreports.providers.AlertProvider;
32 import org.efs.openreports.providers.AlertProviderAware;
33 import org.efs.openreports.providers.DateProvider;
34 import org.efs.openreports.providers.DateProviderAware;
35 import org.efs.openreports.providers.SchedulerProvider;
36 import org.efs.openreports.providers.SchedulerProviderAware;
37 import org.efs.openreports.providers.UserProvider;
38 import org.efs.openreports.providers.UserProviderAware;
39 import org.efs.openreports.util.LocalStrings;
40
41 import com.opensymphony.xwork.ActionContext;
42 import com.opensymphony.xwork.ActionSupport;
43
44 public class ReportScheduleAction extends ActionSupport implements SchedulerProviderAware, DateProviderAware, AlertProviderAware, UserProviderAware
45 {
46     protected static Logger log = Logger.getLogger(ReportScheduleAction.class);
47
48     private boolean submitScheduledReport;
49     private Report report;
50
51     private String JavaDoc scheduleName;
52     private String JavaDoc description;
53     private int userId = Integer.MIN_VALUE;
54
55     private int scheduleType;
56     private String JavaDoc startDate;
57     private String JavaDoc startHour;
58     private String JavaDoc startMinute;
59     private String JavaDoc startAmPm;
60     private String JavaDoc recipients;
61     private int hours;
62     private String JavaDoc cron;
63     
64     private int alertId = Integer.MIN_VALUE;
65     private int alertLimit;
66     private String JavaDoc alertOperator;
67
68     private SchedulerProvider schedulerProvider;
69     private DateProvider dateProvider;
70     private AlertProvider alertProvider;
71     private UserProvider userProvider;
72
73     public String JavaDoc execute()
74     {
75         ReportUser user = null;
76         
77         if (userId >= 0)
78         {
79             try
80             {
81                 user = userProvider.getUser(new Integer JavaDoc(userId));
82             }
83             catch (Exception JavaDoc e)
84             {
85                 addActionError(e.getMessage());
86                 return INPUT;
87             }
88         }
89         else
90         {
91             user = (ReportUser) ActionContext.getContext().getSession().get(
92                 ORStatics.REPORT_USER);
93         }
94         
95         if (user.getEmail() == null || user.getEmail().length() < 1)
96         {
97             addActionError(LocalStrings.getString(LocalStrings.ERROR_EMAILADDRESS_REQUIRED));
98             return INPUT;
99         }
100
101         if (recipients == null || recipients.length() < 1)
102         {
103             recipients = user.getEmail();
104         }
105         
106         if (!submitScheduledReport)
107         {
108             if (scheduleName != null && scheduleName.length() > 0)
109             {
110                 try
111                 {
112                     ReportSchedule reportSchedule = schedulerProvider.getScheduledReport(user, scheduleName);
113
114                     report = reportSchedule.getReport();
115                     scheduleType = reportSchedule.getScheduleType();
116                     startDate = dateProvider.formatDate(reportSchedule.getStartDate());
117                     startHour = reportSchedule.getStartHour();
118                     startMinute = reportSchedule.getStartMinute();
119                     startAmPm = reportSchedule.getStartAmPm();
120                     recipients = reportSchedule.getRecipients();
121                     description = reportSchedule.getScheduleDescription();
122                     hours = reportSchedule.getHours();
123                     cron = reportSchedule.getCronExpression();
124                     
125                     if (reportSchedule.getAlert() != null)
126                     {
127                         ReportUserAlert userAlert = reportSchedule.getAlert();
128                         
129                         alertId = userAlert.getAlert().getId().intValue();
130                         alertLimit = userAlert.getLimit();
131                         alertOperator = userAlert.getOperator();
132                     }
133                 }
134                 catch (Exception JavaDoc e)
135                 {
136                     addActionError(e.getMessage());
137                     return INPUT;
138                 }
139             }
140             else
141             {
142                 report = (Report) ActionContext.getContext().getSession().get(ORStatics.REPORT);
143                 startDate = dateProvider.formatDate(new Date JavaDoc());
144             }
145
146             return INPUT;
147         }
148
149         if (startDate == null || startDate.length() < 1 || startHour == null || startHour.length() < 1
150                 || startMinute == null || startMinute.length() < 1 || startAmPm == null
151                 || startAmPm.length() < 1)
152         {
153             addActionError(LocalStrings.getString(LocalStrings.ERROR_DATEANDTIME_REQUIRED));
154             return INPUT;
155         }
156         
157         int hour = Integer.parseInt(startHour);
158         int minute = Integer.parseInt(startMinute);
159         if (hour > 12 || hour < 1 || minute < 0 || minute > 59)
160         {
161             addActionError(LocalStrings.getString(LocalStrings.ERROR_DATEANDTIME_INVALID));
162             return INPUT;
163         }
164
165         ReportSchedule reportSchedule = new ReportSchedule();
166         
167         try
168         {
169             if (scheduleName != null && scheduleName.length() > 0)
170             {
171                 reportSchedule = schedulerProvider.getScheduledReport(user, scheduleName);
172             }
173             else
174             {
175                 report = (Report) ActionContext.getContext().getSession().get(ORStatics.REPORT);
176
177                 Map JavaDoc reportParameters = (Map JavaDoc) ActionContext.getContext().getSession().get(
178                         ORStatics.REPORT_PARAMETERS);
179
180                 int exportType = Integer.parseInt((String JavaDoc) ActionContext.getContext().getSession().get(
181                         ORStatics.EXPORT_TYPE));
182
183                 reportSchedule.setReport(report);
184                 reportSchedule.setUser(user);
185                 reportSchedule.setReportParameters(reportParameters);
186                 reportSchedule.setExportType(exportType);
187                 reportSchedule.setScheduleName(report.getId() + "|" + new Date JavaDoc().getTime());
188             }
189
190             reportSchedule.setScheduleType(scheduleType);
191             reportSchedule.setStartDate(dateProvider.parseDate(startDate));
192             reportSchedule.setStartHour(startHour);
193             reportSchedule.setStartMinute(startMinute);
194             reportSchedule.setStartAmPm(startAmPm);
195             reportSchedule.setRecipients(recipients);
196             reportSchedule.setScheduleDescription(description);
197             reportSchedule.setHours(hours);
198             reportSchedule.setCronExpression(cron);
199             
200             if (alertId != -1)
201             {
202                 ReportAlert alert = alertProvider.getReportAlert(new Integer JavaDoc(alertId));
203                 
204                 ReportUserAlert userAlert = new ReportUserAlert();
205                 userAlert.setAlert(alert);
206                 userAlert.setUser(user);
207                 userAlert.setLimit(alertLimit);
208                 userAlert.setOperator(alertOperator);
209                                             
210                 reportSchedule.setAlert(userAlert);
211             }
212             else
213             {
214                 reportSchedule.setAlert(null);
215             }
216
217             if (scheduleName != null && scheduleName.length() > 0)
218             {
219                 // in order to update a schedule report, original reportSchedule
220
// is deleted and new a one is created.
221
schedulerProvider.deleteScheduledReport(user, scheduleName);
222             }
223
224             schedulerProvider.scheduleReport(reportSchedule);
225         }
226         catch (Exception JavaDoc e)
227         {
228             addActionError(e.getMessage());
229             return INPUT;
230         }
231
232         addActionError(LocalStrings.getString(LocalStrings.MESSAGE_SCHEDULE_SUCCESSFUL));
233
234         return SUCCESS;
235     }
236
237     public Report getReport()
238     {
239         return report;
240     }
241
242     public void setReport(Report report)
243     {
244         this.report = report;
245     }
246
247     public void setSchedulerProvider(SchedulerProvider schedulerProvider)
248     {
249         this.schedulerProvider = schedulerProvider;
250     }
251
252     public void setSubmitScheduledReport(String JavaDoc submitScheduledReport)
253     {
254         if (submitScheduledReport != null) this.submitScheduledReport = true;
255     }
256
257     public int getScheduleType()
258     {
259         return scheduleType;
260     }
261
262     public void setScheduleType(int scheduleType)
263     {
264         this.scheduleType = scheduleType;
265     }
266
267     public String JavaDoc getStartAmPm()
268     {
269         return startAmPm;
270     }
271
272     public void setStartAmPm(String JavaDoc startAmPm)
273     {
274         this.startAmPm = startAmPm;
275     }
276
277     public String JavaDoc getStartDate()
278     {
279         return startDate;
280     }
281
282     public void setStartDate(String JavaDoc startDate)
283     {
284         this.startDate = startDate;
285     }
286
287     public String JavaDoc getStartHour()
288     {
289         return startHour;
290     }
291
292     public void setStartHour(String JavaDoc startHour)
293     {
294         this.startHour = startHour;
295     }
296
297     public String JavaDoc getStartMinute()
298     {
299         return startMinute;
300     }
301
302     public void setStartMinute(String JavaDoc startMinute)
303     {
304         this.startMinute = startMinute;
305     }
306
307     public void setDateProvider(DateProvider dateProvider)
308     {
309         this.dateProvider = dateProvider;
310     }
311
312     public String JavaDoc getRecipients()
313     {
314         return recipients;
315     }
316
317     public void setRecipients(String JavaDoc recipients)
318     {
319         this.recipients = recipients;
320     }
321
322     public String JavaDoc getScheduleName()
323     {
324         return scheduleName;
325     }
326
327     public void setScheduleName(String JavaDoc scheduleName)
328     {
329         this.scheduleName = scheduleName;
330     }
331     
332     public String JavaDoc getDateFormat()
333     {
334         return dateProvider.getDateFormat().toPattern();
335     }
336     
337     public int getAlertId()
338     {
339         return alertId;
340     }
341
342     public void setAlertId(int alertId)
343     {
344         this.alertId = alertId;
345     }
346
347     public List JavaDoc getAlerts()
348     {
349         try
350         {
351             return alertProvider.getReportAlerts();
352         }
353         catch (Exception JavaDoc e)
354         {
355             addActionError(e.getMessage());
356             return null;
357         }
358     }
359
360     public void setAlertProvider(AlertProvider alertProvider)
361     {
362         this.alertProvider = alertProvider;
363     }
364
365     public int getAlertLimit()
366     {
367         return alertLimit;
368     }
369
370     public void setAlertLimit(int alertLimit)
371     {
372         this.alertLimit = alertLimit;
373     }
374
375     public String JavaDoc getAlertOperator()
376     {
377         return alertOperator;
378     }
379
380     public void setAlertOperator(String JavaDoc alertOperator)
381     {
382         this.alertOperator = alertOperator;
383     }
384     
385     public String JavaDoc[] getOperators()
386     {
387         return new String JavaDoc[] {ReportAlert.OPERATOR_EQUAL, ReportAlert.OPERATOR_GREATER, ReportAlert.OPERATOR_LESS};
388     }
389
390     public String JavaDoc getDescription()
391     {
392         return description;
393     }
394
395     public void setDescription(String JavaDoc description)
396     {
397         this.description = description;
398     }
399
400     public void setUserProvider(UserProvider userProvider)
401     {
402         this.userProvider = userProvider;
403     }
404
405     public int getUserId()
406     {
407         return userId;
408     }
409
410     public void setUserId(int userId)
411     {
412         this.userId = userId;
413     }
414
415     public String JavaDoc getCron()
416     {
417         return cron;
418     }
419
420     public void setCron(String JavaDoc cron)
421     {
422         this.cron = cron;
423     }
424
425     public int getHours()
426     {
427         return hours;
428     }
429
430     public void setHours(int hours)
431     {
432         this.hours = hours;
433     }
434 }
Popular Tags