KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > calendar > struts > EventForm


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: EventForm.java,v $
31  * Revision 1.5 2005/04/30 13:04:13 colinmacleod
32  * Fixes reverting id type from String to Integer.
33  *
34  * Revision 1.4 2005/04/10 20:43:44 colinmacleod
35  * Added new themes.
36  * Changed id type to String.
37  * Changed i tag to em and b tag to strong.
38  * Improved PicoContainerFactory with NanoContainer scripts.
39  *
40  * Revision 1.3 2005/04/09 17:19:18 colinmacleod
41  * Changed copyright text to GPL v2 explicitly.
42  *
43  * Revision 1.2 2005/03/16 18:36:15 colinmacleod
44  * Fixed clear button and pop-up closing.
45  *
46  * Revision 1.1.1.1 2005/03/10 17:47:43 colinmacleod
47  * Restructured ivata op around Hibernate/PicoContainer.
48  * Renamed ivata groupware.
49  *
50  * Revision 1.8 2004/12/23 21:01:27 colinmacleod
51  * Updated Struts to v1.2.4.
52  * Changed base classes to use ivata masks.
53  *
54  * Revision 1.7 2004/11/12 18:19:14 colinmacleod
55  * Change action and form classes to extend MaskAction, MaskForm respectively.
56  *
57  * Revision 1.6 2004/11/12 16:08:11 colinmacleod
58  * Removed dependencies on SSLEXT.
59  * Moved Persistence classes to ivata masks.
60  *
61  * Revision 1.5 2004/07/13 19:42:14 colinmacleod
62  * Moved project to POJOs from EJBs.
63  * Applied PicoContainer to services layer (replacing session EJBs).
64  * Applied Hibernate to persistence layer (replacing entity EJBs).
65  *
66  * Revision 1.4 2004/03/21 21:16:22 colinmacleod
67  * Shortened name to ivata op.
68  *
69  * Revision 1.3 2004/02/10 15:14:51 janboros
70  * fixing bug
71  *
72  * Revision 1.2 2004/02/01 22:07:29 colinmacleod
73  * Added full names to author tags
74  *
75  * Revision 1.1.1.1 2004/01/27 20:58:21 colinmacleod
76  * Moved ivata openportal to SourceForge..
77  *
78  * Revision 1.3 2003/11/13 16:07:26 jano
79  * commitng everything to CVS
80  * can deploy and application is ruuning, can login into
81  *
82  * Revision 1.2 2003/10/15 13:57:23 colin
83  * fixing for XDoclet
84  *
85  * Revision 1.8 2003/05/02 10:30:03 peter
86  * fixed validation of times when dates the equal
87  *
88  * Revision 1.7 2003/04/14 07:10:15 peter
89  * fixed helpKey logic
90  *
91  * Revision 1.6 2003/03/21 09:35:38 peter
92  * fixed check for event type before validation
93  *
94  * Revision 1.5 2003/02/28 09:36:51 jano
95  * RuntimeException(e) -> IntrnetRuntimeException
96  *
97  * Revision 1.4 2003/02/27 12:33:46 peter
98  * fixed reset - points and minutes are vectors
99  *
100  * Revision 1.3 2003/02/26 17:12:37 peter
101  * fixed the countryCode validation, when set to empty string, must be null
102  *
103  * Revision 1.2 2003/02/25 17:27:37 peter
104  * fixed validation
105  *
106  * Revision 1.1 2003/02/24 19:09:21 colin
107  * moved to business
108  *
109  * Revision 1.5 2003/02/20 20:25:37 colin
110  * improved validation by adding ValidationField and ValidationException
111  *
112  * Revision 1.4 2003/02/20 16:20:45 peter
113  * date/time formats changed to input ones
114  *
115  * Revision 1.3 2003/02/20 13:00:26 peter
116  * 24H time formats changed to simple ones
117  *
118  * Revision 1.2 2003/02/13 08:45:42 colin
119  * conversion to Struts/popups
120  *
121  * Revision 1.1 2003/02/04 17:50:17 colin
122  * first version in CVS
123  *
124  * Revision 1.1 2003/02/04 17:41:16 colin
125  * first version in CVS
126  * -----------------------------------------------------------------------------
127  */

