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