1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.lar;
16  
17  import com.liferay.documentlibrary.service.DLServiceUtil;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.lar.BasePortletDataHandler;
21  import com.liferay.portal.kernel.lar.PortletDataContext;
22  import com.liferay.portal.kernel.lar.PortletDataException;
23  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
24  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
25  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.MapUtil;
29  import com.liferay.portal.kernel.util.ObjectValuePair;
30  import com.liferay.portal.kernel.util.StringBundler;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.workflow.WorkflowConstants;
33  import com.liferay.portal.kernel.xml.Document;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.model.CompanyConstants;
37  import com.liferay.portal.model.User;
38  import com.liferay.portal.service.ServiceContext;
39  import com.liferay.portal.service.persistence.UserUtil;
40  import com.liferay.portal.util.PortletKeys;
41  import com.liferay.portlet.messageboards.NoSuchCategoryException;
42  import com.liferay.portlet.messageboards.NoSuchMessageException;
43  import com.liferay.portlet.messageboards.NoSuchThreadException;
44  import com.liferay.portlet.messageboards.model.MBBan;
45  import com.liferay.portlet.messageboards.model.MBCategory;
46  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
47  import com.liferay.portlet.messageboards.model.MBMessage;
48  import com.liferay.portlet.messageboards.model.MBMessageConstants;
49  import com.liferay.portlet.messageboards.model.MBMessageFlag;
50  import com.liferay.portlet.messageboards.model.MBThread;
51  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
52  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
53  import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
54  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
55  import com.liferay.portlet.messageboards.service.persistence.MBBanUtil;
56  import com.liferay.portlet.messageboards.service.persistence.MBCategoryUtil;
57  import com.liferay.portlet.messageboards.service.persistence.MBMessageFlagUtil;
58  import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
59  import com.liferay.portlet.messageboards.service.persistence.MBThreadUtil;
60  
61  import java.util.ArrayList;
62  import java.util.Iterator;
63  import java.util.List;
64  import java.util.Map;
65  
66  import javax.portlet.PortletPreferences;
67  
68  /**
69   * <a href="MBPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Bruno Farache
72   * @author Raymond Augé
73   */
74  public class MBPortletDataHandlerImpl extends BasePortletDataHandler {
75  
76      public PortletPreferences deleteData(
77              PortletDataContext context, String portletId,
78              PortletPreferences preferences)
79          throws PortletDataException {
80  
81          try {
82              if (!context.addPrimaryKey(
83                      MBPortletDataHandlerImpl.class, "deleteData")) {
84  
85                  MBCategoryLocalServiceUtil.deleteCategories(
86                      context.getGroupId());
87              }
88  
89              return null;
90          }
91          catch (Exception e) {
92              throw new PortletDataException(e);
93          }
94      }
95  
96      public String exportData(
97              PortletDataContext context, String portletId,
98              PortletPreferences preferences)
99          throws PortletDataException {
100 
101         try {
102             context.addPermissions(
103                 "com.liferay.portlet.messageboards", context.getGroupId());
104 
105             Document doc = SAXReaderUtil.createDocument();
106 
107             Element root = doc.addElement("message-boards-data");
108 
109             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
110 
111             Element categoriesEl = root.addElement("categories");
112             Element messagesEl = root.addElement("messages");
113             Element messageFlagsEl = root.addElement("message-flags");
114             Element userBansEl = root.addElement("user-bans");
115 
116             List<MBCategory> categories = MBCategoryUtil.findByGroupId(
117                 context.getGroupId());
118 
119             for (MBCategory category : categories) {
120                 exportCategory(
121                     context, categoriesEl, messagesEl, messageFlagsEl,
122                     category);
123             }
124 
125             List<MBMessage> messages = MBMessageUtil.findByG_C(
126                 context.getGroupId(),
127                 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
128 
129             for (MBMessage message : messages) {
130                 exportMessage(
131                     context, categoriesEl, messagesEl, messageFlagsEl, message);
132             }
133 
134             if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
135                 List<MBBan> bans = MBBanUtil.findByGroupId(
136                     context.getGroupId());
137 
138                 for (MBBan ban : bans) {
139                     exportUserBan(context, userBansEl, ban);
140                 }
141             }
142 
143             return doc.formattedString();
144         }
145         catch (Exception e) {
146             throw new PortletDataException(e);
147         }
148     }
149 
150     public PortletDataHandlerControl[] getExportControls() {
151         return new PortletDataHandlerControl[] {
152             _categoriesAndMessages, _attachments, _userBans, _flags, _ratings,
153             _tags
154         };
155     }
156 
157     public PortletDataHandlerControl[] getImportControls() {
158         return new PortletDataHandlerControl[] {
159             _categoriesAndMessages, _attachments, _userBans, _flags, _ratings,
160             _tags
161         };
162     }
163 
164     public PortletPreferences importData(
165             PortletDataContext context, String portletId,
166             PortletPreferences preferences, String data)
167         throws PortletDataException {
168 
169         try {
170             context.importPermissions(
171                 "com.liferay.portlet.messageboards", context.getSourceGroupId(),
172                 context.getGroupId());
173 
174             Document doc = SAXReaderUtil.read(data);
175 
176             Element root = doc.getRootElement();
177 
178             List<Element> categoryEls = root.element("categories").elements(
179                 "category");
180 
181             Map<Long, Long> categoryPKs =
182                 (Map<Long, Long>)context.getNewPrimaryKeysMap(MBCategory.class);
183 
184             for (Element categoryEl : categoryEls) {
185                 String path = categoryEl.attributeValue("path");
186 
187                 if (!context.isPathNotProcessed(path)) {
188                     continue;
189                 }
190 
191                 MBCategory category = (MBCategory)context.getZipEntryAsObject(
192                     path);
193 
194                 importCategory(context, categoryPKs, category);
195             }
196 
197             List<Element> messageEls = root.element("messages").elements(
198                 "message");
199 
200             Map<Long, Long> threadPKs =
201                 (Map<Long, Long>)context.getNewPrimaryKeysMap(MBThread.class);
202             Map<Long, Long> messagePKs =
203                 (Map<Long, Long>)context.getNewPrimaryKeysMap(MBMessage.class);
204 
205             for (Element messageEl : messageEls) {
206                 String path = messageEl.attributeValue("path");
207 
208                 if (!context.isPathNotProcessed(path)) {
209                     continue;
210                 }
211 
212                 MBMessage message = (MBMessage)context.getZipEntryAsObject(
213                     path);
214 
215                 importMessage(
216                     context, categoryPKs, threadPKs, messagePKs, messageEl,
217                     message);
218             }
219 
220             if (context.getBooleanParameter(_NAMESPACE, "flags")) {
221                 List<Element> flagEls = root.element("message-flags").elements(
222                     "flag");
223 
224                 for (Element flagEl : flagEls) {
225                     String path = flagEl.attributeValue("path");
226 
227                     if (!context.isPathNotProcessed(path)) {
228                         continue;
229                     }
230 
231                     MBMessageFlag flag =
232                         (MBMessageFlag)context.getZipEntryAsObject(path);
233 
234                     importFlag(context, messagePKs, flag);
235                 }
236             }
237 
238             if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
239                 List<Element> banEls = root.element("user-bans").elements(
240                     "user-ban");
241 
242                 for (Element banEl : banEls) {
243                     String path = banEl.attributeValue("path");
244 
245                     if (!context.isPathNotProcessed(path)) {
246                         continue;
247                     }
248 
249                     MBBan ban = (MBBan)context.getZipEntryAsObject(path);
250 
251                     importBan(context, ban);
252                 }
253             }
254 
255             return null;
256         }
257         catch (Exception e) {
258             throw new PortletDataException(e);
259         }
260     }
261 
262     protected void exportCategory(
263             PortletDataContext context, Element categoriesEl,
264             Element messagesEl, Element messageFlagsEl, MBCategory category)
265         throws PortalException, SystemException {
266 
267         if (context.isWithinDateRange(category.getModifiedDate())) {
268             exportParentCategory(
269                 context, categoriesEl, category.getParentCategoryId());
270 
271             String path = getCategoryPath(context, category);
272 
273             if (context.isPathNotProcessed(path)) {
274                 Element categoryEl = categoriesEl.addElement("category");
275 
276                 categoryEl.addAttribute("path", path);
277 
278                 category.setUserUuid(category.getUserUuid());
279 
280                 context.addPermissions(
281                     MBCategory.class, category.getCategoryId());
282 
283                 context.addZipEntry(path, category);
284             }
285         }
286 
287         List<MBMessage> messages = MBMessageUtil.findByG_C(
288             category.getGroupId(), category.getCategoryId());
289 
290         for (MBMessage message : messages) {
291             exportMessage(
292                 context, categoriesEl, messagesEl, messageFlagsEl, message);
293         }
294     }
295 
296     protected void exportMessage(
297             PortletDataContext context, Element categoriesEl,
298             Element messagesEl, Element messageFlagsEl, MBMessage message)
299         throws PortalException, SystemException {
300 
301         if (!context.isWithinDateRange(message.getModifiedDate())) {
302             return;
303         }
304 
305         if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
306             return;
307         }
308 
309         exportParentCategory(context, categoriesEl, message.getCategoryId());
310 
311         String path = getMessagePath(context, message);
312 
313         if (context.isPathNotProcessed(path)) {
314             Element messageEl = messagesEl.addElement("message");
315 
316             messageEl.addAttribute("path", path);
317 
318             message.setUserUuid(message.getUserUuid());
319             message.setPriority(message.getPriority());
320 
321             context.addPermissions(MBMessage.class, message.getMessageId());
322 
323             context.addLocks(
324                 MBThread.class, String.valueOf(message.getThreadId()));
325 
326             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
327                 context.addRatingsEntries(
328                     MBMessage.class, message.getMessageId());
329             }
330 
331             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
332                 context.addAssetTags(MBMessage.class, message.getMessageId());
333             }
334 
335             if (context.getBooleanParameter(_NAMESPACE, "attachments") &&
336                 message.isAttachments()) {
337 
338                 for (String attachment : message.getAttachmentsFiles()) {
339                     int pos = attachment.lastIndexOf(StringPool.FORWARD_SLASH);
340 
341                     String name = attachment.substring(pos + 1);
342                     String binPath = getMessageAttachementBinPath(
343                         context, message, name);
344 
345                     Element attachmentEl = messageEl.addElement("attachment");
346 
347                     attachmentEl.addAttribute("name", name);
348                     attachmentEl.addAttribute("bin-path", binPath);
349 
350                     byte[] bytes = DLServiceUtil.getFile(
351                         context.getCompanyId(), CompanyConstants.SYSTEM,
352                         attachment);
353 
354                     context.addZipEntry(binPath, bytes);
355                 }
356 
357                 message.setAttachmentsDir(message.getAttachmentsDir());
358             }
359 
360             if (context.getBooleanParameter(_NAMESPACE, "flags")) {
361                 List<MBMessageFlag> messageFlags =
362                     MBMessageFlagUtil.findByMessageId(
363                         message.getMessageId());
364 
365                 for (MBMessageFlag messageFlag : messageFlags) {
366                     exportMessageFlag(context, messageFlagsEl, messageFlag);
367                 }
368             }
369 
370             context.addZipEntry(path, message);
371         }
372     }
373 
374     protected void exportMessageFlag(
375             PortletDataContext context, Element messageFlagsEl,
376             MBMessageFlag messageFlag)
377         throws SystemException {
378 
379         String path = getMessageFlagPath(context, messageFlag);
380 
381         if (!context.isPathNotProcessed(path)) {
382             return;
383         }
384 
385         Element messageFlagEl = messageFlagsEl.addElement("message-flag");
386 
387         messageFlagEl.addAttribute("path", path);
388 
389         messageFlag.setUserUuid(messageFlag.getUserUuid());
390 
391         context.addZipEntry(path, messageFlag);
392     }
393 
394     protected void exportParentCategory(
395             PortletDataContext context, Element categoriesEl, long categoryId)
396         throws PortalException, SystemException {
397 
398         if ((!context.hasDateRange()) ||
399             (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
400             (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
401 
402             return;
403         }
404 
405         MBCategory category = MBCategoryUtil.findByPrimaryKey(categoryId);
406 
407         exportParentCategory(
408             context, categoriesEl, category.getParentCategoryId());
409 
410         String path = getCategoryPath(context, category);
411 
412         if (context.isPathNotProcessed(path)) {
413             Element categoryEl = categoriesEl.addElement("category");
414 
415             categoryEl.addAttribute("path", path);
416 
417             category.setUserUuid(category.getUserUuid());
418 
419             context.addPermissions(MBCategory.class, category.getCategoryId());
420 
421             context.addZipEntry(path, category);
422         }
423     }
424 
425     protected void exportUserBan(
426             PortletDataContext context, Element userBansEl, MBBan ban)
427         throws SystemException {
428 
429         if (!context.isWithinDateRange(ban.getModifiedDate())) {
430             return;
431         }
432 
433         String path = getUserBanPath(context, ban);
434 
435         if (!context.isPathNotProcessed(path)) {
436             return;
437         }
438 
439         Element userBanEl = userBansEl.addElement("user-ban");
440 
441         userBanEl.addAttribute("path", path);
442 
443         ban.setBanUserUuid(ban.getBanUserUuid());
444         ban.setUserUuid(ban.getUserUuid());
445 
446         context.addZipEntry(path, ban);
447     }
448 
449     protected void importBan(PortletDataContext context, MBBan ban)
450         throws Exception {
451 
452         long userId = context.getUserId(ban.getUserUuid());
453 
454         ServiceContext serviceContext = new ServiceContext();
455 
456         serviceContext.setCreateDate(ban.getCreateDate());
457         serviceContext.setModifiedDate(ban.getModifiedDate());
458         serviceContext.setScopeGroupId(context.getGroupId());
459 
460         List<User> users = UserUtil.findByUuid(ban.getBanUserUuid());
461 
462         Iterator<User> itr = users.iterator();
463 
464         if (itr.hasNext()) {
465             User user = itr.next();
466 
467             MBBanLocalServiceUtil.addBan(
468                 userId, user.getUserId(), serviceContext);
469         }
470         else {
471             _log.error(
472                 "Could not find banned user with uuid " + ban.getBanUserUuid());
473         }
474     }
475 
476     protected void importCategory(
477             PortletDataContext context, Map<Long, Long> categoryPKs,
478             MBCategory category)
479         throws Exception {
480 
481         long userId = context.getUserId(category.getUserUuid());
482         long parentCategoryId = MapUtil.getLong(
483             categoryPKs, category.getParentCategoryId(),
484             category.getParentCategoryId());
485 
486         String emailAddress = null;
487         String inProtocol = null;
488         String inServerName = null;
489         int inServerPort = 0;
490         boolean inUseSSL = false;
491         String inUserName = null;
492         String inPassword = null;
493         int inReadInterval = 0;
494         String outEmailAddress = null;
495         boolean outCustom = false;
496         String outServerName = null;
497         int outServerPort = 0;
498         boolean outUseSSL = false;
499         String outUserName = null;
500         String outPassword = null;
501         boolean mailingListActive = false;
502 
503         ServiceContext serviceContext = new ServiceContext();
504 
505         serviceContext.setAddCommunityPermissions(true);
506         serviceContext.setAddGuestPermissions(true);
507         serviceContext.setCreateDate(category.getCreateDate());
508         serviceContext.setModifiedDate(category.getModifiedDate());
509         serviceContext.setScopeGroupId(context.getGroupId());
510 
511         if ((parentCategoryId !=
512                 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
513             (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
514             (parentCategoryId == category.getParentCategoryId())) {
515 
516             String path = getImportCategoryPath(context, parentCategoryId);
517 
518             MBCategory parentCategory =
519                 (MBCategory)context.getZipEntryAsObject(path);
520 
521             importCategory(context, categoryPKs, parentCategory);
522 
523             parentCategoryId = MapUtil.getLong(
524                 categoryPKs, category.getParentCategoryId(),
525                 category.getParentCategoryId());
526         }
527 
528         MBCategory importedCategory = null;
529 
530         try {
531             if ((parentCategoryId !=
532                     MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
533                 (parentCategoryId !=
534                     MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
535 
536                 MBCategoryUtil.findByPrimaryKey(parentCategoryId);
537             }
538 
539             if (context.getDataStrategy().equals(
540                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
541 
542                 MBCategory existingCategory = MBCategoryUtil.fetchByUUID_G(
543                     category.getUuid(), context.getGroupId());
544 
545                 if (existingCategory == null) {
546                     importedCategory = MBCategoryLocalServiceUtil.addCategory(
547                         category.getUuid(), userId, parentCategoryId,
548                         category.getName(), category.getDescription(),
549                         emailAddress, inProtocol, inServerName, inServerPort,
550                         inUseSSL, inUserName, inPassword, inReadInterval,
551                         outEmailAddress, outCustom, outServerName,
552                         outServerPort, outUseSSL, outUserName, outPassword,
553                         mailingListActive, serviceContext);
554                 }
555                 else {
556                     importedCategory =
557                         MBCategoryLocalServiceUtil.updateCategory(
558                             existingCategory.getCategoryId(), parentCategoryId,
559                             category.getName(), category.getDescription(),
560                             emailAddress, inProtocol, inServerName,
561                             inServerPort, inUseSSL, inUserName, inPassword,
562                             inReadInterval, outEmailAddress, outCustom,
563                             outServerName, outServerPort, outUseSSL,
564                             outUserName, outPassword, mailingListActive, false,
565                             serviceContext);
566                 }
567             }
568             else {
569                 importedCategory = MBCategoryLocalServiceUtil.addCategory(
570                     userId, parentCategoryId, category.getName(),
571                     category.getDescription(), emailAddress, inProtocol,
572                     inServerName, inServerPort, inUseSSL, inUserName,
573                     inPassword, inReadInterval, outEmailAddress, outCustom,
574                     outServerName, outServerPort, outUseSSL, outUserName,
575                     outPassword, mailingListActive, serviceContext);
576             }
577 
578             categoryPKs.put(
579                 category.getCategoryId(), importedCategory.getCategoryId());
580 
581             context.importPermissions(
582                 MBCategory.class, category.getCategoryId(),
583                 importedCategory.getCategoryId());
584         }
585         catch (NoSuchCategoryException nsce) {
586             _log.error(
587                 "Could not find the parent category for category " +
588                     category.getCategoryId());
589         }
590     }
591 
592     protected void importFlag(
593             PortletDataContext context, Map<Long, Long> messagePKs,
594             MBMessageFlag flag)
595         throws Exception {
596 
597         long userId = context.getUserId(flag.getUserUuid());
598         long messageId = MapUtil.getLong(
599             messagePKs, flag.getMessageId(), flag.getMessageId());
600 
601         try {
602             MBMessage message = MBMessageUtil.findByPrimaryKey(messageId);
603 
604             MBThread thread = message.getThread();
605 
606             MBMessageFlagLocalServiceUtil.addReadFlags(userId, thread);
607         }
608         catch (NoSuchMessageException nsme) {
609             _log.error(
610                 "Could not find the message for flag " +
611                     flag.getMessageFlagId());
612         }
613     }
614 
615     protected void importMessage(
616             PortletDataContext context, Map<Long, Long> categoryPKs,
617             Map<Long, Long> threadPKs, Map<Long, Long> messagePKs,
618             Element messageEl, MBMessage message)
619         throws Exception {
620 
621         long userId = context.getUserId(message.getUserUuid());
622         String userName = message.getUserName();
623         long categoryId = MapUtil.getLong(
624             categoryPKs, message.getCategoryId(), message.getCategoryId());
625         long threadId = MapUtil.getLong(
626             threadPKs, message.getThreadId(), message.getThreadId());
627         long parentMessageId = MapUtil.getLong(
628             messagePKs, message.getParentMessageId(),
629             message.getParentMessageId());
630 
631         List<ObjectValuePair<String, byte[]>> files =
632             new ArrayList<ObjectValuePair<String, byte[]>>();
633         List<String> existingFiles = new ArrayList<String>();
634 
635         if (context.getBooleanParameter(_NAMESPACE, "attachments") &&
636             message.isAttachments()) {
637 
638             List<Element> attachmentEls = messageEl.elements("attachment");
639 
640             for (Element attachmentEl : attachmentEls) {
641                 String name = attachmentEl.attributeValue("name");
642                 String binPath = attachmentEl.attributeValue("bin-path");
643 
644                 byte[] bytes = context.getZipEntryAsByteArray(binPath);
645 
646                 files.add(new ObjectValuePair<String, byte[]>(name, bytes));
647             }
648 
649             if (files.size() <= 0) {
650                 _log.error(
651                     "Could not find attachments for message " +
652                         message.getMessageId());
653             }
654         }
655 
656         String[] assetTagNames = null;
657 
658         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
659             assetTagNames = context.getAssetTagNames(
660                 MBMessage.class, message.getMessageId());
661         }
662 
663         ServiceContext serviceContext = new ServiceContext();
664 
665         serviceContext.setAddCommunityPermissions(true);
666         serviceContext.setAddGuestPermissions(true);
667         serviceContext.setAssetTagNames(assetTagNames);
668         serviceContext.setCreateDate(message.getCreateDate());
669         serviceContext.setModifiedDate(message.getModifiedDate());
670         serviceContext.setScopeGroupId(context.getGroupId());
671 
672         if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
673             serviceContext.setWorkflowAction(
674                 WorkflowConstants.ACTION_SAVE_DRAFT);
675         }
676 
677         if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
678             (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
679             (categoryId == message.getCategoryId())) {
680 
681             String path = getImportCategoryPath(context, categoryId);
682 
683             MBCategory category = (MBCategory)context.getZipEntryAsObject(path);
684 
685             importCategory(context, categoryPKs, category);
686 
687             categoryId = MapUtil.getLong(
688                 categoryPKs, message.getCategoryId(), message.getCategoryId());
689         }
690 
691         MBMessage importedMessage = null;
692 
693         try {
694             MBCategoryUtil.findByPrimaryKey(categoryId);
695 
696             if (parentMessageId !=
697                     MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
698 
699                 MBMessageUtil.findByPrimaryKey(parentMessageId);
700                 MBThreadUtil.findByPrimaryKey(threadId);
701             }
702 
703             if (context.getDataStrategy().equals(
704                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
705 
706                 MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(
707                     message.getUuid(), context.getGroupId());
708 
709                 if (existingMessage == null) {
710                     importedMessage = MBMessageLocalServiceUtil.addMessage(
711                         message.getUuid(), userId, userName,
712                         context.getGroupId(), categoryId, threadId,
713                         parentMessageId, message.getSubject(),
714                         message.getBody(), files, message.getAnonymous(),
715                         message.getPriority(), message.getAllowPingbacks(),
716                         serviceContext);
717                 }
718                 else {
719                     importedMessage = MBMessageLocalServiceUtil.updateMessage(
720                         userId, existingMessage.getMessageId(),
721                         message.getSubject(), message.getBody(), files,
722                         existingFiles, message.getPriority(),
723                         message.getAllowPingbacks(), serviceContext);
724                 }
725             }
726             else {
727                 importedMessage = MBMessageLocalServiceUtil.addMessage(
728                     userId, userName, context.getGroupId(), categoryId,
729                     threadId, parentMessageId, message.getSubject(),
730                     message.getBody(), files, message.getAnonymous(),
731                     message.getPriority(), message.getAllowPingbacks(),
732                     serviceContext);
733             }
734 
735             threadPKs.put(message.getThreadId(), importedMessage.getThreadId());
736             messagePKs.put(
737                 message.getMessageId(), importedMessage.getMessageId());
738 
739             context.importLocks(
740                 MBThread.class, String.valueOf(message.getThreadId()),
741                 String.valueOf(importedMessage.getThreadId()));
742 
743             context.importPermissions(
744                 MBMessage.class, message.getMessageId(),
745                 importedMessage.getMessageId());
746 
747             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
748                 context.importRatingsEntries(
749                     MBMessage.class, message.getMessageId(),
750                     importedMessage.getMessageId());
751             }
752         }
753         catch (NoSuchCategoryException nsce) {
754             _log.error(
755                 "Could not find the parent category for message " +
756                     message.getMessageId());
757         }
758         catch (NoSuchMessageException nsme) {
759             _log.error(
760                 "Could not find the parent message for message " +
761                     message.getMessageId());
762         }
763         catch (NoSuchThreadException nste) {
764             _log.error(
765                 "Could not find the thread for message " +
766                     message.getMessageId());
767         }
768     }
769 
770     protected String getCategoryPath(
771         PortletDataContext context, MBCategory category) {
772 
773         StringBundler sb = new StringBundler(4);
774 
775         sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
776         sb.append("/categories/");
777         sb.append(category.getCategoryId());
778         sb.append(".xml");
779 
780         return sb.toString();
781     }
782 
783     protected String getImportCategoryPath(
784         PortletDataContext context, long categoryId) {
785 
786         StringBundler sb = new StringBundler(4);
787 
788         sb.append(context.getSourcePortletPath(PortletKeys.MESSAGE_BOARDS));
789         sb.append("/categories/");
790         sb.append(categoryId);
791         sb.append(".xml");
792 
793         return sb.toString();
794     }
795 
796     protected String getMessageAttachementBinPath(
797         PortletDataContext context, MBMessage message, String attachment) {
798 
799         StringBundler sb = new StringBundler(5);
800 
801         sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
802         sb.append("/bin/");
803         sb.append(message.getMessageId());
804         sb.append(StringPool.SLASH);
805         sb.append(attachment);
806 
807         return sb.toString();
808     }
809 
810     protected String getMessageFlagPath(
811         PortletDataContext context, MBMessageFlag messageFlag) {
812 
813         StringBundler sb = new StringBundler(4);
814 
815         sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
816         sb.append("/message-flags/");
817         sb.append(messageFlag.getMessageFlagId());
818         sb.append(".xml");
819 
820         return sb.toString();
821     }
822 
823     protected String getMessagePath(
824         PortletDataContext context, MBMessage message) {
825 
826         StringBundler sb = new StringBundler(4);
827 
828         sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
829         sb.append("/messages/");
830         sb.append(message.getMessageId());
831         sb.append(".xml");
832 
833         return sb.toString();
834     }
835 
836     protected String getUserBanPath(PortletDataContext context, MBBan ban) {
837         StringBundler sb = new StringBundler(4);
838 
839         sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
840         sb.append("/user-bans/");
841         sb.append(ban.getBanId());
842         sb.append(".xml");
843 
844         return sb.toString();
845     }
846 
847     private static final String _NAMESPACE = "message_board";
848 
849     private static Log _log = LogFactoryUtil.getLog(
850         MBPortletDataHandlerImpl.class);
851 
852     private static PortletDataHandlerBoolean _attachments =
853         new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
854 
855     private static PortletDataHandlerBoolean _categoriesAndMessages =
856         new PortletDataHandlerBoolean(
857             _NAMESPACE, "categories-and-messages", true, true);
858 
859     private static PortletDataHandlerBoolean _flags =
860         new PortletDataHandlerBoolean(_NAMESPACE, "flags");
861 
862     private static PortletDataHandlerBoolean _ratings =
863         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
864 
865     private static PortletDataHandlerBoolean _tags =
866         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
867 
868     private static PortletDataHandlerBoolean _userBans =
869         new PortletDataHandlerBoolean(_NAMESPACE, "user-bans");
870 
871 }