KickJava   Java API By Example, From Geeks To Geeks.

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


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: EventAction.java,v $
31  * Revision 1.6 2005/04/30 13:04:13 colinmacleod
32  * Fixes reverting id type from String to Integer.
33  *
34  * Revision 1.5 2005/04/29 02:48:15 colinmacleod
35  * Data bugfixes.
36  * Changed primary key back to Integer.
37  *
38  * Revision 1.4 2005/04/10 20:09:42 colinmacleod
39  * Added new themes.
40  * Changed id type to String.
41  * Changed i tag to em and b tag to strong.
42  * Improved PicoContainerFactory with NanoContainer scripts.
43  *
44  * Revision 1.3 2005/04/09 17:19:18 colinmacleod
45  * Changed copyright text to GPL v2 explicitly.
46  *
47  * Revision 1.2 2005/03/16 18:36:15 colinmacleod
48  * Fixed clear button and pop-up closing.
49  *
50  * Revision 1.1.1.1 2005/03/10 17:47:45 colinmacleod
51  * Restructured ivata op around Hibernate/PicoContainer.
52  * Renamed ivata groupware.
53  *
54  * Revision 1.10 2004/12/31 18:27:43 colinmacleod
55  * Added MaskFactory to constructor of MaskAction.
56  *
57  * Revision 1.9 2004/12/23 21:01:26 colinmacleod
58  * Updated Struts to v1.2.4.
59  * Changed base classes to use ivata masks.
60  *
61  * Revision 1.8 2004/11/12 18:19:14 colinmacleod
62  * Change action and form classes to extend MaskAction, MaskForm respectively.
63  *
64  * Revision 1.7 2004/11/12 16:08:10 colinmacleod
65  * Removed dependencies on SSLEXT.
66  * Moved Persistence classes to ivata masks.
67  *
68  * Revision 1.6 2004/11/03 15:31:50 colinmacleod
69  * Change method interfaces to remove log.
70  *
71  * Revision 1.5 2004/07/18 21:59:14 colinmacleod
72  * Removed Person from User - now you need to use addressbook/persistence manager to find the person (makes the app run faster.)
73  *
74  * Revision 1.4 2004/07/13 19:42:14 colinmacleod
75  * Moved project to POJOs from EJBs.
76  * Applied PicoContainer to services layer (replacing session EJBs).
77  * Applied Hibernate to persistence layer (replacing entity EJBs).
78  *
79  * Revision 1.3 2004/03/21 21:16:22 colinmacleod
80  * Shortened name to ivata op.
81  *
82  * Revision 1.2 2004/02/01 22:07:29 colinmacleod
83  * Added full names to author tags
84  *
85  * Revision 1.1.1.1 2004/01/27 20:58:21 colinmacleod
86  * Moved ivata openportal to SourceForge..
87  *
88  * Revision 1.3 2003/10/28 13:10:23 jano
89  * commiting calendar,
90  * still fixing compile and building openGroupware project
91  *
92  * Revision 1.2 2003/10/15 13:57:23 colin
93  * fixing for XDoclet
94  *
95  * Revision 1.11 2003/04/14 07:10:15 peter
96  * fixed helpKey logic
97  *
98  * Revision 1.10 2003/04/09 08:57:28 jano
99  * handling data of removing user
100  *
101  * Revision 1.9 2003/03/21 09:35:05 peter
102  * null meeting when not meeting type event
103  *
104  * Revision 1.8 2003/03/12 10:43:01 peter
105  * chairPerson defaults to current user
106  *
107  * Revision 1.7 2003/03/04 00:25:41 colin
108  * added type after clear to save for new form after clear button
109  *
110  * Revision 1.6 2003/03/03 16:40:43 colin
111  * now sets user name and modified by as appropriate
112  *
113  * Revision 1.5 2003/02/27 12:31:51 peter
114  * fixed Conversion of Ids String to Vector of Integers
115  *
116  * Revision 1.4 2003/02/26 17:12:37 peter
117  * fixed the countryCode validation, when set to empty string, must be null
118  *
119  * Revision 1.3 2003/02/26 13:43:07 peter
120  * fixed countryCode and multiplying new events in onConfirm
121  *
122  * Revision 1.2 2003/02/25 17:26:09 peter
123  * fixed return value of onConfirm
124  *
125  * Revision 1.1 2003/02/24 19:09:21 colin
126  * moved to business
127  *
128  * Revision 1.2 2003/02/13 13:53:06 colin
129  * conversion to Struts/pop-ups
130  *
131  * Revision 1.2 2003/02/13 08:45:42 colin
132  * conversion to Struts/popups
133  *
134  * Revision 1.1 2003/02/04 17:50:17 colin
135  * first version in CVS
136  * -----------------------------------------------------------------------------
137  */

