001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.calendar.lar;
022:
023: import com.liferay.portal.kernel.lar.PortletDataContext;
024: import com.liferay.portal.kernel.lar.PortletDataException;
025: import com.liferay.portal.kernel.lar.PortletDataHandler;
026: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
027: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
028: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
029: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portlet.calendar.model.CalEvent;
032: import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
033: import com.liferay.portlet.calendar.service.persistence.CalEventUtil;
034:
035: import com.thoughtworks.xstream.XStream;
036:
037: import java.util.Calendar;
038: import java.util.Date;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import javax.portlet.PortletPreferences;
043:
044: import org.dom4j.Document;
045: import org.dom4j.DocumentHelper;
046: import org.dom4j.Element;
047:
048: /**
049: * <a href="CalendarPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
050: * </a>
051: *
052: * @author Bruno Farache
053: *
054: */
055: public class CalendarPortletDataHandlerImpl implements
056: PortletDataHandler {
057:
058: public PortletPreferences deleteData(PortletDataContext context,
059: String portletId, PortletPreferences prefs)
060: throws PortletDataException {
061:
062: try {
063:
064: // Events
065:
066: if (!context.addPrimaryKey(
067: CalendarPortletDataHandlerImpl.class, "deleteData")) {
068:
069: CalEventLocalServiceUtil.deleteEvents(context
070: .getGroupId());
071: }
072: return null;
073: } catch (Exception e) {
074: throw new PortletDataException(e);
075: }
076: }
077:
078: public String exportData(PortletDataContext context,
079: String portletId, PortletPreferences prefs)
080: throws PortletDataException {
081:
082: try {
083: XStream xStream = new XStream();
084:
085: Document doc = DocumentHelper.createDocument();
086:
087: Element root = doc.addElement("calendar-data");
088:
089: root.addAttribute("group-id", String.valueOf(context
090: .getGroupId()));
091:
092: // Events
093:
094: List events = CalEventUtil.findByGroupId(context
095: .getGroupId());
096:
097: Iterator itr = events.iterator();
098:
099: while (itr.hasNext()) {
100: CalEvent event = (CalEvent) itr.next();
101:
102: if (context.addPrimaryKey(CalEvent.class, event
103: .getPrimaryKeyObj())) {
104:
105: itr.remove();
106: } else {
107: event.setUserUuid(event.getUserUuid());
108: }
109: }
110:
111: String xml = xStream.toXML(events);
112:
113: Element el = root.addElement("calendar-events");
114:
115: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
116:
117: el.content().add(tempDoc.getRootElement().createCopy());
118:
119: return doc.asXML();
120: } catch (Exception e) {
121: throw new PortletDataException(e);
122: }
123: }
124:
125: public PortletDataHandlerControl[] getExportControls()
126: throws PortletDataException {
127:
128: return new PortletDataHandlerControl[] { _events };
129: }
130:
131: public PortletDataHandlerControl[] getImportControls()
132: throws PortletDataException {
133:
134: return new PortletDataHandlerControl[] { _events };
135: }
136:
137: public PortletPreferences importData(PortletDataContext context,
138: String portletId, PortletPreferences prefs, String data)
139: throws PortletDataException {
140:
141: try {
142: XStream xStream = new XStream();
143:
144: Document doc = PortalUtil.readDocumentFromXML(data);
145:
146: Element root = doc.getRootElement();
147:
148: // Events
149:
150: Element el = root.element("calendar-events")
151: .element("list");
152:
153: Document tempDoc = DocumentHelper.createDocument();
154:
155: tempDoc.content().add(el.createCopy());
156:
157: List events = (List) xStream.fromXML(tempDoc.asXML());
158:
159: Iterator itr = events.iterator();
160:
161: while (itr.hasNext()) {
162: CalEvent event = (CalEvent) itr.next();
163:
164: importEvent(context, event);
165: }
166:
167: return null;
168: } catch (Exception e) {
169: throw new PortletDataException(e);
170: }
171: }
172:
173: protected void importEvent(PortletDataContext context,
174: CalEvent event) throws Exception {
175:
176: long userId = context.getUserId(event.getUserUuid());
177: long plid = context.getPlid();
178:
179: Date startDate = event.getStartDate();
180:
181: int startDateMonth = 0;
182: int startDateDay = 0;
183: int startDateYear = 0;
184: int startDateHour = 0;
185: int startDateMinute = 0;
186:
187: if (startDate != null) {
188: Calendar startCal = CalendarFactoryUtil.getCalendar();
189:
190: startCal.setTime(startDate);
191:
192: startDateMonth = startCal.get(Calendar.MONTH);
193: startDateDay = startCal.get(Calendar.DATE);
194: startDateYear = startCal.get(Calendar.YEAR);
195: startDateHour = startCal.get(Calendar.HOUR);
196: startDateMinute = startCal.get(Calendar.MINUTE);
197: }
198:
199: Date endDate = event.getEndDate();
200:
201: int endDateMonth = 0;
202: int endDateDay = 0;
203: int endDateYear = 0;
204:
205: if (endDate != null) {
206: Calendar endCal = CalendarFactoryUtil.getCalendar();
207:
208: endCal.setTime(endDate);
209:
210: endDateMonth = endCal.get(Calendar.MONTH);
211: endDateDay = endCal.get(Calendar.DATE);
212: endDateYear = endCal.get(Calendar.YEAR);
213: }
214:
215: boolean addCommunityPermissions = true;
216: boolean addGuestPermissions = true;
217:
218: CalEvent existingEvent = null;
219:
220: if (context.getDataStrategy().equals(
221: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
222:
223: existingEvent = CalEventUtil.fetchByUUID_G(event.getUuid(),
224: context.getGroupId());
225:
226: if (existingEvent == null) {
227: CalEventLocalServiceUtil.addEvent(event.getUuid(),
228: userId, plid, event.getTitle(), event
229: .getDescription(), startDateMonth,
230: startDateDay, startDateYear, startDateHour,
231: startDateMinute, endDateMonth, endDateDay,
232: endDateYear, event.getDurationHour(), event
233: .getDurationMinute(),
234: event.getAllDay(),
235: event.getTimeZoneSensitive(), event.getType(),
236: event.getRepeating(), event.getRecurrenceObj(),
237: event.getRemindBy(), event.getFirstReminder(),
238: event.getSecondReminder(),
239: addCommunityPermissions, addGuestPermissions);
240: } else {
241: CalEventLocalServiceUtil.updateEvent(userId,
242: existingEvent.getEventId(), event.getTitle(),
243: event.getDescription(), startDateMonth,
244: startDateDay, startDateYear, startDateHour,
245: startDateMinute, endDateMonth, endDateDay,
246: endDateYear, event.getDurationHour(), event
247: .getDurationMinute(),
248: event.getAllDay(),
249: event.getTimeZoneSensitive(), event.getType(),
250: event.getRepeating(), event.getRecurrenceObj(),
251: event.getRemindBy(), event.getFirstReminder(),
252: event.getSecondReminder());
253: }
254: } else {
255: CalEventLocalServiceUtil.addEvent(userId, plid, event
256: .getTitle(), event.getDescription(),
257: startDateMonth, startDateDay, startDateYear,
258: startDateHour, startDateMinute, endDateMonth,
259: endDateDay, endDateYear, event.getDurationHour(),
260: event.getDurationMinute(), event.getAllDay(), event
261: .getTimeZoneSensitive(), event.getType(),
262: event.getRepeating(), event.getRecurrenceObj(),
263: event.getRemindBy(), event.getFirstReminder(),
264: event.getSecondReminder(), addCommunityPermissions,
265: addGuestPermissions);
266: }
267: }
268:
269: private static final String _NAMESPACE = "calendar";
270:
271: private static final PortletDataHandlerBoolean _events = new PortletDataHandlerBoolean(
272: _NAMESPACE, "events", true, true);
273:
274: }
|