001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.calendar.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
023    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.calendar.model.CalEvent;
031    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
032    import com.liferay.portlet.calendar.service.persistence.CalEventUtil;
033    
034    import java.util.Calendar;
035    import java.util.Date;
036    import java.util.List;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Bruno Farache
042     * @author Raymond Augé
043     */
044    public class CalendarPortletDataHandlerImpl extends BasePortletDataHandler {
045    
046            public PortletDataHandlerControl[] getExportControls() {
047                    return new PortletDataHandlerControl[] {
048                            _events, _categories, _comments, _tags
049                    };
050            }
051    
052            public PortletDataHandlerControl[] getImportControls() {
053                    return new PortletDataHandlerControl[] {
054                            _events, _categories, _comments, _tags
055                    };
056            }
057    
058            public boolean isPublishToLiveByDefault() {
059                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
060            }
061    
062            protected PortletPreferences doDeleteData(
063                            PortletDataContext context, String portletId,
064                            PortletPreferences preferences)
065                    throws Exception {
066    
067                    if (!context.addPrimaryKey(
068                                    CalendarPortletDataHandlerImpl.class, "deleteData")) {
069    
070                            CalEventLocalServiceUtil.deleteEvents(context.getScopeGroupId());
071                    }
072    
073                    return null;
074            }
075    
076            protected String doExportData(
077                            PortletDataContext context, String portletId,
078                            PortletPreferences preferences)
079                    throws Exception {
080    
081                    context.addPermissions(
082                            "com.liferay.portlet.calendar", context.getScopeGroupId());
083    
084                    Document document = SAXReaderUtil.createDocument();
085    
086                    Element rootElement = document.addElement("calendar-data");
087    
088                    rootElement.addAttribute(
089                            "group-id", String.valueOf(context.getScopeGroupId()));
090    
091                    List<CalEvent> events = CalEventUtil.findByGroupId(
092                            context.getScopeGroupId());
093    
094                    for (CalEvent event : events) {
095                            exportEvent(context, rootElement, event);
096                    }
097    
098                    return document.formattedString();
099            }
100    
101            protected PortletPreferences doImportData(
102                            PortletDataContext context, String portletId,
103                            PortletPreferences preferences, String data)
104                    throws Exception {
105    
106                    context.importPermissions(
107                            "com.liferay.portlet.calendar", context.getSourceGroupId(),
108                            context.getScopeGroupId());
109    
110                    Document document = SAXReaderUtil.read(data);
111    
112                    Element rootElement = document.getRootElement();
113    
114                    for (Element eventElement : rootElement.elements("event")) {
115                            String path = eventElement.attributeValue("path");
116    
117                            if (!context.isPathNotProcessed(path)) {
118                                    continue;
119                            }
120    
121                            CalEvent event = (CalEvent)context.getZipEntryAsObject(path);
122    
123                            importEvent(context, event);
124                    }
125    
126                    return null;
127            }
128    
129            protected void exportEvent(
130                            PortletDataContext context, Element rootElement, CalEvent event)
131                    throws PortalException, SystemException {
132    
133                    if (!context.isWithinDateRange(event.getModifiedDate())) {
134                            return;
135                    }
136    
137                    String path = getEventPath(context, event);
138    
139                    if (!context.isPathNotProcessed(path)) {
140                            return;
141                    }
142    
143                    Element eventElement = rootElement.addElement("event");
144    
145                    eventElement.addAttribute("path", path);
146    
147                    event.setUserUuid(event.getUserUuid());
148    
149                    context.addPermissions(CalEvent.class, event.getEventId());
150    
151                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
152                            context.addAssetCategories(CalEvent.class, event.getEventId());
153                    }
154    
155                    if (context.getBooleanParameter(_NAMESPACE, "comments")) {
156                            context.addComments(CalEvent.class, event.getEventId());
157                    }
158    
159                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
160                            context.addAssetTags(CalEvent.class, event.getEventId());
161                    }
162    
163                    context.addZipEntry(path, event);
164            }
165    
166            protected String getEventPath(PortletDataContext context, CalEvent event) {
167                    StringBundler sb = new StringBundler(4);
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    
182                    Date startDate = event.getStartDate();
183    
184                    int startDateMonth = 0;
185                    int startDateDay = 0;
186                    int startDateYear = 0;
187                    int startDateHour = 0;
188                    int startDateMinute = 0;
189    
190                    if (startDate != null) {
191                            Calendar startCal = CalendarFactoryUtil.getCalendar();
192    
193                            startCal.setTime(startDate);
194    
195                            startDateMonth = startCal.get(Calendar.MONTH);
196                            startDateDay = startCal.get(Calendar.DATE);
197                            startDateYear = startCal.get(Calendar.YEAR);
198                            startDateHour = startCal.get(Calendar.HOUR);
199                            startDateMinute = startCal.get(Calendar.MINUTE);
200    
201                            if (startCal.get(Calendar.AM_PM) == Calendar.PM) {
202                                    startDateHour += 12;
203                            }
204                    }
205    
206                    Date endDate = event.getEndDate();
207    
208                    int endDateMonth = 0;
209                    int endDateDay = 0;
210                    int endDateYear = 0;
211    
212                    if (endDate != null) {
213                            Calendar endCal = CalendarFactoryUtil.getCalendar();
214    
215                            endCal.setTime(endDate);
216    
217                            endDateMonth = endCal.get(Calendar.MONTH);
218                            endDateDay = endCal.get(Calendar.DATE);
219                            endDateYear = endCal.get(Calendar.YEAR);
220                    }
221    
222                    long[] assetCategoryIds = null;
223                    String[] assetTagNames = null;
224    
225                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
226                            assetCategoryIds = context.getAssetCategoryIds(
227                                    CalEvent.class, event.getEventId());
228                    }
229    
230                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
231                            assetTagNames = context.getAssetTagNames(
232                                    CalEvent.class, event.getEventId());
233                    }
234    
235                    ServiceContext serviceContext = new ServiceContext();
236    
237                    serviceContext.setAddCommunityPermissions(true);
238                    serviceContext.setAddGuestPermissions(true);
239                    serviceContext.setAssetCategoryIds(assetCategoryIds);
240                    serviceContext.setAssetTagNames(assetTagNames);
241                    serviceContext.setCreateDate(event.getCreateDate());
242                    serviceContext.setModifiedDate(event.getModifiedDate());
243                    serviceContext.setScopeGroupId(context.getScopeGroupId());
244    
245                    CalEvent importedEvent = null;
246    
247                    if (context.isDataStrategyMirror()) {
248                            CalEvent existingEvent = CalEventUtil.fetchByUUID_G(
249                                    event.getUuid(), context.getScopeGroupId());
250    
251                            if (existingEvent == null) {
252                                    serviceContext.setUuid(event.getUuid());
253    
254                                    importedEvent = CalEventLocalServiceUtil.addEvent(
255                                            userId, event.getTitle(), event.getDescription(),
256                                            startDateMonth, startDateDay, startDateYear, startDateHour,
257                                            startDateMinute, endDateMonth, endDateDay, endDateYear,
258                                            event.getDurationHour(), event.getDurationMinute(),
259                                            event.getAllDay(), event.getTimeZoneSensitive(),
260                                            event.getType(), event.getRepeating(),
261                                            event.getRecurrenceObj(), event.getRemindBy(),
262                                            event.getFirstReminder(), event.getSecondReminder(),
263                                            serviceContext);
264                            }
265                            else {
266                                    importedEvent = CalEventLocalServiceUtil.updateEvent(
267                                            userId, existingEvent.getEventId(), event.getTitle(),
268                                            event.getDescription(), startDateMonth, startDateDay,
269                                            startDateYear, startDateHour, startDateMinute, endDateMonth,
270                                            endDateDay, endDateYear, event.getDurationHour(),
271                                            event.getDurationMinute(), event.getAllDay(),
272                                            event.getTimeZoneSensitive(), event.getType(),
273                                            event.getRepeating(), event.getRecurrenceObj(),
274                                            event.getRemindBy(), event.getFirstReminder(),
275                                            event.getSecondReminder(), serviceContext);
276                            }
277                    }
278                    else {
279                            importedEvent = CalEventLocalServiceUtil.addEvent(
280                                    userId, event.getTitle(), event.getDescription(),
281                                    startDateMonth, startDateDay, startDateYear, startDateHour,
282                                    startDateMinute, endDateMonth, endDateDay, endDateYear,
283                                    event.getDurationHour(), event.getDurationMinute(),
284                                    event.getAllDay(), event.getTimeZoneSensitive(),
285                                    event.getType(), event.getRepeating(), event.getRecurrenceObj(),
286                                    event.getRemindBy(), event.getFirstReminder(),
287                                    event.getSecondReminder(), serviceContext);
288                    }
289    
290                    context.importPermissions(
291                            CalEvent.class, event.getEventId(), importedEvent.getEventId());
292    
293                    if (context.getBooleanParameter(_NAMESPACE, "comments")) {
294                            context.importComments(
295                                    CalEvent.class, event.getEventId(), importedEvent.getEventId(),
296                                    context.getScopeGroupId());
297                    }
298            }
299    
300            private static final String _NAMESPACE = "calendar";
301    
302            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
303    
304            private static PortletDataHandlerBoolean _categories =
305                    new PortletDataHandlerBoolean(_NAMESPACE, "categories");
306    
307            private static PortletDataHandlerBoolean _comments =
308                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
309    
310            private static PortletDataHandlerBoolean _events =
311                    new PortletDataHandlerBoolean(_NAMESPACE, "events", true, true);
312    
313            private static PortletDataHandlerBoolean _tags =
314                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
315    
316    }