View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.calendar.ws.bean;
19  
20  import static org.exoplatform.calendar.ws.CalendarRestApi.CALENDAR_URI;
21  import static org.exoplatform.calendar.ws.CalendarRestApi.CATEGORY_URI;
22  import static org.exoplatform.calendar.ws.CalendarRestApi.EVENT_URI;
23  
24  import java.io.Serializable;
25  import java.util.Calendar;
26  import java.util.LinkedList;
27  import java.util.List;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.exoplatform.calendar.service.Attachment;
31  import org.exoplatform.calendar.service.CalendarEvent;
32  import org.exoplatform.calendar.service.Reminder;
33  import org.exoplatform.calendar.service.Utils;
34  import org.exoplatform.calendar.ws.CalendarRestApi;
35  import org.exoplatform.calendar.ws.common.Resource;
36  import org.exoplatform.commons.utils.ISO8601;
37  import org.exoplatform.webservice.cs.bean.End;
38  
39  public class EventResource extends Resource {
40    private static final long serialVersionUID = 9085055105843346382L;
41    
42    private String            subject;
43    private String                    description;
44    private String                      from;
45    private String                      to;
46    private Serializable           calendar;
47    private Serializable[]                  categories;
48    private String                    location;
49    private String                       priority;
50    private RepeatResource            repeat;
51    private String                    recurrenceId;
52    private Serializable                    originalEvent;
53    private boolean                   isOccur;
54    private Reminder[]                reminder;
55    private Serializable[]              attachments;
56    private UploadResource[]        uploadResources;
57    private String[]                  participants;
58    private String                    privacy;
59    private String                    availability;  
60    private String                categoryId;
61    private String                calendarId;
62    
63    public EventResource() {
64      super(null);
65    }
66  
67    public EventResource(CalendarEvent data, String basePath) throws Exception {
68      super(data.getId());
69  
70      StringBuilder href = new StringBuilder(basePath).append(EVENT_URI).append(data.getId()); 
71      setHref(href.toString());
72      subject = data.getSummary();
73      description = data.getDescription();
74      
75      Calendar fromCal = Utils.getInstanceTempCalendar();
76      fromCal.setTime(data.getFromDateTime());
77      from = ISO8601.format(fromCal);
78      
79      Calendar toCal = Utils.getInstanceTempCalendar();
80      toCal.setTime(data.getToDateTime());
81      to = ISO8601.format(toCal);
82      
83      calendar = new StringBuilder(basePath).append(CALENDAR_URI)
84                                               .append(data.getCalendarId())
85                                               .toString();
86      calendarId = data.getCalendarId();
87      if (data.getEventCategoryId() != null) {
88        categories = new String[] { new StringBuilder(basePath).append(CATEGORY_URI)
89            .append(data.getEventCategoryId())
90            .toString() };      
91        categoryId = data.getEventCategoryId();
92      }
93      location = data.getLocation();
94      this.priority = data.getPriority();
95  
96      End end;
97      if (data.getRepeatUntilDate() != null) {
98        java.util.Calendar tmp = java.util.Calendar.getInstance();
99        tmp.setTime(data.getRepeatUntilDate());      
100       end = new End(CalendarRestApi.RP_END_BYDATE, ISO8601.format(tmp));
101     } else if (data.getRepeatCount() > 0) {
102       end = new End(CalendarRestApi.RP_END_AFTER, String.valueOf(data.getRepeatCount()));
103     } else {
104       end = new End(CalendarRestApi.RP_END_NEVER, null);
105     }
106     
107     StringBuilder repeatByMonthDay = new StringBuilder();
108     if (data.getRepeatByMonthDay() != null) {
109       for (long d : data.getRepeatByMonthDay()) {
110         repeatByMonthDay.append(d).append(",");
111       }
112       if (repeatByMonthDay.length() > 0) {
113         repeatByMonthDay.deleteCharAt(repeatByMonthDay.length() - 1);
114       }      
115     }
116     
117     boolean isRepeat = (data.getRepeatType() != null && !CalendarEvent.RP_NOREPEAT.equals(data.getRepeatType()));    
118     repeat = new RepeatResource(isRepeat,
119                                 data.getRepeatType(),
120                                 (int)data.getRepeatInterval(),
121                                 StringUtils.join(data.getRepeatByDay(), ","),
122                                 repeatByMonthDay.toString(),
123                                 data.getExceptionIds(),
124                                 end);    
125     recurrenceId = data.getRecurrenceId();
126     if (data.getOriginalReference() != null) {
127       originalEvent = new StringBuilder(basePath).append(EVENT_URI)
128           .append(data.getOriginalReference())
129           .toString();
130     }
131     isOccur = isRepeat && (data.getIsExceptionOccurrence() == null || !data.getIsExceptionOccurrence());
132     if (data.getReminders() != null)
133       reminder = data.getReminders().toArray(new Reminder[] {});
134     
135     if (data.getAttachment() != null) {
136       List<String> atts = new LinkedList<String>();
137       
138       for (Attachment att : data.getAttachment()) {
139         AttachmentResource attRs = new AttachmentResource(att, basePath);
140         atts.add(attRs.getHref());
141       }
142       attachments = atts.toArray(new String[atts.size()]);
143     }
144     participants = data.getParticipant();
145     privacy = data.isPrivate() ? CalendarEvent.IS_PRIVATE : CalendarEvent.IS_PUBLIC;
146     availability = data.getEventState();
147   }
148 
149   public String getSubject() {
150     return subject;
151   }
152 
153   public void setSubject(String subject) {
154     this.subject = subject;
155   }
156 
157   public String getDescription() {
158     return description;
159   }
160 
161   public void setDescription(String description) {
162     this.description = description;
163   }
164 
165   public String getFrom() {
166     return from;
167   }
168 
169   public void setFrom(String from) {
170     this.from = from;
171   }
172 
173   public String getTo() {
174     return to;
175   }
176 
177   public void setTo(String to) {
178     this.to = to;
179   }
180 
181   public Serializable getCalendar() {
182     return calendar;
183   }
184 
185   public EventResource setCal(Serializable calendar) {
186     this.calendar = calendar;
187     return this;
188   }
189 
190   public Serializable[] getCategories() {
191     return categories;
192   }
193 
194   public void setCats(Serializable[] categories) {
195     this.categories = categories;
196   }
197 
198   public String getLocation() {
199     return location;
200   }
201 
202   public void setLocation(String location) {
203     this.location = location;
204   }
205 
206   public String getPriority() {
207     return priority;
208   }
209 
210   public void setPriority(String priority) {
211     this.priority = priority;
212   }
213 
214   public RepeatResource getRepeat() {
215     return repeat;
216   }
217 
218   public void setRepeat(RepeatResource repeat) {
219     this.repeat = repeat;
220   }
221 
222   public String getRecurrenceId() {
223     return recurrenceId;
224   }
225 
226   public void setRecurrenceId(String recurrenceId) {
227     this.recurrenceId = recurrenceId;
228   }
229 
230   public Serializable getOriginalEvent() {
231     return originalEvent;
232   }
233 
234   public void setOEvent(Serializable originalEvent) {
235     this.originalEvent = originalEvent;
236   }
237 
238   public Reminder[] getReminder() {
239     return reminder;
240   }
241 
242   public void setReminder(Reminder[] reminder) {
243     this.reminder = reminder;
244   }
245 
246   public Serializable[] getAttachments() {
247     return attachments;
248   }
249 
250   public void setAtts(Serializable[] attachments) {
251     this.attachments = attachments;
252   }
253 
254   public UploadResource[] getUploadResources() {
255     return uploadResources;
256   }
257 
258   public void setUploadResources(UploadResource[] uploadResources) {
259     this.uploadResources = uploadResources;
260   }
261 
262   public String[] getParticipants() {
263     return participants;
264   }
265 
266   public void setParticipants(String[] participants) {
267     this.participants = participants;
268   }
269 
270   public String getPrivacy() {
271     return privacy;
272   }
273 
274   public void setPrivacy(String privacy) {
275     this.privacy = privacy;
276   }
277 
278   public String getAvailability() {
279     return availability;
280   }
281 
282   public void setAvailability(String availability) {
283     this.availability = availability;
284   }
285 
286   public String getCategoryId() {
287     return categoryId;
288   }
289 
290   public void setCategoryId(String categoryId) {
291     this.categoryId = categoryId;
292   }
293 
294   public String getCalendarId() {
295     return calendarId;
296   }
297 
298   public void setCalendarId(String calendarId) {
299     this.calendarId = calendarId;
300   }
301 
302   public Boolean getIsOccur() {
303     return isOccur;
304   }
305 
306   public void setIsOccur(Boolean isOccur) {
307     isOccur = isOccur;
308   }
309 }