001
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.LocaleUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.TimeZoneUtil;
027 import com.liferay.portal.kernel.xml.Document;
028 import com.liferay.portal.kernel.xml.Element;
029 import com.liferay.portal.kernel.xml.SAXReaderUtil;
030 import com.liferay.portal.model.User;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portal.service.UserLocalServiceUtil;
033 import com.liferay.portal.util.PortletKeys;
034 import com.liferay.portlet.calendar.model.CalEvent;
035 import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
036 import com.liferay.portlet.calendar.service.persistence.CalEventUtil;
037
038 import java.util.Calendar;
039 import java.util.Date;
040 import java.util.List;
041 import java.util.Locale;
042 import java.util.TimeZone;
043
044 import javax.portlet.PortletPreferences;
045
046
050 public class CalendarPortletDataHandlerImpl extends BasePortletDataHandler {
051
052 @Override
053 public PortletDataHandlerControl[] getExportControls() {
054 return new PortletDataHandlerControl[] {
055 _events
056 };
057 }
058
059 @Override
060 public PortletDataHandlerControl[] getExportMetadataControls() {
061 return new PortletDataHandlerControl[] {
062 new PortletDataHandlerBoolean(
063 _NAMESPACE, "events", true, _metadataControls)
064 };
065 }
066
067 @Override
068 public PortletDataHandlerControl[] getImportControls() {
069 return new PortletDataHandlerControl[] {
070 _events
071 };
072 }
073
074 @Override
075 public PortletDataHandlerControl[] getImportMetadataControls() {
076 return new PortletDataHandlerControl[] {
077 new PortletDataHandlerBoolean(
078 _NAMESPACE, "events", true, _metadataControls)
079 };
080 }
081
082 @Override
083 public boolean isAlwaysExportable() {
084 return _ALWAYS_EXPORTABLE;
085 }
086
087 @Override
088 public boolean isPublishToLiveByDefault() {
089 return _PUBLISH_TO_LIVE_BY_DEFAULT;
090 }
091
092 @Override
093 protected PortletPreferences doDeleteData(
094 PortletDataContext portletDataContext, String portletId,
095 PortletPreferences portletPreferences)
096 throws Exception {
097
098 if (!portletDataContext.addPrimaryKey(
099 CalendarPortletDataHandlerImpl.class, "deleteData")) {
100
101 CalEventLocalServiceUtil.deleteEvents(
102 portletDataContext.getScopeGroupId());
103 }
104
105 return null;
106 }
107
108 @Override
109 protected String doExportData(
110 PortletDataContext portletDataContext, String portletId,
111 PortletPreferences portletPreferences)
112 throws Exception {
113
114 portletDataContext.addPermissions(
115 "com.liferay.portlet.calendar",
116 portletDataContext.getScopeGroupId());
117
118 Document document = SAXReaderUtil.createDocument();
119
120 Element rootElement = document.addElement("calendar-data");
121
122 rootElement.addAttribute(
123 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
124
125 List<CalEvent> events = CalEventUtil.findByGroupId(
126 portletDataContext.getScopeGroupId());
127
128 for (CalEvent event : events) {
129 exportEvent(portletDataContext, rootElement, event);
130 }
131
132 return document.formattedString();
133 }
134
135 @Override
136 protected PortletPreferences doImportData(
137 PortletDataContext portletDataContext, String portletId,
138 PortletPreferences portletPreferences, String data)
139 throws Exception {
140
141 portletDataContext.importPermissions(
142 "com.liferay.portlet.calendar",
143 portletDataContext.getSourceGroupId(),
144 portletDataContext.getScopeGroupId());
145
146 Document document = SAXReaderUtil.read(data);
147
148 Element rootElement = document.getRootElement();
149
150 for (Element eventElement : rootElement.elements("event")) {
151 String path = eventElement.attributeValue("path");
152
153 if (!portletDataContext.isPathNotProcessed(path)) {
154 continue;
155 }
156
157 CalEvent event = (CalEvent)portletDataContext.getZipEntryAsObject(
158 path);
159
160 importEvent(portletDataContext, eventElement, event);
161 }
162
163 return null;
164 }
165
166 protected void exportEvent(
167 PortletDataContext portletDataContext, Element rootElement,
168 CalEvent event)
169 throws PortalException, SystemException {
170
171 if (!portletDataContext.isWithinDateRange(event.getModifiedDate())) {
172 return;
173 }
174
175 String path = getEventPath(portletDataContext, event);
176
177 if (!portletDataContext.isPathNotProcessed(path)) {
178 return;
179 }
180
181 Element eventElement = rootElement.addElement("event");
182
183 portletDataContext.addClassedModel(
184 eventElement, path, event, _NAMESPACE);
185 }
186
187 protected String getEventPath(
188 PortletDataContext portletDataContext, CalEvent event) {
189
190 StringBundler sb = new StringBundler(4);
191
192 sb.append(portletDataContext.getPortletPath(PortletKeys.CALENDAR));
193 sb.append("/events/");
194 sb.append(event.getEventId());
195 sb.append(".xml");
196
197 return sb.toString();
198 }
199
200 protected void importEvent(
201 PortletDataContext portletDataContext, Element eventElement,
202 CalEvent event)
203 throws Exception {
204
205 long userId = portletDataContext.getUserId(event.getUserUuid());
206
207 Date startDate = event.getStartDate();
208
209 int startDateMonth = 0;
210 int startDateDay = 0;
211 int startDateYear = 0;
212 int startDateHour = 0;
213 int startDateMinute = 0;
214
215 if (startDate != null) {
216 Locale locale = null;
217 TimeZone timeZone = null;
218
219 if (event.getTimeZoneSensitive()) {
220 User user = UserLocalServiceUtil.getUser(userId);
221
222 locale = user.getLocale();
223 timeZone = user.getTimeZone();
224 }
225 else {
226 locale = LocaleUtil.getDefault();
227 timeZone = TimeZoneUtil.getDefault();
228 }
229
230 Calendar startCal = CalendarFactoryUtil.getCalendar(
231 timeZone, locale);
232
233 startCal.setTime(startDate);
234
235 startDateMonth = startCal.get(Calendar.MONTH);
236 startDateDay = startCal.get(Calendar.DATE);
237 startDateYear = startCal.get(Calendar.YEAR);
238 startDateHour = startCal.get(Calendar.HOUR);
239 startDateMinute = startCal.get(Calendar.MINUTE);
240
241 if (startCal.get(Calendar.AM_PM) == Calendar.PM) {
242 startDateHour += 12;
243 }
244 }
245
246 ServiceContext serviceContext = portletDataContext.createServiceContext(
247 eventElement, event, _NAMESPACE);
248
249 CalEvent importedEvent = null;
250
251 if (portletDataContext.isDataStrategyMirror()) {
252 CalEvent existingEvent = CalEventUtil.fetchByUUID_G(
253 event.getUuid(), portletDataContext.getScopeGroupId());
254
255 if (existingEvent == null) {
256 serviceContext.setUuid(event.getUuid());
257
258 importedEvent = CalEventLocalServiceUtil.addEvent(
259 userId, event.getTitle(), event.getDescription(),
260 event.getLocation(), startDateMonth, startDateDay,
261 startDateYear, startDateHour, startDateMinute,
262 event.getDurationHour(), event.getDurationMinute(),
263 event.isAllDay(), event.isTimeZoneSensitive(),
264 event.getType(), event.getRepeating(),
265 event.getRecurrenceObj(), event.getRemindBy(),
266 event.getFirstReminder(), event.getSecondReminder(),
267 serviceContext);
268 }
269 else {
270 importedEvent = CalEventLocalServiceUtil.updateEvent(
271 userId, existingEvent.getEventId(), event.getTitle(),
272 event.getDescription(), event.getLocation(), startDateMonth,
273 startDateDay, startDateYear, startDateHour, startDateMinute,
274 event.getDurationHour(), event.getDurationMinute(),
275 event.isAllDay(), event.isTimeZoneSensitive(),
276 event.getType(), event.getRepeating(),
277 event.getRecurrenceObj(), event.getRemindBy(),
278 event.getFirstReminder(), event.getSecondReminder(),
279 serviceContext);
280 }
281 }
282 else {
283 importedEvent = CalEventLocalServiceUtil.addEvent(
284 userId, event.getTitle(), event.getDescription(),
285 event.getLocation(), startDateMonth, startDateDay,
286 startDateYear, startDateHour, startDateMinute,
287 event.getDurationHour(), event.getDurationMinute(),
288 event.isAllDay(), event.isTimeZoneSensitive(), event.getType(),
289 event.getRepeating(), event.getRecurrenceObj(),
290 event.getRemindBy(), event.getFirstReminder(),
291 event.getSecondReminder(), serviceContext);
292 }
293
294 portletDataContext.importClassedModel(event, importedEvent, _NAMESPACE);
295 }
296
297 private static final boolean _ALWAYS_EXPORTABLE = true;
298
299 private static final String _NAMESPACE = "calendar";
300
301 private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
302
303 private static PortletDataHandlerBoolean _events =
304 new PortletDataHandlerBoolean(_NAMESPACE, "events", true, true);
305 private static PortletDataHandlerControl[] _metadataControls =
306 new PortletDataHandlerControl[] {
307 new PortletDataHandlerBoolean(_NAMESPACE, "categories"),
308 new PortletDataHandlerBoolean(_NAMESPACE, "comments"),
309 new PortletDataHandlerBoolean(_NAMESPACE, "ratings"),
310 new PortletDataHandlerBoolean(_NAMESPACE, "tags")
311 };
312
313 }