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.messageboards.lar;
022:
023: import com.liferay.documentlibrary.NoSuchDirectoryException;
024: import com.liferay.documentlibrary.service.DLServiceUtil;
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.model.User;
032: import com.liferay.portal.model.impl.CompanyImpl;
033: import com.liferay.portal.service.persistence.UserUtil;
034: import com.liferay.portal.theme.ThemeDisplay;
035: import com.liferay.portal.util.PortalUtil;
036: import com.liferay.portlet.messageboards.NoSuchCategoryException;
037: import com.liferay.portlet.messageboards.NoSuchMessageException;
038: import com.liferay.portlet.messageboards.NoSuchThreadException;
039: import com.liferay.portlet.messageboards.model.MBBan;
040: import com.liferay.portlet.messageboards.model.MBCategory;
041: import com.liferay.portlet.messageboards.model.MBMessage;
042: import com.liferay.portlet.messageboards.model.MBMessageFlag;
043: import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
044: import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
045: import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
046: import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
047: import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
048: import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
049: import com.liferay.portlet.messageboards.service.persistence.MBBanUtil;
050: import com.liferay.portlet.messageboards.service.persistence.MBCategoryUtil;
051: import com.liferay.portlet.messageboards.service.persistence.MBMessageFinderUtil;
052: import com.liferay.portlet.messageboards.service.persistence.MBMessageFlagUtil;
053: import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
054: import com.liferay.portlet.messageboards.service.persistence.MBThreadUtil;
055: import com.liferay.util.CollectionFactory;
056: import com.liferay.util.MapUtil;
057:
058: import com.thoughtworks.xstream.XStream;
059:
060: import java.util.ArrayList;
061: import java.util.Iterator;
062: import java.util.List;
063: import java.util.Map;
064:
065: import javax.portlet.PortletPreferences;
066:
067: import org.apache.commons.logging.Log;
068: import org.apache.commons.logging.LogFactory;
069:
070: import org.dom4j.Document;
071: import org.dom4j.DocumentHelper;
072: import org.dom4j.Element;
073:
074: /**
075: * <a href="MBPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
076: *
077: * @author Bruno Farache
078: *
079: */
080: public class MBPortletDataHandlerImpl implements PortletDataHandler {
081:
082: public PortletPreferences deleteData(PortletDataContext context,
083: String portletId, PortletPreferences prefs)
084: throws PortletDataException {
085:
086: try {
087:
088: // Categories
089:
090: if (!context.addPrimaryKey(MBPortletDataHandlerImpl.class,
091: "deleteData")) {
092:
093: MBCategoryLocalServiceUtil.deleteCategories(context
094: .getGroupId());
095: }
096:
097: return null;
098: } catch (Exception e) {
099: throw new PortletDataException(e);
100: }
101: }
102:
103: public String exportData(PortletDataContext context,
104: String portletId, PortletPreferences prefs)
105: throws PortletDataException {
106:
107: try {
108: XStream xStream = new XStream();
109:
110: Document doc = DocumentHelper.createDocument();
111:
112: Element root = doc.addElement("message-boards-data");
113:
114: root.addAttribute("group-id", String.valueOf(context
115: .getGroupId()));
116:
117: // Categories
118:
119: List categories = MBCategoryUtil.findByGroupId(context
120: .getGroupId());
121:
122: List messages = new ArrayList();
123:
124: Iterator itr = categories.iterator();
125:
126: while (itr.hasNext()) {
127: MBCategory category = (MBCategory) itr.next();
128:
129: if (context.addPrimaryKey(MBCategory.class, category
130: .getPrimaryKeyObj())) {
131:
132: itr.remove();
133: } else {
134: category.setUserUuid(category.getUserUuid());
135:
136: List categoryMessages = MBMessageUtil
137: .findByCategoryId(category.getCategoryId());
138:
139: messages.addAll(categoryMessages);
140: }
141: }
142:
143: String xml = xStream.toXML(categories);
144:
145: Element el = root.addElement("message-board-categories");
146:
147: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
148:
149: el.content().add(tempDoc.getRootElement().createCopy());
150:
151: // Messages
152:
153: List flags = new ArrayList();
154:
155: itr = messages.iterator();
156:
157: while (itr.hasNext()) {
158: MBMessage message = (MBMessage) itr.next();
159:
160: if (context.addPrimaryKey(MBMessage.class, message
161: .getPrimaryKeyObj())) {
162:
163: itr.remove();
164: } else {
165: message.setUserUuid(message.getUserUuid());
166: message.setPriority(message.getPriority());
167:
168: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
169: context.addTagsEntries(MBMessage.class, message
170: .getPrimaryKeyObj());
171: }
172:
173: // Attachments
174:
175: if (context.getBooleanParameter(_NAMESPACE,
176: "attachments")
177: && message.isAttachments()) {
178:
179: String[] fileNames = null;
180:
181: try {
182: fileNames = DLServiceUtil.getFileNames(
183: context.getCompanyId(),
184: CompanyImpl.SYSTEM, message
185: .getAttachmentsDir());
186:
187: for (int i = 0; i < fileNames.length; i++) {
188: byte[] byteArray = DLServiceUtil
189: .getFile(
190: context.getCompanyId(),
191: CompanyImpl.SYSTEM,
192: fileNames[i]);
193:
194: context.getZipWriter().addEntry(
195: fileNames[i], byteArray);
196: }
197:
198: message.setAttachmentsDir(message
199: .getAttachmentsDir());
200: } catch (NoSuchDirectoryException nsde) {
201: }
202: }
203:
204: if (context
205: .getBooleanParameter(_NAMESPACE, "flags")) {
206: List messageFlags = MBMessageFlagUtil
207: .findByMessageId(message.getMessageId());
208:
209: flags.addAll(messageFlags);
210: }
211: }
212: }
213:
214: xml = xStream.toXML(messages);
215:
216: el = root.addElement("message-board-messages");
217:
218: tempDoc = PortalUtil.readDocumentFromXML(xml);
219:
220: el.content().add(tempDoc.getRootElement().createCopy());
221:
222: // Flags
223:
224: itr = flags.iterator();
225:
226: while (itr.hasNext()) {
227: MBMessageFlag flag = (MBMessageFlag) itr.next();
228:
229: if (context.addPrimaryKey(MBMessageFlag.class, flag
230: .getPrimaryKeyObj())) {
231:
232: itr.remove();
233: } else {
234: flag.setUserUuid(flag.getUserUuid());
235: }
236: }
237:
238: xml = xStream.toXML(flags);
239:
240: el = root.addElement("message-board-flags");
241:
242: tempDoc = PortalUtil.readDocumentFromXML(xml);
243:
244: el.content().add(tempDoc.getRootElement().createCopy());
245:
246: // Bans
247:
248: List bans = new ArrayList();
249:
250: if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
251: bans = MBBanUtil.findByGroupId(context.getGroupId());
252:
253: itr = bans.iterator();
254:
255: while (itr.hasNext()) {
256: MBBan ban = (MBBan) itr.next();
257:
258: if (context.addPrimaryKey(MBBan.class, ban
259: .getPrimaryKeyObj())) {
260:
261: itr.remove();
262: } else {
263: ban.setBanUserUuid(ban.getBanUserUuid());
264:
265: ban.setUserUuid(ban.getUserUuid());
266: }
267: }
268: }
269:
270: xml = xStream.toXML(bans);
271:
272: el = root.addElement("message-board-bans");
273:
274: tempDoc = PortalUtil.readDocumentFromXML(xml);
275:
276: el.content().add(tempDoc.getRootElement().createCopy());
277:
278: return doc.asXML();
279: } catch (Exception e) {
280: throw new PortletDataException(e);
281: }
282: }
283:
284: public PortletDataHandlerControl[] getExportControls()
285: throws PortletDataException {
286:
287: return new PortletDataHandlerControl[] {
288: _categoriesAndMessages, _attachments, _userBans,
289: _flags, _tags };
290: }
291:
292: public PortletDataHandlerControl[] getImportControls()
293: throws PortletDataException {
294:
295: return new PortletDataHandlerControl[] {
296: _categoriesAndMessages, _attachments, _userBans,
297: _flags, _tags };
298: }
299:
300: public PortletPreferences importData(PortletDataContext context,
301: String portletId, PortletPreferences prefs, String data)
302: throws PortletDataException {
303:
304: try {
305: XStream xStream = new XStream();
306:
307: Document doc = PortalUtil.readDocumentFromXML(data);
308:
309: Element root = doc.getRootElement();
310:
311: // Categories
312:
313: Element el = root.element("message-board-categories")
314: .element("list");
315:
316: Document tempDoc = DocumentHelper.createDocument();
317:
318: tempDoc.content().add(el.createCopy());
319:
320: Map categoryPKs = CollectionFactory.getHashMap();
321:
322: List categories = (List) xStream.fromXML(tempDoc.asXML());
323:
324: Iterator itr = categories.iterator();
325:
326: while (itr.hasNext()) {
327: MBCategory category = (MBCategory) itr.next();
328:
329: importCategory(context, categoryPKs, category);
330: }
331:
332: // Messages
333:
334: el = root.element("message-board-messages").element("list");
335:
336: tempDoc = DocumentHelper.createDocument();
337:
338: tempDoc.content().add(el.createCopy());
339:
340: Map threadPKs = CollectionFactory.getHashMap();
341: Map messagePKs = CollectionFactory.getHashMap();
342:
343: List messages = (List) xStream.fromXML(tempDoc.asXML());
344:
345: itr = messages.iterator();
346:
347: while (itr.hasNext()) {
348: MBMessage message = (MBMessage) itr.next();
349:
350: importMessage(context, categoryPKs, threadPKs,
351: messagePKs, message);
352: }
353:
354: // Flags
355:
356: if (context.getBooleanParameter(_NAMESPACE, "flags")) {
357: el = root.element("message-board-flags")
358: .element("list");
359:
360: tempDoc = DocumentHelper.createDocument();
361:
362: tempDoc.content().add(el.createCopy());
363:
364: List flags = (List) xStream.fromXML(tempDoc.asXML());
365:
366: itr = flags.iterator();
367:
368: while (itr.hasNext()) {
369: MBMessageFlag flag = (MBMessageFlag) itr.next();
370:
371: importFlag(context, messagePKs, flag);
372: }
373: }
374:
375: // Bans
376:
377: if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
378: el = root.element("message-board-bans").element("list");
379:
380: tempDoc = DocumentHelper.createDocument();
381:
382: tempDoc.content().add(el.createCopy());
383:
384: List bans = (List) xStream.fromXML(tempDoc.asXML());
385:
386: itr = bans.iterator();
387:
388: while (itr.hasNext()) {
389: MBBan ban = (MBBan) itr.next();
390:
391: importBan(context, ban);
392: }
393: }
394:
395: return null;
396: } catch (Exception e) {
397: throw new PortletDataException(e);
398: }
399: }
400:
401: protected void importBan(PortletDataContext context, MBBan ban)
402: throws Exception {
403:
404: long userId = context.getUserId(ban.getUserUuid());
405: long plid = context.getPlid();
406:
407: List users = UserUtil.findByUuid(ban.getBanUserUuid());
408:
409: Iterator itr = users.iterator();
410:
411: if (itr.hasNext()) {
412: User user = (User) itr.next();
413:
414: MBBanLocalServiceUtil
415: .addBan(userId, plid, user.getUserId());
416: } else {
417: _log.error("Could not find banned user with uuid "
418: + ban.getBanUserUuid());
419: }
420: }
421:
422: protected void importCategory(PortletDataContext context,
423: Map categoryPKs, MBCategory category) throws Exception {
424:
425: long userId = context.getUserId(category.getUserUuid());
426: long plid = context.getPlid();
427: long parentCategoryId = MapUtil.getLong(categoryPKs, category
428: .getParentCategoryId(), category.getParentCategoryId());
429:
430: boolean addCommunityPermissions = true;
431: boolean addGuestPermissions = true;
432:
433: MBCategory existingCategory = null;
434:
435: try {
436: if (parentCategoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
437: MBCategoryUtil.findByPrimaryKey(parentCategoryId);
438: }
439:
440: if (context.getDataStrategy().equals(
441: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
442:
443: existingCategory = MBCategoryUtil.fetchByUUID_G(
444: category.getUuid(), context.getGroupId());
445:
446: if (existingCategory == null) {
447: existingCategory = MBCategoryLocalServiceUtil
448: .addCategory(category.getUuid(), userId,
449: plid, parentCategoryId, category
450: .getName(), category
451: .getDescription(),
452: addCommunityPermissions,
453: addGuestPermissions);
454: } else {
455: existingCategory = MBCategoryLocalServiceUtil
456: .updateCategory(existingCategory
457: .getCategoryId(), parentCategoryId,
458: category.getName(), category
459: .getDescription(), false);
460: }
461: } else {
462: existingCategory = MBCategoryLocalServiceUtil
463: .addCategory(userId, plid, parentCategoryId,
464: category.getName(), category
465: .getDescription(),
466: addCommunityPermissions,
467: addGuestPermissions);
468: }
469:
470: categoryPKs.put(category.getPrimaryKeyObj(),
471: existingCategory.getPrimaryKeyObj());
472: } catch (NoSuchCategoryException nsce) {
473: _log
474: .error("Could not find the parent category for category "
475: + category.getCategoryId());
476: }
477: }
478:
479: protected void importFlag(PortletDataContext context,
480: Map messagePKs, MBMessageFlag flag) throws Exception {
481:
482: long userId = context.getUserId(flag.getUserUuid());
483: long messageId = MapUtil.getLong(messagePKs, flag
484: .getMessageId(), flag.getMessageId());
485:
486: try {
487: List messages = new ArrayList();
488:
489: messages.add(MBMessageUtil.findByPrimaryKey(messageId));
490:
491: MBMessageFlagLocalServiceUtil
492: .addReadFlags(userId, messages);
493: } catch (NoSuchMessageException nsme) {
494: _log.error("Could not find the message for flag "
495: + flag.getMessageFlagId());
496: }
497: }
498:
499: protected void importMessage(PortletDataContext context,
500: Map categoryPKs, Map threadPKs, Map messagePKs,
501: MBMessage message) throws Exception {
502:
503: long userId = context.getUserId(message.getUserUuid());
504: long categoryId = MapUtil.getLong(categoryPKs, message
505: .getCategoryId(), message.getCategoryId());
506: long threadId = MapUtil.getLong(threadPKs, message
507: .getThreadId(), message.getThreadId());
508: long parentMessageId = MapUtil.getLong(messagePKs, message
509: .getParentMessageId(), message.getParentMessageId());
510:
511: List files = new ArrayList();
512:
513: if (context.getBooleanParameter(_NAMESPACE, "attachments")
514: && message.isAttachments()) {
515:
516: files = (List) context.getZipReader().getFolderEntries()
517: .get(message.getAttachmentsDir() + "/");
518:
519: if (files == null) {
520: _log.error("Could not find attachments for message "
521: + message.getMessageId());
522:
523: files = new ArrayList();
524: }
525: }
526:
527: String[] tagsEntries = null;
528:
529: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
530: tagsEntries = context.getTagsEntries(MBMessage.class,
531: message.getPrimaryKeyObj());
532: }
533:
534: PortletPreferences prefs = null;
535:
536: boolean addCommunityPermissions = true;
537: boolean addGuestPermissions = true;
538:
539: ThemeDisplay themeDisplay = null;
540:
541: MBMessage existingMessage = null;
542:
543: try {
544: MBCategoryUtil.findByPrimaryKey(categoryId);
545:
546: if (parentMessageId != MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) {
547: MBMessageUtil.findByPrimaryKey(parentMessageId);
548: MBThreadUtil.findByPrimaryKey(threadId);
549: }
550:
551: if (context.getDataStrategy().equals(
552: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
553:
554: try {
555: existingMessage = MBMessageFinderUtil.findByUuid_G(
556: message.getUuid(), context.getGroupId());
557:
558: MBMessageLocalServiceUtil.updateMessage(userId,
559: existingMessage.getMessageId(), message
560: .getSubject(), message.getBody(),
561: files, message.getPriority(), tagsEntries,
562: prefs, themeDisplay);
563: } catch (NoSuchMessageException nsme) {
564: existingMessage = MBMessageLocalServiceUtil
565: .addMessage(message.getUuid(), userId,
566: categoryId, threadId,
567: parentMessageId, message
568: .getSubject(), message
569: .getBody(), files, message
570: .getAnonymous(), message
571: .getPriority(),
572: tagsEntries, prefs,
573: addCommunityPermissions,
574: addGuestPermissions, themeDisplay);
575: }
576: } else {
577: existingMessage = MBMessageLocalServiceUtil.addMessage(
578: userId, categoryId, threadId, parentMessageId,
579: message.getSubject(), message.getBody(), files,
580: message.getAnonymous(), message.getPriority(),
581: tagsEntries, prefs, addCommunityPermissions,
582: addGuestPermissions, themeDisplay);
583: }
584:
585: threadPKs.put(new Long(message.getThreadId()), new Long(
586: existingMessage.getThreadId()));
587: messagePKs.put(message.getPrimaryKeyObj(), existingMessage
588: .getPrimaryKeyObj());
589: } catch (NoSuchCategoryException nsce) {
590: _log
591: .error("Could not find the parent category for message "
592: + message.getMessageId());
593: } catch (NoSuchMessageException nsme) {
594: _log.error("Could not find the parent message for message "
595: + message.getMessageId());
596: } catch (NoSuchThreadException nste) {
597: _log.error("Could not find the thread for message "
598: + message.getMessageId());
599: }
600: }
601:
602: private static final String _NAMESPACE = "message_board";
603:
604: private static final PortletDataHandlerBoolean _categoriesAndMessages = new PortletDataHandlerBoolean(
605: _NAMESPACE, "categories-and-messages", true, true);
606:
607: private static final PortletDataHandlerBoolean _attachments = new PortletDataHandlerBoolean(
608: _NAMESPACE, "attachments");
609:
610: private static final PortletDataHandlerBoolean _userBans = new PortletDataHandlerBoolean(
611: _NAMESPACE, "user-bans");
612:
613: private static final PortletDataHandlerBoolean _flags = new PortletDataHandlerBoolean(
614: _NAMESPACE, "flags");
615:
616: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
617: _NAMESPACE, "tags");
618:
619: private static Log _log = LogFactory
620: .getLog(MBPortletDataHandlerImpl.class);
621:
622: }
|