View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.groupware.map;
24  
25  import java.util.ArrayList;
26  import java.util.Calendar;
27  import java.util.List;
28  
29  import de.bea.domingo.DDocument;
30  import de.bea.domingo.DViewEntry;
31  import de.bea.domingo.groupware.CalendarEntry;
32  import de.bea.domingo.groupware.CalendarEntryDigest;
33  import de.bea.domingo.groupware.CalendarEntry.Type;
34  import de.bea.domingo.map.BaseDMapper;
35  import de.bea.domingo.map.BaseMapper;
36  import de.bea.domingo.map.ConstantMapper;
37  import de.bea.domingo.map.CustomMapper;
38  import de.bea.domingo.map.DirectMapper;
39  import de.bea.domingo.map.MappingException;
40  import de.bea.domingo.map.MethodNotFoundException;
41  import de.bea.domingo.util.GregorianDateTime;
42  
43  /***
44   * Mapper for calendar entries, e.g. in a Notes mail database.
45   *
46   * @see de.bea.domingo.groupware.CalendarEntry
47   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
48   * @author Thanx to <a href=mailto:d-rizzle at users.sourceforge.net>Dave Rowe</a> for some patches
49   */
50  public final class CalendarEntryMapper extends BaseMapper {
51  
52      private static final int TYPE_COLUMN = 14;
53  
54      private static final int CHAIR_COLUMN = 11;
55  
56      private static final int INFO_COLUMN = 12;
57  
58      private static final int START_DATE_TIME_COLUMN = 8;
59  
60      private static final int END_DATE_COLUMN = 10;
61  
62      private static final Class INSTANCE_CLASS = CalendarEntry.class;
63  
64      private static final Class DIGEST_CLASS = CalendarEntryDigest.class;
65  
66      /***
67       * Constructor.
68       *
69       * @throws MethodNotFoundException if the getter or setter method was not found for the attribute name
70       */
71      public CalendarEntryMapper() throws MethodNotFoundException {
72          super(INSTANCE_CLASS, DIGEST_CLASS);
73          add(new DirectMapper("StartDate", Calendar.class));
74          add(new DirectMapper("EndDate", Calendar.class));
75          add(new DirectMapper("StartTime", Calendar.class));
76          add(new DirectMapper("EndTime", Calendar.class));
77          // TODO add timezone handling
78  //        add(new DirectMapper("StartTimezone", Calendar.class));
79  //        add(new DirectMapper("EndTimezone", Calendar.class));
80          add(new DirectMapper("StartDateTime", List.class));
81          add(new ConstantMapper("Form", "Appointment"));
82          add(new DirectMapper("Subject", "Title", String.class));
83          // TODO fix the mapping of Chair. It can be a List or a String depending on the calendar entry.
84          //        add(new DirectMapper("Chair", List.class));
85          add(new DirectMapper("Location", String.class));
86          add(new DirectMapper("Categories", List.class));
87  //        add(new DirectMapper("SendAttachments", Boolean.class));
88          add(new DirectMapper("SendTo", "RequiredInvitees", List.class));
89          add(new DirectMapper("CopyTo", "OptionalInvitees", List.class));
90          add(new DirectMapper("BlindCopyTo", "InformedInvitees", List.class));
91          add(new DirectMapper("Room", String.class));
92          add(new DirectMapper("RoomToReserve", "Rooms", List.class));
93          add(new DirectMapper("Resources", "Resources", List.class));
94          add(new DirectMapper("APPTUNID", "AppointmentUnid", String.class));
95          add(new TypeMapper());
96          add(new ComputeCalendarEntry());
97          // TODO add missing fields for calendar entry
98      }
99  
100     /***
101      * {@inheritDoc}
102      *
103      * @see de.bea.domingo.map.DMapper#newInstance()
104      */
105     public Object newInstance() {
106         return new CalendarEntry();
107     }
108 
109     /***
110      * {@inheritDoc}
111      *
112      * @see de.bea.domingo.map.DMapper#newDigest()
113      */
114     public Object newDigest() {
115         return new CalendarEntry();
116     }
117 
118     /***
119      * {@inheritDoc}
120      *
121      * @see de.bea.domingo.map.DMapper#map(de.bea.domingo.DViewEntry,
122      *      java.lang.Object)
123      */
124     public void map(final DViewEntry viewEntry, final Object object) throws MappingException {
125         CalendarEntryDigest digest = (CalendarEntryDigest) object;
126         digest.setUnid(viewEntry.getUniversalID());
127         // TODO the following situation is coming up fairly frequently. Is there a way to make this simpler?
128         // CHAIR_COLUMN will be zero, one or more strings. Handle list vs. string case.
129         List columnValues = viewEntry.getColumnValues();
130         Object chairColumnValues = columnValues.get(CHAIR_COLUMN);
131         if (chairColumnValues instanceof List) {
132             digest.setChairs((List) chairColumnValues);
133         } else {
134             List chairList = new ArrayList(1);
135             chairList.add((String) chairColumnValues);
136             digest.setChairs(chairList);
137         }
138         // digest.setType(getType(Integer.parseInt(((String)
139         // columnValues.get(TYPE_COLUMN)))));
140         digest.setStartDateTime(getCalendar(columnValues.get(START_DATE_TIME_COLUMN)));
141         if (digest.getType() != Type.REMINDER && digest.getType() != Type.ANNIVERSARY) {
142             digest.setEndDateTime(getCalendar(columnValues.get(END_DATE_COLUMN)));
143         }
144         Object infoColumnValues = columnValues.get(INFO_COLUMN);
145         if (infoColumnValues instanceof List) {
146             List infoValues = (List) infoColumnValues;
147             digest.setSubject((String) infoValues.get(0));
148             digest.setLocation((String) infoValues.get(1));
149         } else {
150             digest.setSubject((String) infoColumnValues);
151         }
152     }
153 
154     private static class ComputeCalendarEntry extends CustomMapper {
155 
156         /***
157          * {@inheritDoc}
158          *
159          * @see de.bea.domingo.map.Mapper#map(de.bea.domingo.DDocument,
160          *      java.lang.Object)
161          */
162         public void map(final DDocument document, final Object object) throws MappingException {
163             // TODO Auto-generated method stub
164 
165         }
166 
167         /***
168          * {@inheritDoc}
169          *
170          * @see de.bea.domingo.map.Mapper#map(java.lang.Object,
171          *      de.bea.domingo.DDocument)
172          */
173         public void map(final Object object, final DDocument document) throws MappingException {
174             CalendarEntry entry = (CalendarEntry) object;
175             Calendar date = entry.getStartDate();
176             Calendar time = entry.getStartTime();
177             final Calendar dateTime = new GregorianDateTime();
178             if (date != null) {
179                 dateTime.set(Calendar.YEAR, date.get(Calendar.YEAR));
180                 dateTime.set(Calendar.MONTH, date.get(Calendar.MONTH));
181                 dateTime.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH));
182             }
183             if (time != null) {
184                 dateTime.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
185                 dateTime.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
186                 dateTime.set(Calendar.SECOND, time.get(Calendar.SECOND));
187             }
188             // TODO add time zone handling
189             document.replaceItemValue("CalendarDateTime", dateTime);
190         }
191     }
192 
193     /***
194      * Maps the type.
195      */
196     private static class TypeMapper extends BaseDMapper {
197 
198         /***
199          * {@inheritDoc}
200          * @see de.bea.domingo.map.Mapper#map(de.bea.domingo.DDocument, java.lang.Object)
201          */
202         public void map(final DDocument document, final Object object) throws MappingException {
203             ((CalendarEntry) object).setUnid(document.getUniversalID());
204             if (!"".equals(document.getItemValueString("Type"))) {
205                 int type = Integer.parseInt(document.getItemValueString("Type"));
206                 ((CalendarEntry) object).setType(getType((byte) type));
207             } else if (!"".equals(document.getItemValueString("AppointmentType"))) {
208                 int type = Integer.parseInt(document.getItemValueString("AppointmentType"));
209                 ((CalendarEntry) object).setType(getType((byte) type));
210             }
211         }
212 
213         /***
214          * {@inheritDoc}
215          *
216          * @see de.bea.domingo.map.Mapper#map(java.lang.Object,
217          *      de.bea.domingo.DDocument)
218          */
219         public void map(final Object object, final DDocument document) throws MappingException {
220             document.replaceItemValue("Type", getType(((CalendarEntry) object).getType()));
221             document.replaceItemValue("AppointmentType", getType(((CalendarEntry) object).getType()));
222         }
223     }
224 
225     private static int getType(final Type value) {
226         if (value == Type.APPOINTMENT) {
227             return Type.APPOINTMENT_VALUE;
228         } else if (value == Type.ANNIVERSARY) {
229             return Type.ANNIVERSARY_VALUE;
230         } else if (value == Type.ALLDAYEVENT) {
231             return Type.ALLDAYEVENT_VALUE;
232         } else if (value == Type.MEETING) {
233             return Type.MEETING_VALUE;
234         } else if (value == Type.REMINDER) {
235             return Type.REMINDER_VALUE;
236         }
237         throw new IllegalArgumentException("Unknown type: " + value);
238     }
239 
240     private static Type getType(final int value) {
241         if (value == Type.APPOINTMENT_VALUE) {
242             return Type.APPOINTMENT;
243         } else if (value == Type.ANNIVERSARY_VALUE) {
244             return Type.ANNIVERSARY;
245         } else if (value == Type.ALLDAYEVENT_VALUE) {
246             return Type.ALLDAYEVENT;
247         } else if (value == Type.MEETING_VALUE) {
248             return Type.MEETING;
249         } else if (value == Type.REMINDER_VALUE) {
250             return Type.REMINDER;
251         }
252         throw new IllegalArgumentException("Unknown type: " + value);
253     }
254 }