1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.lar;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
24  import com.liferay.portal.kernel.xml.Document;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.lar.PortletDataContext;
28  import com.liferay.portal.lar.PortletDataException;
29  import com.liferay.portal.lar.PortletDataHandler;
30  import com.liferay.portal.lar.PortletDataHandlerBoolean;
31  import com.liferay.portal.lar.PortletDataHandlerControl;
32  import com.liferay.portal.lar.PortletDataHandlerKeys;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.calendar.model.CalEvent;
35  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
36  import com.liferay.portlet.calendar.service.persistence.CalEventUtil;
37  
38  import java.util.Calendar;
39  import java.util.Date;
40  import java.util.List;
41  
42  import javax.portlet.PortletPreferences;
43  
44  /**
45   * <a href="CalendarPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
46   * </a>
47   *
48   * @author Bruno Farache
49   * @author Raymond Augé
50   *
51   */
52  public class CalendarPortletDataHandlerImpl implements PortletDataHandler {
53  
54      public PortletPreferences deleteData(
55              PortletDataContext context, String portletId,
56              PortletPreferences prefs)
57          throws PortletDataException {
58  
59          try {
60              if (!context.addPrimaryKey(
61                      CalendarPortletDataHandlerImpl.class, "deleteData")) {
62  
63                  CalEventLocalServiceUtil.deleteEvents(context.getGroupId());
64              }
65              return null;
66          }
67          catch (Exception e) {
68              throw new PortletDataException(e);
69          }
70      }
71  
72      public String exportData(
73              PortletDataContext context, String portletId,
74              PortletPreferences prefs)
75          throws PortletDataException {
76  
77          try {
78              Document doc = SAXReaderUtil.createDocument();
79  
80              Element root = doc.addElement("calendar-data");
81  
82              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
83  
84              List<CalEvent> events = CalEventUtil.findByGroupId(
85                  context.getGroupId());
86  
87              for (CalEvent event : events) {
88                  exportEvent(context, root, event);
89              }
90  
91              return doc.formattedString();
92          }
93          catch (Exception e) {
94              throw new PortletDataException(e);
95          }
96      }
97  
98      public PortletDataHandlerControl[] getExportControls() {
99          return new PortletDataHandlerControl[] {_events};
100     }
101 
102     public PortletDataHandlerControl[] getImportControls() {
103         return new PortletDataHandlerControl[] {_events};
104     }
105 
106     public PortletPreferences importData(
107             PortletDataContext context, String portletId,
108             PortletPreferences prefs, String data)
109         throws PortletDataException {
110 
111         try {
112             Document doc = SAXReaderUtil.read(data);
113 
114             Element root = doc.getRootElement();
115 
116             List<Element> eventsEl = root.elements("event");
117 
118             for (Element eventEl : eventsEl) {
119                 String path = eventEl.attributeValue("path");
120 
121                 if (!context.isPathNotProcessed(path)) {
122                     continue;
123                 }
124 
125                 CalEvent event = (CalEvent)context.getZipEntryAsObject(path);
126 
127                 importEvent(context, event);
128             }
129 
130             return null;
131         }
132         catch (Exception e) {
133             throw new PortletDataException(e);
134         }
135     }
136 
137     public boolean isPublishToLiveByDefault() {
138         return false;
139     }
140 
141     protected void exportEvent(
142             PortletDataContext context, Element root, CalEvent event)
143         throws SystemException {
144 
145         if (!context.isWithinDateRange(event.getModifiedDate())) {
146             return;
147         }
148 
149         String path = getEventPath(context, event);
150 
151         if (!context.isPathNotProcessed(path)) {
152             return;
153         }
154 
155         Element eventEl = root.addElement("event");
156 
157         eventEl.addAttribute("path", path);
158 
159         event.setUserUuid(event.getUserUuid());
160 
161         context.addZipEntry(path, event);
162     }
163 
164     protected String getEventPath(PortletDataContext context, CalEvent event) {
165         StringBuilder sb = new StringBuilder();
166 
167         sb.append(context.getPortletPath(PortletKeys.CALENDAR));
168         sb.append("/events/");
169         sb.append(event.getEventId());
170         sb.append(".xml");
171 
172         return sb.toString();
173     }
174 
175     protected void importEvent(PortletDataContext context, CalEvent event)
176         throws Exception {
177 
178         long userId = context.getUserId(event.getUserUuid());
179         long plid = context.getPlid();
180 
181         Date startDate = event.getStartDate();
182 
183         int startDateMonth = 0;
184         int startDateDay = 0;
185         int startDateYear = 0;
186         int startDateHour = 0;
187         int startDateMinute = 0;
188 
189         if (startDate != null) {
190             Calendar startCal = CalendarFactoryUtil.getCalendar();
191 
192             startCal.setTime(startDate);
193 
194             startDateMonth = startCal.get(Calendar.MONTH);
195             startDateDay = startCal.get(Calendar.DATE);
196             startDateYear = startCal.get(Calendar.YEAR);
197             startDateHour = startCal.get(Calendar.HOUR);
198             startDateMinute = startCal.get(Calendar.MINUTE);
199 
200             if (startCal.get(Calendar.AM_PM) == Calendar.PM) {
201                 startDateHour += 12;
202             }
203         }
204 
205         Date endDate = event.getEndDate();
206 
207         int endDateMonth = 0;
208         int endDateDay = 0;
209         int endDateYear = 0;
210 
211         if (endDate != null) {
212             Calendar endCal = CalendarFactoryUtil.getCalendar();
213 
214             endCal.setTime(endDate);
215 
216             endDateMonth = endCal.get(Calendar.MONTH);
217             endDateDay = endCal.get(Calendar.DATE);
218             endDateYear = endCal.get(Calendar.YEAR);
219         }
220 
221         boolean addCommunityPermissions = true;
222         boolean addGuestPermissions = true;
223 
224         CalEvent existingEvent = null;
225 
226         if (context.getDataStrategy().equals(
227                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
228 
229             existingEvent = CalEventUtil.fetchByUUID_G(
230                 event.getUuid(), context.getGroupId());
231 
232             if (existingEvent == null) {
233                 CalEventLocalServiceUtil.addEvent(
234                     event.getUuid(), userId, plid, event.getTitle(),
235                     event.getDescription(), startDateMonth, startDateDay,
236                     startDateYear, startDateHour, startDateMinute, endDateMonth,
237                     endDateDay, endDateYear, event.getDurationHour(),
238                     event.getDurationMinute(), event.getAllDay(),
239                     event.getTimeZoneSensitive(), event.getType(),
240                     event.getRepeating(), event.getRecurrenceObj(),
241                     event.getRemindBy(), event.getFirstReminder(),
242                     event.getSecondReminder(), addCommunityPermissions,
243                     addGuestPermissions);
244             }
245             else {
246                 CalEventLocalServiceUtil.updateEvent(
247                     userId, existingEvent.getEventId(), event.getTitle(),
248                     event.getDescription(), startDateMonth, startDateDay,
249                     startDateYear, startDateHour, startDateMinute, endDateMonth,
250                     endDateDay, endDateYear, event.getDurationHour(),
251                     event.getDurationMinute(), event.getAllDay(),
252                     event.getTimeZoneSensitive(), event.getType(),
253                     event.getRepeating(), event.getRecurrenceObj(),
254                     event.getRemindBy(), event.getFirstReminder(),
255                     event.getSecondReminder());
256             }
257         }
258         else {
259             CalEventLocalServiceUtil.addEvent(
260                 userId, plid, event.getTitle(), event.getDescription(),
261                 startDateMonth, startDateDay, startDateYear, startDateHour,
262                 startDateMinute, endDateMonth, endDateDay, endDateYear,
263                 event.getDurationHour(), event.getDurationMinute(),
264                 event.getAllDay(), event.getTimeZoneSensitive(),
265                 event.getType(), event.getRepeating(), event.getRecurrenceObj(),
266                 event.getRemindBy(), event.getFirstReminder(),
267                 event.getSecondReminder(), addCommunityPermissions,
268                 addGuestPermissions);
269         }
270     }
271 
272     private static final String _NAMESPACE = "calendar";
273 
274     private static final PortletDataHandlerBoolean _events =
275         new PortletDataHandlerBoolean(_NAMESPACE, "events", true, true);
276 
277 }