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.polls.lar;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.lar.PortletDataContext;
026: import com.liferay.portal.kernel.lar.PortletDataException;
027: import com.liferay.portal.kernel.lar.PortletDataHandler;
028: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
029: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
030: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
031: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.portlet.polls.DuplicateVoteException;
034: import com.liferay.portlet.polls.NoSuchChoiceException;
035: import com.liferay.portlet.polls.NoSuchQuestionException;
036: import com.liferay.portlet.polls.model.PollsChoice;
037: import com.liferay.portlet.polls.model.PollsQuestion;
038: import com.liferay.portlet.polls.model.PollsVote;
039: import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
040: import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
041: import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
042: import com.liferay.portlet.polls.service.persistence.PollsChoiceFinderUtil;
043: import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
044: import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
045: import com.liferay.portlet.polls.service.persistence.PollsVoteUtil;
046: import com.liferay.util.CollectionFactory;
047: import com.liferay.util.MapUtil;
048:
049: import com.thoughtworks.xstream.XStream;
050:
051: import java.util.ArrayList;
052: import java.util.Calendar;
053: import java.util.Date;
054: import java.util.Iterator;
055: import java.util.List;
056: import java.util.Map;
057:
058: import javax.portlet.PortletPreferences;
059:
060: import org.apache.commons.logging.Log;
061: import org.apache.commons.logging.LogFactory;
062:
063: import org.dom4j.Document;
064: import org.dom4j.DocumentHelper;
065: import org.dom4j.Element;
066:
067: /**
068: * <a href="PollsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
069: *
070: * @author Bruno Farache
071: *
072: */
073: public class PollsPortletDataHandlerImpl implements PortletDataHandler {
074:
075: public PortletPreferences deleteData(PortletDataContext context,
076: String portletId, PortletPreferences prefs)
077: throws PortletDataException {
078:
079: try {
080:
081: // Questions
082:
083: if (!context.addPrimaryKey(
084: PollsPortletDataHandlerImpl.class, "deleteData")) {
085:
086: PollsQuestionLocalServiceUtil.deleteQuestions(context
087: .getGroupId());
088: }
089:
090: return null;
091: } catch (Exception e) {
092: throw new PortletDataException(e);
093: }
094: }
095:
096: public String exportData(PortletDataContext context,
097: String portletId, PortletPreferences prefs)
098: throws PortletDataException {
099:
100: try {
101: XStream xStream = new XStream();
102:
103: Document doc = DocumentHelper.createDocument();
104:
105: Element root = doc.addElement("polls-data");
106:
107: root.addAttribute("group-id", String.valueOf(context
108: .getGroupId()));
109:
110: // Questions
111:
112: List questions = PollsQuestionUtil.findByGroupId(context
113: .getGroupId());
114:
115: List choices = new ArrayList();
116:
117: List votes = new ArrayList();
118:
119: Iterator itr = questions.iterator();
120:
121: while (itr.hasNext()) {
122: PollsQuestion question = (PollsQuestion) itr.next();
123:
124: if (context.addPrimaryKey(PollsQuestion.class, question
125: .getPrimaryKeyObj())) {
126:
127: itr.remove();
128: } else {
129: List questionChoices = PollsChoiceUtil
130: .findByQuestionId(question.getQuestionId());
131:
132: choices.addAll(questionChoices);
133:
134: if (context
135: .getBooleanParameter(_NAMESPACE, "votes")) {
136: question.setUserUuid(question.getUserUuid());
137:
138: List questionVotes = PollsVoteUtil
139: .findByQuestionId(question
140: .getQuestionId());
141:
142: votes.addAll(questionVotes);
143: }
144: }
145: }
146:
147: String xml = xStream.toXML(questions);
148:
149: Element el = root.addElement("poll-questions");
150:
151: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
152:
153: el.content().add(tempDoc.getRootElement().createCopy());
154:
155: // Choices
156:
157: itr = choices.iterator();
158:
159: while (itr.hasNext()) {
160: PollsChoice choice = (PollsChoice) itr.next();
161:
162: if (context.addPrimaryKey(PollsChoice.class, choice
163: .getPrimaryKeyObj())) {
164:
165: itr.remove();
166: }
167: }
168:
169: xml = xStream.toXML(choices);
170:
171: el = root.addElement("poll-choices");
172:
173: tempDoc = PortalUtil.readDocumentFromXML(xml);
174:
175: el.content().add(tempDoc.getRootElement().createCopy());
176:
177: // Votes
178:
179: itr = votes.iterator();
180:
181: while (itr.hasNext()) {
182: PollsVote vote = (PollsVote) itr.next();
183:
184: if (context.addPrimaryKey(PollsVote.class, vote
185: .getPrimaryKeyObj())) {
186:
187: itr.remove();
188: } else {
189: vote.setUserUuid(vote.getUserUuid());
190: }
191: }
192:
193: xml = xStream.toXML(votes);
194:
195: el = root.addElement("poll-votes");
196:
197: tempDoc = PortalUtil.readDocumentFromXML(xml);
198:
199: el.content().add(tempDoc.getRootElement().createCopy());
200:
201: return doc.asXML();
202: } catch (Exception e) {
203: throw new PortletDataException(e);
204: }
205: }
206:
207: public PortletDataHandlerControl[] getExportControls()
208: throws PortletDataException {
209:
210: return new PortletDataHandlerControl[] { _questions, _votes };
211: }
212:
213: public PortletDataHandlerControl[] getImportControls()
214: throws PortletDataException {
215:
216: return new PortletDataHandlerControl[] { _questions, _votes };
217: }
218:
219: public PortletPreferences importData(PortletDataContext context,
220: String portletId, PortletPreferences prefs, String data)
221: throws PortletDataException {
222:
223: try {
224: XStream xStream = new XStream();
225:
226: Document doc = PortalUtil.readDocumentFromXML(data);
227:
228: Element root = doc.getRootElement();
229:
230: // Questions
231:
232: Element el = root.element("poll-questions").element("list");
233:
234: Document tempDoc = DocumentHelper.createDocument();
235:
236: tempDoc.content().add(el.createCopy());
237:
238: Map questionPKs = CollectionFactory.getHashMap();
239:
240: List questions = (List) xStream.fromXML(tempDoc.asXML());
241:
242: Iterator itr = questions.iterator();
243:
244: while (itr.hasNext()) {
245: PollsQuestion question = (PollsQuestion) itr.next();
246:
247: importQuestion(context, questionPKs, question);
248: }
249:
250: // Choices
251:
252: el = root.element("poll-choices").element("list");
253:
254: tempDoc = DocumentHelper.createDocument();
255:
256: tempDoc.content().add(el.createCopy());
257:
258: Map choicePKs = CollectionFactory.getHashMap();
259:
260: List choices = (List) xStream.fromXML(tempDoc.asXML());
261:
262: itr = choices.iterator();
263:
264: while (itr.hasNext()) {
265: PollsChoice choice = (PollsChoice) itr.next();
266:
267: importChoice(context, questionPKs, choicePKs, choice);
268: }
269:
270: // Votes
271:
272: if (context.getBooleanParameter(_NAMESPACE, "votes")) {
273: el = root.element("poll-votes").element("list");
274:
275: tempDoc = DocumentHelper.createDocument();
276:
277: tempDoc.content().add(el.createCopy());
278:
279: List votes = (List) xStream.fromXML(tempDoc.asXML());
280:
281: itr = votes.iterator();
282:
283: while (itr.hasNext()) {
284: PollsVote vote = (PollsVote) itr.next();
285:
286: importVote(context, questionPKs, choicePKs, vote);
287: }
288: }
289:
290: return null;
291: } catch (Exception e) {
292: throw new PortletDataException(e);
293: }
294: }
295:
296: protected void importChoice(PortletDataContext context,
297: Map questionPKs, Map choicePKs, PollsChoice choice)
298: throws Exception {
299:
300: long questionId = MapUtil.getLong(questionPKs, choice
301: .getQuestionId(), choice.getQuestionId());
302:
303: PollsChoice existingChoice = null;
304:
305: try {
306: PollsQuestionUtil.findByPrimaryKey(questionId);
307:
308: if (context.getDataStrategy().equals(
309: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
310:
311: try {
312: existingChoice = PollsChoiceFinderUtil
313: .findByUuid_G(choice.getUuid(), context
314: .getGroupId());
315:
316: existingChoice = PollsChoiceLocalServiceUtil
317: .updateChoice(existingChoice.getChoiceId(),
318: questionId, choice.getName(),
319: choice.getDescription());
320: } catch (NoSuchChoiceException nsce) {
321: existingChoice = PollsChoiceLocalServiceUtil
322: .addChoice(choice.getUuid(), questionId,
323: choice.getName(), choice
324: .getDescription());
325: }
326: } else {
327: existingChoice = PollsChoiceLocalServiceUtil.addChoice(
328: questionId, choice.getName(), choice
329: .getDescription());
330: }
331:
332: choicePKs.put(choice.getPrimaryKeyObj(), existingChoice
333: .getPrimaryKeyObj());
334: } catch (NoSuchQuestionException nsqe) {
335: _log.error("Could not find the question for choice "
336: + choice.getChoiceId());
337: }
338: }
339:
340: protected void importQuestion(PortletDataContext context,
341: Map questionPKs, PollsQuestion question)
342: throws SystemException, PortalException {
343:
344: long userId = context.getUserId(question.getUserUuid());
345: long plid = context.getPlid();
346:
347: Date expirationDate = question.getExpirationDate();
348:
349: int expirationMonth = 0;
350: int expirationDay = 0;
351: int expirationYear = 0;
352: int expirationHour = 0;
353: int expirationMinute = 0;
354: boolean neverExpire = true;
355:
356: if (expirationDate != null) {
357: Calendar expirationCal = CalendarFactoryUtil.getCalendar();
358:
359: expirationCal.setTime(expirationDate);
360:
361: expirationMonth = expirationCal.get(Calendar.MONTH);
362: expirationDay = expirationCal.get(Calendar.DATE);
363: expirationYear = expirationCal.get(Calendar.YEAR);
364: expirationHour = expirationCal.get(Calendar.HOUR);
365: expirationMinute = expirationCal.get(Calendar.MINUTE);
366: neverExpire = false;
367: }
368:
369: boolean addCommunityPermissions = true;
370: boolean addGuestPermissions = true;
371:
372: PollsQuestion existingQuestion = null;
373:
374: if (context.getDataStrategy().equals(
375: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
376: existingQuestion = PollsQuestionUtil.fetchByUUID_G(question
377: .getUuid(), context.getGroupId());
378:
379: if (existingQuestion == null) {
380: existingQuestion = PollsQuestionLocalServiceUtil
381: .addQuestion(question.getUuid(), userId, plid,
382: question.getTitle(), question
383: .getDescription(),
384: expirationMonth, expirationDay,
385: expirationYear, expirationHour,
386: expirationMinute, neverExpire,
387: addCommunityPermissions,
388: addGuestPermissions);
389: } else {
390: existingQuestion = PollsQuestionLocalServiceUtil
391: .updateQuestion(userId, existingQuestion
392: .getQuestionId(), question.getTitle(),
393: question.getDescription(),
394: expirationMonth, expirationDay,
395: expirationYear, expirationHour,
396: expirationMinute, neverExpire);
397: }
398: } else {
399: existingQuestion = PollsQuestionLocalServiceUtil
400: .addQuestion(userId, plid, question.getTitle(),
401: question.getDescription(), expirationMonth,
402: expirationDay, expirationYear,
403: expirationHour, expirationMinute,
404: neverExpire, addCommunityPermissions,
405: addGuestPermissions);
406: }
407:
408: questionPKs.put(question.getPrimaryKeyObj(), existingQuestion
409: .getPrimaryKeyObj());
410: }
411:
412: protected void importVote(PortletDataContext context,
413: Map questionPKs, Map choicePKs, PollsVote vote)
414: throws Exception {
415:
416: long userId = context.getUserId(vote.getUserUuid());
417: long questionId = MapUtil.getLong(questionPKs, vote
418: .getQuestionId(), vote.getQuestionId());
419: long choiceId = MapUtil.getLong(choicePKs, vote.getChoiceId(),
420: vote.getChoiceId());
421:
422: try {
423: PollsQuestionUtil.findByPrimaryKey(questionId);
424: PollsChoiceUtil.findByPrimaryKey(choiceId);
425:
426: PollsVoteLocalServiceUtil.addVote(userId, questionId,
427: choiceId);
428: } catch (DuplicateVoteException dve) {
429: } catch (NoSuchQuestionException nsqe) {
430: _log.error("Could not find the question for vote "
431: + vote.getVoteId());
432: } catch (NoSuchChoiceException nsve) {
433: _log.error("Could not find the choice for vote "
434: + vote.getVoteId());
435: }
436: }
437:
438: private static final String _NAMESPACE = "polls";
439:
440: private static final PortletDataHandlerBoolean _questions = new PortletDataHandlerBoolean(
441: _NAMESPACE, "questions", true, true);
442:
443: private static final PortletDataHandlerBoolean _votes = new PortletDataHandlerBoolean(
444: _NAMESPACE, "votes");
445:
446: private static Log _log = LogFactory
447: .getLog(PollsPortletDataHandlerImpl.class);
448:
449: }
|