KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > calendar > CalendarImpl


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: CalendarImpl.java,v $
31  * Revision 1.3 2005/04/10 20:43:45 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:16 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:47:42 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.5 2004/11/12 18:17:26 colinmacleod
45  * Ordered imports.
46  *
47  * Revision 1.4 2004/11/12 16:08:08 colinmacleod
48  * Removed dependencies on SSLEXT.
49  * Moved Persistence classes to ivata masks.
50  *
51  * Revision 1.3 2004/11/03 15:49:49 colinmacleod
52  * Changed todo comments to TODO: all caps.
53  *
54  * Revision 1.2 2004/07/13 19:42:12 colinmacleod
55  * Moved project to POJOs from EJBs.
56  * Applied PicoContainer to services layer (replacing session EJBs).
57  * Applied Hibernate to persistence layer (replacing entity EJBs).
58  *
59  * Revision 1.1 2004/03/27 10:31:25 colinmacleod
60  * Split off business logic from remote facades to POJOs.
61  * -----------------------------------------------------------------------------
62  */

63 package com.ivata.groupware.business.calendar;
64
65 import java.sql.Timestamp JavaDoc;
66 import java.util.ArrayList JavaDoc;
67 import java.util.Arrays JavaDoc;
68 import java.util.Collection JavaDoc;
69 import java.util.GregorianCalendar JavaDoc;
70 import java.util.Iterator JavaDoc;
71 import java.util.TimeZone JavaDoc;
72 import java.util.TreeSet JavaDoc;
73
74 import com.ivata.groupware.admin.security.server.SecuritySession;
75 import com.ivata.groupware.business.BusinessLogic;
76 import com.ivata.groupware.business.calendar.event.EventDO;
77 import com.ivata.groupware.business.calendar.event.meeting.MeetingDO;
78 import com.ivata.groupware.business.calendar.event.publicholiday.PublicHolidayDO;
79 import com.ivata.groupware.container.persistence.QueryPersistenceManager;
80 import com.ivata.groupware.container.persistence.TimestampDOHandling;
81 import com.ivata.mask.Mask;
82 import com.ivata.mask.MaskFactory;
83 import com.ivata.mask.persistence.PersistenceSession;
84 import com.ivata.mask.util.StringHandling;
85 import com.ivata.mask.util.SystemException;
86 import com.ivata.mask.validation.ValidationError;
87 import com.ivata.mask.validation.ValidationErrors;
88 import com.ivata.mask.validation.ValidationException;
89
90 /**
91  * @author Colin MacLeod
92  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
93  * @since 24-Mar-2004
94  * @version $Revision: 1.3 $
95  */