138 package com.ivata.groupware.business.calendar.struts;
139
140 import org.apache.log4j.Logger;
141
142 import java.util.HashSet JavaDoc;
143 import java.util.Iterator JavaDoc;
144 import java.util.List JavaDoc;
145 import java.util.Locale JavaDoc;
146 import java.util.Set JavaDoc;
147 import java.util.Vector JavaDoc;
148
149 import javax.naming.OperationNotSupportedException JavaDoc;
150 import javax.servlet.http.HttpServletRequest JavaDoc;
151 import javax.servlet.http.HttpServletResponse JavaDoc;
152 import javax.servlet.http.HttpSession JavaDoc;
153
154 import org.apache.struts.Globals;
155 import org.apache.struts.action.ActionErrors;
156 import org.apache.struts.action.ActionForm;
157 import org.apache.struts.action.ActionMapping;
158 import org.apache.struts.util.MessageResources;
159 import org.picocontainer.PicoContainer;
160
161 import com.ivata.groupware.admin.security.server.SecuritySession;
162 import com.ivata.groupware.admin.security.user.UserDO;
163 import com.ivata.groupware.business.addressbook.AddressBook;
164 import com.ivata.groupware.business.addressbook.person.PersonDO;
165 import com.ivata.groupware.business.calendar.Calendar;
166 import com.ivata.groupware.business.calendar.event.EventDO;
167 import com.ivata.groupware.business.calendar.event.meeting.MeetingDO;
168 import com.ivata.groupware.business.calendar.event.meeting.category.MeetingCategoryDO;
169 import com.ivata.groupware.business.calendar.event.publicholiday.PublicHolidayDO;
170 import com.ivata.mask.MaskFactory;
171 import com.ivata.mask.util.CollectionHandling;
172 import com.ivata.mask.util.StringHandling;
173 import com.ivata.mask.util.SystemException;
174 import com.ivata.mask.web.struts.MaskAction;
175 import com.ivata.mask.web.struts.MaskAuthenticator;
176
177
178 /**
179  * <p>Invoked when the user edits, displays or enters a new event or
180  * public holiday.</p>
181  *
182  * @since 2003-02-02
183  * @author Colin MacLeod
184  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
185  * @version $Revision: 1.6 $
186  */