128 package com.ivata.groupware.business.calendar.struts;
129
130 import java.text.ParseException JavaDoc;
131 import java.util.Arrays JavaDoc;
132 import java.util.GregorianCalendar JavaDoc;
133 import java.util.Locale JavaDoc;
134
135 import javax.naming.OperationNotSupportedException JavaDoc;
136 import javax.servlet.http.HttpServletRequest JavaDoc;
137 import javax.servlet.http.HttpSession JavaDoc;
138
139 import org.apache.struts.Globals;
140 import org.apache.struts.action.ActionMapping;
141
142 import com.ivata.groupware.admin.security.server.SecuritySession;
143 import com.ivata.groupware.business.calendar.Calendar;
144 import com.ivata.groupware.business.calendar.event.EventDO;
145 import com.ivata.groupware.business.calendar.event.meeting.MeetingDO;
146 import com.ivata.groupware.business.calendar.event.publicholiday.PublicHolidayDO;
147 import com.ivata.groupware.util.SettingDateFormatter;
148 import com.ivata.mask.Mask;
149 import com.ivata.mask.MaskFactory;
150 import com.ivata.mask.util.StringHandling;
151 import com.ivata.mask.util.SystemException;
152 import com.ivata.mask.validation.ValidationError;
153 import com.ivata.mask.validation.ValidationErrors;
154 import com.ivata.mask.web.format.DateFormatterConstants;
155 import com.ivata.mask.web.format.DateFormatterException;
156 import com.ivata.mask.web.struts.DialogForm;
157
158
159 /**
160  * <p>This form is wrapper for <code>EventDO</code>. It is used in
161  * <code>event.jsp</code>.</p>
162  *
163  * @since 2003-01-31
164  * @author Colin MacLeod
165  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
166  * @version $Revision: 1.5 $
167  *
168  */