96 public class CalendarImpl extends BusinessLogic implements Calendar {
97
98     /**
99      * <p>This class is used to sort the Events.</p>
100      */

101     private class EventComparator implements java.util.Comparator JavaDoc {
102
103         /**
104          * <p>Compare two objects (in this case, both are instances of
105          * {@link com.ivata.groupware.business.calendar.event.EventDO
106          * EventDO}) and return which is the higher priority.</p>
107          *
108          * @param o1 first instance of <code>Event</code> to be compared.
109          * @param o2 second instance of <code>Event</code> to be compared.
110          * @return a negative integer, or a positive integer as the first argument
111          * comes after, same as, or before the second.
112          */

113         public int compare(final Object JavaDoc o1,
114             final Object JavaDoc o2) {
115             EventDO event1 = (EventDO) o1;
116             EventDO event2 = (EventDO) o2;
117             long test1 = event1.getStart().getTime().getTime();
118             long test2 = event2.getStart().getTime().getTime();
119
120             // note: purposely don't allow an equals case here!! If two are
121
// equal, then only one will appear in the final set
122
return (test1 > test2) ? 1 : -1;
123         }
124     }
125
126     /**
127      * Persistence manger used to store/retrieve data objects.
128      */

129     private QueryPersistenceManager persistenceManager;
130
131     /**
132      * Construct a new address book.
133      *
134      * @param persistenceManager used to store objects in db.
135      */

136     public CalendarImpl(QueryPersistenceManager persistenceManager,
137             MaskFactory maskFactory) {
138         this.persistenceManager = persistenceManager;
139         this.maskFactory = maskFactory;
140     }
141     private MaskFactory maskFactory;
142
143
144     /* (non-Javadoc)
145      * @see com.ivata.groupware.business.calendar.Calendar#addEvent(com.ivata.groupware.business.calendar.event.EventDO)
146      */

147     public EventDO addEvent(final SecuritySession securitySession,
148             final EventDO event)
149             throws SystemException {
150         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
151         try {
152             // before creating the event, check we have reasonable data
153
ValidationErrors errors = validate(securitySession, event);
154             if (!errors.isEmpty()) {
155                 throw new ValidationException(errors);
156             }
157             TimestampDOHandling.add(securitySession, event);
158             return (EventDO) persistenceManager.add(persistenceSession, event);
159         } catch (Exception JavaDoc e) {
160             persistenceSession.cancel();
161             throw new SystemException(e);
162         } finally {
163             persistenceSession.close();
164         }
165     }
166
167     /**
168      * <p>method finding meeting without minutes, it's mean that meeting hasn't
169      * library item.
170      * meeting has to be before input day.</p>
171      *
172      * @param day we are finding meetings before this day
173      * @return Vector of meetingDOs
174      *
175      * @ejb.interface-method
176      * view-type = "remote"
177      */

178 /* public java.util.Vector findMeetingsWithoutMinutes(java.util.Calendar day) {
179         Vector returnItems = new Vector();
180
181         try {
182             MeetingLocalHome homeMeeting = (MeetingLocalHome)
183                 ApplicationServer.findLocalHome("MeetingLocal",
184                     MeetingLocalHome.class);
185
186             Calendar tmpday = new java.util.Calendar();
187             tmpday.setTime(day.getTime());
188
189             Collection meetings = homeMeeting.findMeetingWithoutMinutes(new Timestamp(tmpday.getTime().getTime()));
190
191             for (Iterator i = meetings.iterator();
192                 i.hasNext();) {
193                 MeetingLocal meeting = (MeetingLocal) i.next();
194                 returnItems.add(meeting.getDO());
195             }
196
197             return returnItems;
198
199         } catch (NamingException e) {
200             throw new EJBException(e);
201         } catch (FinderException e) {
202             throw new EJBException(e);
203         }
204     }*/

205
206
207     /* (non-Javadoc)
208      * @see com.ivata.groupware.business.calendar.Calendar#amendEvent(com.ivata.groupware.business.calendar.event.EventDO)
209      */

210     public EventDO amendEvent(final SecuritySession securitySession,
211             final EventDO event)
212             throws SystemException {
213         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
214         try {
215             // before changing the event, check we have reasonable data
216
ValidationErrors errors = validate(securitySession, event);
217             if (!errors.isEmpty()) {
218                 throw new ValidationException(errors);
219             }
220             TimestampDOHandling.amend(securitySession, event);
221             persistenceManager.amend(persistenceSession, event);
222             return event;
223         } catch (Exception JavaDoc e) {
224             persistenceSession.cancel();
225             throw new SystemException(e);
226         } finally {
227             persistenceSession.close();
228         }
229     }
230
231     /* (non-Javadoc)
232      * @see com.ivata.groupware.business.calendar.Calendar#findEventByPrimaryKey(Integer)
233      */

234     public EventDO findEventByPrimaryKey(final SecuritySession securitySession,
235             final String JavaDoc id)
236             throws SystemException {
237         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
238         try {
239             return (EventDO) persistenceManager.findByPrimaryKey(persistenceSession,
240                     EventDO.class, id);
241         } catch (Exception JavaDoc e) {
242             persistenceSession.cancel();
243             throw new SystemException(e);
244         } finally {
245             persistenceSession.close();
246         }
247     }
248
249     /* (non-Javadoc)
250      * @see com.ivata.groupware.business.calendar.Calendar#findEventsForDay(java.util.Calendar)
251      */

252     public Collection JavaDoc findEventsForDay(final SecuritySession securitySession,
253             final java.util.Calendar JavaDoc day)
254             throws SystemException {
255         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
256         try {
257             GregorianCalendar JavaDoc tmpday = new GregorianCalendar JavaDoc();
258             TimeZone JavaDoc timeZone = day.getTimeZone();
259
260             tmpday.setTime(day.getTime());
261             tmpday.setTimeZone(timeZone);
262             Timestamp JavaDoc start = new Timestamp JavaDoc(tmpday.getTime().getTime());
263
264             tmpday.set(java.util.Calendar.DAY_OF_YEAR, tmpday.get(java.util.Calendar.DAY_OF_YEAR) + 1); // this move day field to next
265
Timestamp JavaDoc finish = new Timestamp JavaDoc(tmpday.getTime().getTime());
266             Collection JavaDoc events = persistenceManager.find(persistenceSession, "calendarEventByStartFinish",
267                 new Object JavaDoc[] {start, finish});
268             // sort the events TODO: I would rather use the database to sort
269
// but EJB QL still has no 'order by'
270
TreeSet JavaDoc sorted = new TreeSet JavaDoc(new EventComparator());
271
272             sorted.addAll(events);
273             Collection JavaDoc returnItems = new ArrayList JavaDoc();
274             // either return all items, or the count requested, whatever comes
275
// first
276
// TODO: I would rather use the database to limit the return but
277
// EJB QL still has no 'limit'
278
for (Iterator JavaDoc i = sorted.iterator();
279                 i.hasNext();) {
280                 EventDO eventDO = (EventDO) i.next();
281                 // changing the timeZone for start and end, for correct sorting
282
// outside this method
283
java.util.Calendar JavaDoc tmpStart = eventDO.getStart();
284                 if (tmpStart!=null) {
285                     tmpStart.setTimeZone(timeZone);
286                     eventDO.setStart(tmpStart);
287                 }
288                 java.util.Calendar JavaDoc tmpFinish = eventDO.getFinish();
289                 if (tmpFinish!=null) {
290                     tmpFinish.setTimeZone(timeZone);
291                     eventDO.setFinish(tmpFinish);
292                 }
293
294                 returnItems.add(eventDO);
295             }
296             return returnItems;
297         } catch (Exception JavaDoc e) {
298             persistenceSession.cancel();
299             throw new SystemException(e);
300         } finally {
301             persistenceSession.close();
302         }
303     }
304
305     /* (non-Javadoc)
306      * @see com.ivata.groupware.business.calendar.Calendar#removeEvent(com.ivata.groupware.business.calendar.event.EventDO)
307      */

308     public void removeEvent(final SecuritySession securitySession,
309             final EventDO event)
310             throws SystemException {
311         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
312         try {
313             persistenceManager.remove(persistenceSession,
314                     event.getClass(), event.getId());
315         } catch (Exception JavaDoc e) {
316             persistenceSession.cancel();
317             throw new SystemException(e);
318         } finally {
319             persistenceSession.close();
320         }
321     }
322
323     /* (non-Javadoc)
324      * @see com.ivata.groupware.business.calendar.Calendar#validateEvent(com.ivata.groupware.business.calendar.event.EventDO, java.util.Locale)
325      */

326     public ValidationErrors validate(final SecuritySession securitySession,
327             final EventDO eventDO) {
328         ValidationErrors errors = new ValidationErrors();
329         Mask mask = maskFactory.getMask(EventDO.class);
330         String JavaDoc fieldKey;
331
332         String JavaDoc startFieldKey = "";
333         String JavaDoc finishFieldKey = "";
334
335         // the field names depend on the type
336
if (eventDO instanceof PublicHolidayDO) {
337             startFieldKey = "event.field.startDate.publicHoliday";
338             finishFieldKey = "event.field.finishDate.publicHoliday";
339         } else {
340             startFieldKey = "event.field.startDate";
341             finishFieldKey = "event.field.finishDate";
342         }
343
344         // start date is always mandatory
345
if (eventDO.getStart() == null) {
346             errors.add(new ValidationError(
347                     "event",
348                     Calendar.BUNDLE_PATH,
349                     mask.getField("start"),
350                     "errors.required"));
351         }
352
353         //start date/time must be before end date/time
354
if (eventDO.getStart() != null && eventDO.getFinish()!= null &&
355             eventDO.getStart().after(eventDO.getFinish())) {
356
357             errors.add(new ValidationError(
358                     "event",
359                     Calendar.BUNDLE_PATH,
360                     mask.getField("finish"),
361                     "errors.calendar.startAfterFinish",
362                     Arrays.asList(new String JavaDoc[] {
363                         startFieldKey,
364                         finishFieldKey})));
365         }
366
367         // subject is always mandatory
368
if (StringHandling.isNullOrEmpty(eventDO.getSubject())) {
369             // the field name depends on the type
370
if (eventDO instanceof PublicHolidayDO) {
371                 fieldKey = "event.field.subject.publicHoliday";
372             } else {
373                 fieldKey = "event.field.subject";
374             }
375             errors.add(new ValidationError(
376                     "event",
377                     Calendar.BUNDLE_PATH,
378                     mask.getField("subject"),
379                     "errors.required"));
380         }
381         return errors;
382     }
383
384     /* (non-Javadoc)
385      * @see com.ivata.groupware.business.calendar.Calendar#validateMeeting(com.ivata.groupware.business.calendar.event.meeting.MeetingDO, java.util.Locale)
386      */

387     public ValidationErrors validate(final
388             SecuritySession securitySession,
389             final MeetingDO meetingDO) {
390         ValidationErrors errors = validate(securitySession, (EventDO) meetingDO);
391         Mask mask = maskFactory.getMask(MeetingDO.class);
392
393         // location is always mandatory
394
if (StringHandling.isNullOrEmpty(meetingDO.getLocation())) {
395             errors.add(new ValidationError(
396                     "event",
397                     Calendar.BUNDLE_PATH,
398                     mask.getField("location"),
399                     "errors.required"));
400         }
401         return errors;
402     }
403
404 }
405
Popular Tags