187 public class EventAction extends MaskAction {
188     /**
189      * Logger for this class.
190      */

191     private static final Logger logger = Logger.getLogger(EventAction.class);
192
193     /**
194      * <p>Private helper method to remove empty categories from the
195      * meeting. Empty in this case means the name/heading is empty and
196      * there
197      * are neither minute texts nor agenda points.</p>
198      *
199      * <p>If there <em>are</em> agenda points or minute texts but the
200      * name/heading is empty, it is replaced with a default localized text
201      * from the application resources.</p>
202      *
203      * <p>Note this method is static as it is also used from the library.</p>
204      *
205      * @param meeting the meeting to remove categories from.
206      * @param messages used to calculate default localized category
207      * name.
208      * @param locale used to calculate default localized category
209      * name.
210      */

211     public static void removeEmptyMeetingCategories(MeetingDO meeting, MessageResources messages, Locale JavaDoc locale) {
212         int count = 1;
213         Set JavaDoc categories = meeting.getCategories();
214         Iterator JavaDoc categoryIterator = categories.iterator();
215         List JavaDoc toRemove = new Vector JavaDoc();
216         while (categoryIterator.hasNext()) {
217             MeetingCategoryDO category = (MeetingCategoryDO) categoryIterator.next();
218
219             // note: the default name does not count as empty
220
String JavaDoc defaultName = messages.getMessage(locale, "default.category", new Integer JavaDoc(count++));
221
222             if (StringHandling.isNullOrEmpty(category.getName())) {
223                 // see if there are points or minutes
224
Set JavaDoc agendaPoints = category.getAgendaPoints();
225                 if ((agendaPoints == null)
226                         || (agendaPoints.size() == 0)) {
227                     toRemove.add(category);
228                 } else {
229                     // default the text to heading + number
230
category.setName(defaultName);
231                 }
232             }
233         }
234         // now go thro' and remove all the ones which are empty
235
categoryIterator = toRemove.iterator();
236         while(categoryIterator.hasNext()) {
237             categories.remove(categoryIterator.next());
238         }
239     }
240     private AddressBook addressBook;
241     private Calendar calendar;
242     /**
243      * TODO
244      *
245      * @param calendar
246      * @param addressBook
247      * @param maskFactory This factory is needed to access the masks and groups
248      * of masks.
249      * @param authenticator used to confirm whether or not the
250      * user should be allowed to continue, in the <code>execute</code> method.
251      */

252     public EventAction(Calendar calendar, AddressBook addressBook,
253             MaskFactory maskFactory, MaskAuthenticator authenticator) {
254         super(maskFactory, authenticator);
255         this.calendar = calendar;
256         this.addressBook = addressBook;
257     }
258
259     /**
260      * <p>Called when the clear button is pressed, or after an ok or
261      * delete button, where the session should be restored to its default
262      * state.</p>
263      *
264      * @param mapping current action mapping from <em>Struts</em> config.
265      * @param log valid logging object to write messages to.
266      * @param errors valid errors object to append errors to. If there are
267      * any errors, the action will return to the input.
268      * @param form optional ActionForm bean for this request (if any)
269      * @param request non-HTTP request we are processing
270      * @param response The non-HTTP response we are creating
271      * @param session returned from the <code>request</code> parameter.
272      * @param userName valid, non-null user name from session.
273      * @param settings valid, non-null settings from session.
274      * @exception SystemException if there is any problem which
275      * prevents processing. It will result in the webapp being forwarded
276      * to
277      * the standard error page.
278      *
279      */

280     public void clear(final ActionMapping mapping,
281             final ActionErrors errors,
282             final ActionForm form,
283             final HttpServletRequest JavaDoc request,
284             final HttpServletResponse JavaDoc response,
285             final HttpSession JavaDoc session) throws SystemException {
286         // and remove the current tab from the session
287
session.removeAttribute("eventTab_activeTab");
288         EventForm eventForm = (EventForm)form;
289         try {
290             eventForm.clear();
291         } catch (OperationNotSupportedException JavaDoc e) {
292             throw new RuntimeException JavaDoc(e);
293         }
294     }
295
296     /**
297      * <p>Overridden to see which type of event to create when creating a
298      * new event.</p>
299      *
300      * @param mapping current action mapping from <em>Struts</em> config.
301      * @param log valid logging object to write messages to.
302      * @param errors valid errors object to append errors to. If there are
303      * any errors, the action will return to the input.
304      * @param form optional ActionForm bean for this request (if any)
305      * @param request non-HTTP request we are processing
306      * @param response The non-HTTP response we are creating
307      * @param session returned from the <code>request</code> parameter.
308      * @param userName current user name from session. .
309      * @param settings valid, non-null settings from session.
310      * @exception SystemException if there is any problem which
311      * prevents processing. It will result in the webapp being forwarded
312      * to
313      * the standard error page.
314      * @return this method returns the string used to identify the correct
315      * <em>Struts</em> <code>ActionForward</code> which should follow this
316      * page, or <code>null</code> if it should return to the input.
317      *
318      *
319      */

320     public String JavaDoc execute(final ActionMapping mapping,
321             final ActionErrors errors,
322             final ActionForm form,
323             final HttpServletRequest JavaDoc request,
324             final HttpServletResponse JavaDoc response,
325             final HttpSession JavaDoc session) throws SystemException {
326         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
327         UserDO user = securitySession.getUser();
328         PicoContainer container = securitySession.getContainer();
329         MessageResources messages = getResources(request, "calendar");
330         Locale JavaDoc locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
331
332         // look for a request parameter identifying the type - that means new
333
String JavaDoc type = request.getParameter("type");
334         EventForm eventForm = (EventForm) form;
335         if (type != null) {
336             if ("meeting".equals(type)) {
337                 MeetingDO meeting = new MeetingDO();
338                 eventForm.setEvent(meeting);
339                 eventForm.setTitleKey("event.title.new.meeting");
340                 eventForm.setHelpKey("calendar.meeting.event");
341
342                 // initially make one agenda category
343
Set JavaDoc meetingCategories = meeting.getCategories();
344                 if (meetingCategories == null) {
345                     meetingCategories = new HashSet JavaDoc();
346                     meeting.setCategories(meetingCategories);
347                 }
348                 if (meetingCategories.size() == 0) {
349                     MeetingCategoryDO newCategory = new MeetingCategoryDO();
350                     newCategory.setMeeting(meeting);
351                     newCategory.setName(messages.getMessage(locale, "default.category", "1"));
352                     meetingCategories.add(newCategory);
353                 }
354                 PersonDO person;
355                 person = addressBook.findPersonByUserName(securitySession,
356                         user.getName());
357                 meeting.setChairPerson(person);
358
359             } else if ("publicHoliday".equals(type)) {
360                 eventForm.setEvent(new PublicHolidayDO());
361                 eventForm.setTitleKey("event.title.new.publicHoliday");
362                 eventForm.setHelpKey("calendar.publicHoliday");
363             } else {
364                 eventForm.setEvent(new EventDO());
365                 eventForm.setTitleKey("event.title.new");
366                 eventForm.setHelpKey("calendar.event");
367             }
368             return null;
369         }
370
371         // --tabs-- currently only meetings have tabs
372
// the request overrides the current setting in the form
373
Integer JavaDoc tab = eventForm.getEventTab_activeTab();
374         Integer JavaDoc requestTab = StringHandling.integerValue(request.getParameter("eventTab_activeTab"));
375         if (requestTab != null) {
376             eventForm.setEventTab_activeTab(tab = requestTab);
377         }
378         EventDO event = eventForm.getEvent();
379
380         // now choose the right page to show based on the tab
381
if (tab.equals(new Integer JavaDoc(1))) {
382             eventForm.setTabPage("/calendar/meetingPeople.jsp");
383             eventForm.setHelpKey("calendar.meeting.people");
384         } else if (tab.equals(new Integer JavaDoc(2))) {
385             eventForm.setTabPage("/calendar/meetingAgenda.jsp");
386             eventForm.setHelpKey("calendar.meeting.agenda");
387         } else {
388             eventForm.setTabPage("/calendar/eventDetails.jsp");
389             if (event instanceof PublicHolidayDO) {
390                 eventForm.setHelpKey("calendar.publicHoliday");
391             } else if (event instanceof MeetingDO) {
392                 eventForm.setHelpKey("calendar.meeting.event");
393             } else {
394                 eventForm.setHelpKey("calendar.event");
395             }
396         }
397
398         // meeting handling
399
if (event instanceof MeetingDO) {
400             MeetingDO meeting = (MeetingDO) event;
401             // go thro' all the categories and discard empty ones
402
removeEmptyMeetingCategories(meeting, messages, locale);
403             // check there is a collection for categories
404
Set JavaDoc categories = meeting.getCategories();
405             if (categories == null) {
406                 meeting.setCategories(categories = new HashSet JavaDoc());
407             }
408
409             // if the add button was pressed, add an empty category
410
// if the new arrays are empty, add a solitary one
411
int size = categories.size();
412             if (!StringHandling.isNullOrEmpty(eventForm.getAddCategory())
413                     || (size == 0)) {
414                 MeetingCategoryDO category = new MeetingCategoryDO();
415                 category.setName(messages.getMessage(locale, "default.category",
416                     new Integer JavaDoc(size + 1)));
417                 category.setMeeting(meeting);
418                 categories.add(category);
419             }
420         }
421
422         return null;
423     }
424
425     /**
426      * <p>This method is called if the ok or apply buttons are pressed.</p>
427      *
428      * @param mapping current action mapping from <em>Struts</em> config.
429      * @param log valid logging object to write messages to.
430      * @param errors valid errors object to append errors to. If there are
431      * any errors, the action will return to the input.
432      * @param form optional ActionForm bean for this request (if any)
433      * @param request non-HTTP request we are processing
434      * @param response The non-HTTP response we are creating
435      * @param session returned from the <code>request</code> parameter.
436      * @param userName valid, non-null user name from session.
437      * @param settings valid, non-null settings from session.
438      * @param ok <code>true</code> if the ok button was pressed, otherwise
439      * <code>false</code> if the apply button was pressed.
440      * @exception SystemException if there is any problem which
441      * prevents processing. It will result in the webapp being forwarded
442      * to
443      * the standard error page.
444      * @return this method returns the string used to identify the correct
445      * <em>Struts</em> <code>ActionForward</code> which should follow this
446      * page, or <code>null</code> if it should return to the input.
447      *
448      */

449     public String JavaDoc onConfirm(final ActionMapping mapping,
450             final ActionErrors errors,
451             final ActionForm form,
452             final HttpServletRequest JavaDoc request,
453             final HttpServletResponse JavaDoc response,
454             final HttpSession JavaDoc session,
455             final String JavaDoc defaultForward) throws SystemException {
456         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
457         UserDO user = securitySession.getUser();
458         EventForm eventForm = (EventForm) form;
459         EventDO event = eventForm.getEvent();
460
461         // meeting ?
462
if (event instanceof MeetingDO) {
463             MeetingDO meeting = (MeetingDO) event;
464
465             // set the attendee ids
466
List JavaDoc attendeeIdStrings = new Vector JavaDoc(CollectionHandling.convertFromLines(eventForm.getAttendees(), ";"));
467             // convert the id strings to Interers
468
Set JavaDoc attendees = new HashSet JavaDoc();
469             for (Iterator JavaDoc i = attendeeIdStrings.iterator(); i.hasNext(); ) {
470                 String JavaDoc attendeeId = (String JavaDoc) i.next();
471                 PersonDO attendee = addressBook.findPersonByPrimaryKey(
472                         securitySession, attendeeId);
473                 attendees.add(attendee);
474             }
475             meeting.setAttendees(attendees);
476
477             // go thro' all the categories and discard empty ones
478
removeEmptyMeetingCategories(meeting,
479                 getResources(request, "calendar"), getLocale(request));
480
481             // if there is no chair person default it to this user's person
482
PersonDO chairPerson = meeting.getChairPerson();
483             if (chairPerson == null) {
484                 chairPerson = addressBook.findPersonByUserName(securitySession,
485                         user.getName());
486             }
487         }
488         // if it doesn't have an id, it is a new event
489
if (event.getId() == null) {
490             event = calendar.addEvent(securitySession, event);
491         } else {
492             // amend an existing event
493
event = calendar.amendEvent(securitySession, event);
494         }
495         eventForm.setEvent(event);
496
497         request.setAttribute("openerPage", "/calendar/index.action");
498
499         return defaultForward;
500     }
501
502     /**
503      * <p>This method is called if the delete (confirm, not warn) button
504      * is pressed.</p>
505      * @param mapping current action mapping from <em>Struts</em> config.
506      * @param errors valid errors object to append errors to. If there are
507      * any errors, the action will return to the input.
508      * @param form optional ActionForm bean for this request (if any)
509      * @param request non-HTTP request we are processing
510      * @param response The non-HTTP response we are creating
511      * @param session returned from the <code>request</code> parameter.
512      * @param log valid logging object to write messages to.
513      * @param userName valid, non-null user name from session.
514      * @param settings valid, non-null settings from session.
515      *
516      * @exception SystemException if there is any problem which
517      * prevents processing. It will result in the webapp being forwarded
518      * to
519      * the standard error page.
520      * @return this method returns the string used to identify the correct
521      * <em>Struts</em> <code>ActionForward</code> which should follow this
522      * page, or <code>null</code> if it should return to the input.
523      *
524      */

525     public String JavaDoc onDelete(final ActionMapping mapping,
526             final ActionErrors errors,
527             final ActionForm form,
528             final HttpServletRequest JavaDoc request,
529             final HttpServletResponse JavaDoc response,
530             final HttpSession JavaDoc session, final String JavaDoc defaultForward) throws SystemException {
531         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
532         EventForm eventForm = (EventForm) form;
533         EventDO event = eventForm.getEvent();
534         calendar.removeEvent(securitySession, event);
535         request.setAttribute("openerPage", "/calendar/index.action");
536         return defaultForward;
537     }
538 }
539
Popular Tags