169 public class EventForm extends DialogForm {
170     /**
171      * <p>If not <code>null</code> and not empty, specifies a new, empty
172      * category should be added.</p>
173      */

174     private String JavaDoc addCategory = "";
175     /**
176      * <p>If this event is a meeting, this is a semicolon (;) deliminated
177      * string of all attendee identifers.</p>
178      */

179     private String JavaDoc attendees = "";
180     private Calendar JavaDoc calendar;
181     private SettingDateFormatter dateFormatter;
182     /**
183      * <p>Instance of event data object, containing all values
184      * of event being submitted.</p>
185      */

186     private EventDO event;
187     /**
188      * <p>Stores the number of the active tab page.</p>
189      */

190     private Integer JavaDoc eventTab_activeTab = new Integer JavaDoc(0);
191     /**
192      * <p>Specifies just the date part of the end date/time.</p>
193      */

194     private String JavaDoc finishDate = "";
195     /**
196      * <p>Specifies just the time part of the end date/time.</p>
197      */

198     private String JavaDoc finishTime = "";
199     /**
200      * <p>Specifies just the date part of the start date/time.</p>
201      */

202     private String JavaDoc startDate = "";
203     /**
204      * <p>Specifies just the time part of the start date/time.</p>
205      */

206     private String JavaDoc startTime = "";
207     /**
208      * <p>Page to currently display in the tab control.</p>
209      */

210     private String JavaDoc tabPage = "/calendar/eventDetails.jsp";
211     /**
212      * <p>Key used to access localized title string.</p>
213      */

214     private String JavaDoc titleKey = "event.title.new";
215     /**
216      * <p>
217      * Defines the base class of all objects in the value object list.
218      * </p>
219      */

220     private Class JavaDoc baseClass;
221     /**
222      * <p>
223      * Mask containing all the field definitions for this list.
224      * </p>
225      */

226     private Mask mask;
227     /**
228      * @param maskParam Refer to {@link DialogForm#DialogForm}.
229      * @param baseClassParam Refer to {@link DialogForm#DialogForm}.
230      * @param settings ivata settings implementation - used in validation.
231      */

232     public EventForm(final Calendar JavaDoc calendar,
233             final SettingDateFormatter dateFormatter,
234             MaskFactory maskFactory) {
235         this.calendar = calendar;
236         this.dateFormatter = dateFormatter;
237         mask = maskFactory.getMask(EventDO.class);
238     }
239
240     /**
241      * <p>
242      * Return all form state to initial values.
243      * </p>
244      *
245      * @see com.ivata.mask.web.struts.MaskForm#clear()
246      */

247     protected void clear() throws OperationNotSupportedException JavaDoc {
248         super.clear();
249         addCategory = "";
250         attendees = "";
251         event = new EventDO();
252         eventTab_activeTab = new Integer JavaDoc(0);
253         finishDate = "";
254         finishTime = "";
255         startDate = "";
256         startTime = "";
257         tabPage = "/calendar/eventDetails.jsp";
258         titleKey = "event.title.new";
259     }
260
261     /**
262      * <p>If not <code>null</code> and not empty, specifies a new, empty
263      * category should be added.</p>
264      *
265      * @return the current value of addCategory.
266      */

267     public final String JavaDoc getAddCategory() {
268         return addCategory;
269     }
270
271     /**
272      * <p>If this event is a meeting, this is a semicolon (;) deliminated
273      * string of all attendee identifers.</p>
274      *
275      * @return the current value of attendees.
276      */

277     public final String JavaDoc getAttendees() {
278         return attendees;
279     }
280
281     /**
282      * <p>Get the values of the event this form refers to as a dependent
283      * value object.</p>
284      *
285      * @return event values this form refers to.
286      *
287      *
288      */

289     public final EventDO getEvent() {
290         return event;
291     }
292
293     /**
294      * <p>Stores the number of the active tab page.</p>
295      *
296      * @return the current value of eventTab_activeTab.
297      */

298     public final Integer JavaDoc getEventTab_activeTab() {
299         return eventTab_activeTab;
300     }
301
302     /**
303      * <p>Specifies just the date part of the end date/time.</p>
304      *
305      * @return the current value of finishDate.
306      */

307     public final String JavaDoc getFinishDate() {
308         return finishDate;
309     }
310
311     /**
312      * <p>Specifies just the time part of the end date/time.</p>
313      *
314      * @return the current value of finishTime.
315      */

316     public final String JavaDoc getFinishTime() {
317         return finishTime;
318     }
319
320     /**
321      * <p>Specifies just the date part of the start date/time.</p>
322      *
323      * @return the current value of startDate.
324      */

325     public final String JavaDoc getStartDate() {
326         return startDate;
327     }
328
329     /**
330      * <p>Specifies just the time part of the start date/time.</p>
331      *
332      * @return the current value of startTime.
333      */

334     public final String JavaDoc getStartTime() {
335         return startTime;
336     }
337
338     /**
339      * <p>Page to currently display in the tab control.</p>
340      *
341      * @return the current value of tabPage.
342      */

343     public final String JavaDoc getTabPage() {
344         return tabPage;
345     }
346
347     /**
348      * <p>Key used to access localized title string.</p>
349      *
350      * @return the current value of titleKey.
351      */

352     public final String JavaDoc getTitleKey() {
353         return titleKey;
354     }
355
356     /**
357      * <p>Find out if this is a meeting.</p>
358      *
359      * @return <code>true</code> if it is a meeting, otherwise
360      * <code>false</code>.
361      */

362     public boolean isMeeting() {
363         return event instanceof MeetingDO;
364     }
365
366     /**
367      * <p>Find out if this is an event rather than a public holiday.</p>
368      *
369      * @return <code>true</code> if it is an event, otherwise
370      * <code>false</code> if it is a public holiday or meeting.
371      */

372     public boolean isPlainEvent() {
373         return !((event instanceof MeetingDO) || (event instanceof PublicHolidayDO));
374     }
375
376     /**
377      * <p>Find out if this is a public holiday.</p>
378      *
379      * @return <code>true</code> if it is a public holiday, otherwise
380      * <code>false</code>.
381      */

382     public boolean isPublicHoliday() {
383         return event instanceof PublicHolidayDO;
384     }
385
386
387     /**
388      * <p>Reset all bean properties to their default state. This method
389      * is called before the properties are repopulated by the controller
390      * servlet.</p>
391      *
392      * @param calendar valid calendar remote instance.
393      * @param request The servlet request we are processing.
394      * @param session The servlet session we are processing.
395      *
396      *
397      */

398     public void reset(final ActionMapping mapping,
399             final HttpServletRequest JavaDoc request) {
400         // if there is no event yet, nothing to reset
401
if (event == null) {
402             return;
403         }
404
405         MeetingDO meeting = null;
406         if (event instanceof MeetingDO) {
407             meeting = (MeetingDO) event;
408         }
409
410         // what we reset depends on the tab page
411
// meeting attendees affects the chairperson and the list of attendees
412
if (tabPage.equals("/calendar/meetingPeople.jsp")) {
413             attendees = "";
414             meeting.setChairPerson(null);
415
416         // meeting agenda affects the list of agenda points
417
} else if (tabPage.equals("/calendar/meetingAgenda.jsp")) {
418             // TODO: clear agenda points and categories here
419

420         // standard event
421
} else {
422             event.setSubject("");
423             startDate = "";
424             startTime = "";
425             finishDate = "";
426             finishTime = "";
427             event.setAllDayEvent(false);
428             event.setDescription("");
429             if (meeting != null) {
430                 meeting.setLocation("");
431             }
432         }
433
434     }
435
436     /**
437      * <p>If not <code>null</code> and not empty, specifies a new, empty
438      * category should be added.</p>
439      *
440      * @param addCategory the new value of addCategory.
441      */

442     public final void setAddCategory(final String JavaDoc addCategory) {
443         this.addCategory = addCategory;
444     }
445
446     /**
447      * <p>If this event is a meeting, this is a semicolon (;) deliminated
448      * string of all attendee identifers.</p>
449      *
450      * @param attendees the new value of attendees.
451      */

452     public final void setAttendees(final String JavaDoc attendees) {
453         this.attendees = attendees;
454     }
455
456     /**
457      * <p>Private helper method to parse the date and time from strings,
458      * and set the results in a <code>GregorianCalendar</code>.</p>
459      *
460      * @param dateFormatter used to parse the strings
461      * @param dateParam string representation of the calendar date (without
462      * time).
463      * @param timeParam string representation of the time of day.
464      * @param calendar target for results (if there are no errors).
465      * @return <code>ValidationError</code> instance if there is a
466      * problem, otherwise <code>null</code>.
467      */

468     private ValidationError setDateTime(
469             final SettingDateFormatter dateFormatter,
470             final String JavaDoc dateParam,
471             final String JavaDoc timeParam,
472             final GregorianCalendar JavaDoc calendar) {
473         ValidationError error = null;
474
475         try {
476             if (!StringHandling.isNullOrEmpty(dateParam)) {
477                 String JavaDoc date = StringHandling.getNotNull(dateParam, "");
478                 String JavaDoc time = StringHandling.getNotNull(timeParam, "");
479                 try {
480                     calendar.setTime(dateFormatter.parse(date
481                             + " "
482                             + time));
483                 } catch (ParseException JavaDoc e) {
484                     // find out which went wrong - date or time
485
try {
486                         dateFormatter.setDateTimeText("{0}");
487                         dateFormatter.parse(dateParam);
488                         String JavaDoc[] parameters = {time};
489
490                         error = new ValidationError(
491                                 "event",
492                                 Calendar.BUNDLE_PATH,
493                                 mask.getField("time"),
494                                 "errors.time",
495                                 Arrays.asList(parameters));
496                     } catch (ParseException JavaDoc e2) {
497                         String JavaDoc[] parameters = {date};
498
499                         error = new ValidationError(
500                                 "event",
501                                 Calendar.BUNDLE_PATH,
502                                 mask.getField("date"),
503                                 "errors.date",
504                                 Arrays.asList(parameters));
505                     }
506                 }
507             }
508         } catch (DateFormatterException e) {
509             throw new RuntimeException JavaDoc(e);
510         }
511         return error;
512     }
513
514     /**
515      * <p>Set the values of the event this form refers to as a dependent
516      * value object.</p>
517      *
518      * @param event new event values this form refers to.
519      *
520      *
521      */

522     public final void setEvent(final EventDO event) {
523         this.event = event;
524     }
525
526     /**
527      * <p>Stores the number of the active tab page.</p>
528      *
529      * @param eventTab_activeTab the new value of eventTab_activeTab.
530      */

531     public final void setEventTab_activeTab(final Integer JavaDoc eventTab_activeTab) {
532         this.eventTab_activeTab = eventTab_activeTab;
533     }
534
535     /**
536      * <p>Specifies just the date part of the end date/time.</p>
537      *
538      * @param finishDate the new value of finishDate.
539      */

540     public final void setFinishDate(final String JavaDoc finishDate) {
541         this.finishDate = finishDate;
542     }
543
544     /**
545      * <p>Specifies just the time part of the end date/time.</p>
546      *
547      * @param finishTime the new value of finishTime.
548      */

549     public final void setFinishTime(final String JavaDoc finishTime) {
550         this.finishTime = finishTime;
551     }
552
553     /**
554      * <p>Specifies just the date part of the start date/time.</p>
555      *
556      * @param startDate the new value of startDate.
557      */

558     public final void setStartDate(final String JavaDoc startDate) {
559         this.startDate = startDate;
560     }
561
562     /**
563      * <p>Specifies just the time part of the start date/time.</p>
564      *
565      * @param startTime the new value of startTime.
566      */

567     public final void setStartTime(final String JavaDoc startTime) {
568         this.startTime = startTime;
569     }
570
571     /**
572      * <p>Page to currently display in the tab control.</p>
573      *
574      * @param tabPage the new value of tabPage.
575      */

576     public final void setTabPage(final String JavaDoc tabPage) {
577         this.tabPage = tabPage;
578     }
579
580     /**
581      * <p>Key used to access localized title string.</p>
582      *
583      * @param titleKey the new value of titleKey.
584      */

585     public final void setTitleKey(final String JavaDoc titleKey) {
586         this.titleKey = titleKey;
587     }
588
589     /**
590      * <p>Validates the properties that have been set for this HTTP
591      * request, and return an <code>ActionMessages</code> object that
592      * encapsulates any validation errors that have been found. If no
593      * errors are found,</p>
594      *
595      * @param mapping The mapping used to select this instance
596      * @param request The servlet request we are processing
597      * @return <code>null</code> or an <code>ActionMessages</code> object
598      * with no recorded error messages.
599      *
600      *
601      */

602     public ValidationErrors validate(final HttpServletRequest JavaDoc request,
603             final HttpSession JavaDoc session) {
604         ValidationError startDateError = null;
605         ValidationError finishDateError = null;
606         SecuritySession securitySession = (SecuritySession)
607             session.getAttribute("securitySession");
608         Locale JavaDoc locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
609
610         try {
611             dateFormatter.setDateFormat(DateFormatterConstants.DATE_INPUT);
612             dateFormatter.setTimeFormat(DateFormatterConstants.TIME_INPUT);
613         } catch (DateFormatterException e) {
614             throw new RuntimeException JavaDoc(e);
615         }
616
617         GregorianCalendar JavaDoc start = new GregorianCalendar JavaDoc();
618
619         if (StringHandling.isNullOrEmpty(startDate)) {
620            event.setStart(null);
621         } else {
622             if (StringHandling.isNullOrEmpty(startTime)) {
623                 try {
624                     dateFormatter.setDateTimeText("{0}");
625                 } catch (DateFormatterException e1) {
626                     throw new RuntimeException JavaDoc(e1);
627                 }
628             } else {
629                 try {
630                     dateFormatter.setDateTimeText("{0} {1}");
631                 } catch (DateFormatterException e1) {
632                     throw new RuntimeException JavaDoc(e1);
633                 }
634             }
635             startDateError = setDateTime(dateFormatter, startDate, startTime, start);
636             if (startDateError == null) {
637                 event.setStart(start);
638                 // when start time was not set, mark it as an all day event
639
if (StringHandling.isNullOrEmpty(startTime)) {
640                     event.setAllDayEvent(true);
641                 }
642             }
643         }
644
645         GregorianCalendar JavaDoc finish = new GregorianCalendar JavaDoc();
646         if (StringHandling.isNullOrEmpty(finishDate)) {
647            event.setFinish(null);
648         } else {
649             if (StringHandling.isNullOrEmpty(finishTime)) {
650                 try {
651                     dateFormatter.setDateTimeText("{0}");
652                 } catch (DateFormatterException e1) {
653                     throw new RuntimeException JavaDoc(e1);
654                 }
655             } else {
656                 try {
657                     dateFormatter.setDateTimeText("{0} {1}");
658                 } catch (DateFormatterException e1) {
659                     throw new RuntimeException JavaDoc(e1);
660                 }
661             }
662             finishDateError = setDateTime(dateFormatter, finishDate, finishTime, finish);
663             // if the finish is before start, that has too mean the finish date
664
// was the same as start date, but the time was unspecified
665
if (finishDateError == null && finish.after(start)) {
666                 if (StringHandling.isNullOrEmpty(finishTime)) {
667                   finish.add(GregorianCalendar.MINUTE, 24*60 - 1);
668                 }
669                 event.setFinish(finish);
670             }
671         }
672
673         // now perform server-side validation
674
ValidationErrors validationErrors = null;
675         try {
676             validationErrors = calendar.validate(securitySession, event);
677         } catch (SystemException e) {
678             throw new RuntimeException JavaDoc(e);
679         }
680
681         // if validation errors is null and we have a start date or an end date
682
// error, create a new colllection object so we can add that to the errors
683
if (((startDateError != null)
684                     || (finishDateError != null))
685                 && (validationErrors == null)) {
686             validationErrors = new ValidationErrors();
687         }
688         if (startDateError != null) {
689             validationErrors.add(startDateError);
690         }
691         if (finishDateError != null) {
692             validationErrors.add(finishDateError);
693         }
694         return validationErrors;
695     }
696
697     /**
698      * <p>
699      * Defines the base class of all objects in the value object list.
700      * </p>
701      *
702      * @return base class of all objects in the value object list.
703      */

704     public final Class JavaDoc getBaseClass() {
705         return baseClass;
706     }
707
708     /**
709      * <p>
710      * Mask containing all the field definitions for this list.
711      * </p>
712      *
713      * @return mask containing all the field definitions for this list.
714      */

715     public final Mask getMask() {
716         return mask;
717     }
718 }
719
Popular Tags