001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.lar;
016    
017    import com.liferay.documentlibrary.service.DLServiceUtil;
018    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.CharPool;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.util.ObjectValuePair;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.kernel.xml.Document;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.model.CompanyConstants;
035    import com.liferay.portal.model.User;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.service.persistence.UserUtil;
038    import com.liferay.portal.util.PortletKeys;
039    import com.liferay.portlet.messageboards.model.MBBan;
040    import com.liferay.portlet.messageboards.model.MBCategory;
041    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
042    import com.liferay.portlet.messageboards.model.MBMessage;
043    import com.liferay.portlet.messageboards.model.MBMessageFlag;
044    import com.liferay.portlet.messageboards.model.MBThread;
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.MBThreadLocalServiceUtil;
050    import com.liferay.portlet.messageboards.service.persistence.MBBanUtil;
051    import com.liferay.portlet.messageboards.service.persistence.MBCategoryUtil;
052    import com.liferay.portlet.messageboards.service.persistence.MBMessageFlagUtil;
053    import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
054    
055    import java.util.ArrayList;
056    import java.util.Iterator;
057    import java.util.List;
058    import java.util.Map;
059    
060    import javax.portlet.PortletPreferences;
061    
062    /**
063     * @author Bruno Farache
064     * @author Raymond Augé
065     */
066    public class MBPortletDataHandlerImpl extends BasePortletDataHandler {
067    
068            public PortletDataHandlerControl[] getExportControls() {
069                    return new PortletDataHandlerControl[] {
070                            _categoriesAndMessages, _attachments, _messageFlags, _userBans,
071                            _ratings, _tags
072                    };
073            }
074    
075            public PortletDataHandlerControl[] getImportControls() {
076                    return new PortletDataHandlerControl[] {
077                            _categoriesAndMessages, _attachments, _messageFlags, _userBans,
078                            _ratings, _tags
079                    };
080            }
081    
082            protected PortletPreferences doDeleteData(
083                            PortletDataContext context, String portletId,
084                            PortletPreferences preferences)
085                    throws Exception {
086    
087                    if (!context.addPrimaryKey(
088                                    MBPortletDataHandlerImpl.class, "deleteData")) {
089    
090                            MBCategoryLocalServiceUtil.deleteCategories(
091                                    context.getScopeGroupId());
092    
093                            MBThreadLocalServiceUtil.deleteThreads(
094                                    context.getScopeGroupId(),
095                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
096                    }
097    
098                    return null;
099            }
100    
101            protected String doExportData(
102                            PortletDataContext context, String portletId,
103                            PortletPreferences preferences)
104                    throws Exception {
105    
106                    context.addPermissions(
107                            "com.liferay.portlet.messageboards", context.getScopeGroupId());
108    
109                    Document document = SAXReaderUtil.createDocument();
110    
111                    Element rootElement = document.addElement("message-boards-data");
112    
113                    rootElement.addAttribute(
114                            "group-id", String.valueOf(context.getScopeGroupId()));
115    
116                    Element categoriesElement = rootElement.addElement("categories");
117                    Element messagesElement = rootElement.addElement("messages");
118                    Element messageFlagsElement = rootElement.addElement("message-flags");
119                    Element userBansElement = rootElement.addElement("user-bans");
120    
121                    List<MBCategory> categories = MBCategoryUtil.findByGroupId(
122                            context.getScopeGroupId());
123    
124                    for (MBCategory category : categories) {
125                            exportCategory(
126                                    context, categoriesElement, messagesElement,
127                                    messageFlagsElement, category);
128                    }
129    
130                    List<MBMessage> messages = MBMessageUtil.findByG_C(
131                            context.getScopeGroupId(),
132                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
133    
134                    for (MBMessage message : messages) {
135                            exportMessage(
136                                    context, categoriesElement, messagesElement,
137                                    messageFlagsElement, message);
138                    }
139    
140                    if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
141                            List<MBBan> bans = MBBanUtil.findByGroupId(
142                                    context.getScopeGroupId());
143    
144                            for (MBBan ban : bans) {
145                                    exportBan(context, userBansElement, ban);
146                            }
147                    }
148    
149                    return document.formattedString();
150            }
151    
152            protected PortletPreferences doImportData(
153                            PortletDataContext context, String portletId,
154                            PortletPreferences preferences, String data)
155                    throws Exception {
156    
157                    context.importPermissions(
158                            "com.liferay.portlet.messageboards", context.getSourceGroupId(),
159                            context.getScopeGroupId());
160    
161                    Document document = SAXReaderUtil.read(data);
162    
163                    Element rootElement = document.getRootElement();
164    
165                    Element categoriesElement = rootElement.element("categories");
166    
167                    for (Element categoryElement : categoriesElement.elements("category")) {
168                            String path = categoryElement.attributeValue("path");
169    
170                            if (!context.isPathNotProcessed(path)) {
171                                    continue;
172                            }
173    
174                            MBCategory category = (MBCategory)context.getZipEntryAsObject(path);
175    
176                            importCategory(context, category);
177                    }
178    
179                    Element messagesElement = rootElement.element("messages");
180    
181                    for (Element messageElement : messagesElement.elements("message")) {
182                            String path = messageElement.attributeValue("path");
183    
184                            if (!context.isPathNotProcessed(path)) {
185                                    continue;
186                            }
187    
188                            MBMessage message = (MBMessage)context.getZipEntryAsObject(
189                                    path);
190    
191                            importMessage(context, messageElement, message);
192                    }
193    
194                    if (context.getBooleanParameter(_NAMESPACE, "message-flags")) {
195                            Element messageFlagsElement = rootElement.element("message-flags");
196    
197                            for (Element messageFlagElement :
198                                            messageFlagsElement.elements("message-flag")) {
199    
200                                    String path = messageFlagElement.attributeValue("path");
201    
202                                    if (!context.isPathNotProcessed(path)) {
203                                            continue;
204                                    }
205    
206                                    MBMessageFlag flag = (MBMessageFlag)context.getZipEntryAsObject(
207                                            path);
208    
209                                    importMessageFlag(context, flag);
210                            }
211                    }
212    
213                    if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
214                            Element userBansElement = rootElement.element("user-bans");
215    
216                            for (Element userBanElement :
217                                            userBansElement.elements("user-ban")) {
218    
219                                    String path = userBanElement.attributeValue("path");
220    
221                                    if (!context.isPathNotProcessed(path)) {
222                                            continue;
223                                    }
224    
225                                    MBBan ban = (MBBan)context.getZipEntryAsObject(path);
226    
227                                    importBan(context, ban);
228                            }
229                    }
230    
231                    return null;
232            }
233    
234            protected void exportBan(
235                            PortletDataContext context, Element userBansElement, MBBan ban)
236                    throws Exception {
237    
238                    if (!context.isWithinDateRange(ban.getModifiedDate())) {
239                            return;
240                    }
241    
242                    String path = getUserBanPath(context, ban);
243    
244                    if (!context.isPathNotProcessed(path)) {
245                            return;
246                    }
247    
248                    Element userBanElement = userBansElement.addElement("user-ban");
249    
250                    userBanElement.addAttribute("path", path);
251    
252                    ban.setBanUserUuid(ban.getBanUserUuid());
253                    ban.setUserUuid(ban.getUserUuid());
254    
255                    context.addZipEntry(path, ban);
256            }
257    
258            protected void exportCategory(
259                            PortletDataContext context, Element categoriesElement,
260                            Element messagesElement, Element messageFlagsElement,
261                            MBCategory category)
262                    throws Exception {
263    
264                    if (context.isWithinDateRange(category.getModifiedDate())) {
265                            exportParentCategory(
266                                    context, categoriesElement, category.getParentCategoryId());
267    
268                            String path = getCategoryPath(context, category);
269    
270                            if (context.isPathNotProcessed(path)) {
271                                    Element categoryElement = categoriesElement.addElement(
272                                            "category");
273    
274                                    categoryElement.addAttribute("path", path);
275    
276                                    category.setUserUuid(category.getUserUuid());
277    
278                                    context.addPermissions(
279                                            MBCategory.class, category.getCategoryId());
280    
281                                    context.addZipEntry(path, category);
282                            }
283                    }
284    
285                    List<MBMessage> messages = MBMessageUtil.findByG_C(
286                            category.getGroupId(), category.getCategoryId());
287    
288                    for (MBMessage message : messages) {
289                            exportMessage(
290                                    context, categoriesElement, messagesElement,
291                                    messageFlagsElement, message);
292                    }
293            }
294    
295            protected void exportMessage(
296                            PortletDataContext context, Element categoriesElement,
297                            Element messagesElement, Element messageFlagsElement,
298                            MBMessage message)
299                    throws Exception {
300    
301                    if (!context.isWithinDateRange(message.getModifiedDate())) {
302                            return;
303                    }
304    
305                    if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
306                            return;
307                    }
308    
309                    exportParentCategory(
310                            context, categoriesElement, message.getCategoryId());
311    
312                    String path = getMessagePath(context, message);
313    
314                    if (context.isPathNotProcessed(path)) {
315                            Element messageElement = messagesElement.addElement("message");
316    
317                            messageElement.addAttribute("path", path);
318    
319                            message.setUserUuid(message.getUserUuid());
320                            message.setPriority(message.getPriority());
321    
322                            context.addPermissions(MBMessage.class, message.getMessageId());
323    
324                            context.addLocks(
325                                    MBThread.class, String.valueOf(message.getThreadId()));
326    
327                            if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
328                                    context.addRatingsEntries(
329                                            MBMessage.class, message.getMessageId());
330                            }
331    
332                            if (context.getBooleanParameter(_NAMESPACE, "tags")) {
333                                    context.addAssetTags(MBMessage.class, message.getMessageId());
334                            }
335    
336                            if (context.getBooleanParameter(_NAMESPACE, "attachments") &&
337                                    message.isAttachments()) {
338    
339                                    for (String attachment : message.getAttachmentsFiles()) {
340                                            int pos = attachment.lastIndexOf(CharPool.FORWARD_SLASH);
341    
342                                            String name = attachment.substring(pos + 1);
343                                            String binPath = getMessageAttachementBinPath(
344                                                    context, message, name);
345    
346                                            Element attachmentElement = messageElement.addElement(
347                                                    "attachment");
348    
349                                            attachmentElement.addAttribute("name", name);
350                                            attachmentElement.addAttribute("bin-path", binPath);
351    
352                                            byte[] bytes = DLServiceUtil.getFile(
353                                                    context.getCompanyId(), CompanyConstants.SYSTEM,
354                                                    attachment);
355    
356                                            context.addZipEntry(binPath, bytes);
357                                    }
358    
359                                    message.setAttachmentsDir(message.getAttachmentsDir());
360                            }
361    
362                            if (context.getBooleanParameter(_NAMESPACE, "message-flags")) {
363                                    List<MBMessageFlag> messageFlags =
364                                            MBMessageFlagUtil.findByMessageId(message.getMessageId());
365    
366                                    for (MBMessageFlag messageFlag : messageFlags) {
367                                            exportMessageFlag(
368                                                    context, messageFlagsElement, messageFlag);
369                                    }
370                            }
371    
372                            context.addZipEntry(path, message);
373                    }
374            }
375    
376            protected void exportMessageFlag(
377                            PortletDataContext context, Element messageFlagsElement,
378                            MBMessageFlag messageFlag)
379                    throws Exception {
380    
381                    String path = getMessageFlagPath(context, messageFlag);
382    
383                    if (!context.isPathNotProcessed(path)) {
384                            return;
385                    }
386    
387                    Element messageFlagElement = messageFlagsElement.addElement(
388                            "message-flag");
389    
390                    messageFlagElement.addAttribute("path", path);
391    
392                    messageFlag.setUserUuid(messageFlag.getUserUuid());
393    
394                    context.addZipEntry(path, messageFlag);
395            }
396    
397            protected void exportParentCategory(
398                            PortletDataContext context, Element categoriesElement,
399                            long categoryId)
400                    throws Exception {
401    
402                    if ((!context.hasDateRange()) ||
403                            (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
404                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
405    
406                            return;
407                    }
408    
409                    MBCategory category = MBCategoryUtil.findByPrimaryKey(categoryId);
410    
411                    exportParentCategory(
412                            context, categoriesElement, category.getParentCategoryId());
413    
414                    String path = getCategoryPath(context, category);
415    
416                    if (context.isPathNotProcessed(path)) {
417                            Element categoryElement = categoriesElement.addElement("category");
418    
419                            categoryElement.addAttribute("path", path);
420    
421                            category.setUserUuid(category.getUserUuid());
422    
423                            context.addPermissions(MBCategory.class, category.getCategoryId());
424    
425                            context.addZipEntry(path, category);
426                    }
427            }
428    
429            protected String getCategoryPath(
430                    PortletDataContext context, MBCategory category) {
431    
432                    StringBundler sb = new StringBundler(4);
433    
434                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
435                    sb.append("/categories/");
436                    sb.append(category.getCategoryId());
437                    sb.append(".xml");
438    
439                    return sb.toString();
440            }
441    
442            protected String getImportCategoryPath(
443                    PortletDataContext context, long categoryId) {
444    
445                    StringBundler sb = new StringBundler(4);
446    
447                    sb.append(context.getSourcePortletPath(PortletKeys.MESSAGE_BOARDS));
448                    sb.append("/categories/");
449                    sb.append(categoryId);
450                    sb.append(".xml");
451    
452                    return sb.toString();
453            }
454    
455            protected String getMessageAttachementBinPath(
456                    PortletDataContext context, MBMessage message, String attachment) {
457    
458                    StringBundler sb = new StringBundler(5);
459    
460                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
461                    sb.append("/bin/");
462                    sb.append(message.getMessageId());
463                    sb.append(StringPool.SLASH);
464                    sb.append(PortalUUIDUtil.generate());
465    
466                    return sb.toString();
467            }
468    
469            protected String getMessageFlagPath(
470                    PortletDataContext context, MBMessageFlag messageFlag) {
471    
472                    StringBundler sb = new StringBundler(4);
473    
474                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
475                    sb.append("/message-flags/");
476                    sb.append(messageFlag.getMessageFlagId());
477                    sb.append(".xml");
478    
479                    return sb.toString();
480            }
481    
482            protected String getMessagePath(
483                    PortletDataContext context, MBMessage message) {
484    
485                    StringBundler sb = new StringBundler(4);
486    
487                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
488                    sb.append("/messages/");
489                    sb.append(message.getMessageId());
490                    sb.append(".xml");
491    
492                    return sb.toString();
493            }
494    
495            protected String getUserBanPath(PortletDataContext context, MBBan ban) {
496                    StringBundler sb = new StringBundler(4);
497    
498                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
499                    sb.append("/user-bans/");
500                    sb.append(ban.getBanId());
501                    sb.append(".xml");
502    
503                    return sb.toString();
504            }
505    
506            protected void importBan(PortletDataContext context, MBBan ban)
507                    throws Exception {
508    
509                    long userId = context.getUserId(ban.getUserUuid());
510    
511                    ServiceContext serviceContext = new ServiceContext();
512    
513                    serviceContext.setCreateDate(ban.getCreateDate());
514                    serviceContext.setModifiedDate(ban.getModifiedDate());
515                    serviceContext.setScopeGroupId(context.getScopeGroupId());
516    
517                    List<User> users = UserUtil.findByUuid(ban.getBanUserUuid());
518    
519                    Iterator<User> itr = users.iterator();
520    
521                    if (itr.hasNext()) {
522                            User user = itr.next();
523    
524                            MBBanLocalServiceUtil.addBan(
525                                    userId, user.getUserId(), serviceContext);
526                    }
527                    else {
528                            _log.error(
529                                    "Could not find banned user with uuid " + ban.getBanUserUuid());
530                    }
531            }
532    
533            protected void importCategory(
534                            PortletDataContext context, MBCategory category)
535                    throws Exception {
536    
537                    long userId = context.getUserId(category.getUserUuid());
538    
539                    Map<Long, Long> categoryPKs =
540                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBCategory.class);
541    
542                    long parentCategoryId = MapUtil.getLong(
543                            categoryPKs, category.getParentCategoryId(),
544                            category.getParentCategoryId());
545    
546                    String emailAddress = null;
547                    String inProtocol = null;
548                    String inServerName = null;
549                    int inServerPort = 0;
550                    boolean inUseSSL = false;
551                    String inUserName = null;
552                    String inPassword = null;
553                    int inReadInterval = 0;
554                    String outEmailAddress = null;
555                    boolean outCustom = false;
556                    String outServerName = null;
557                    int outServerPort = 0;
558                    boolean outUseSSL = false;
559                    String outUserName = null;
560                    String outPassword = null;
561                    boolean mailingListActive = false;
562    
563                    ServiceContext serviceContext = new ServiceContext();
564    
565                    serviceContext.setAddCommunityPermissions(true);
566                    serviceContext.setAddGuestPermissions(true);
567                    serviceContext.setCreateDate(category.getCreateDate());
568                    serviceContext.setModifiedDate(category.getModifiedDate());
569                    serviceContext.setScopeGroupId(context.getScopeGroupId());
570    
571                    if ((parentCategoryId !=
572                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
573                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
574                            (parentCategoryId == category.getParentCategoryId())) {
575    
576                            String path = getImportCategoryPath(context, parentCategoryId);
577    
578                            MBCategory parentCategory =
579                                    (MBCategory)context.getZipEntryAsObject(path);
580    
581                            importCategory(context, parentCategory);
582    
583                            parentCategoryId = MapUtil.getLong(
584                                    categoryPKs, category.getParentCategoryId(),
585                                    category.getParentCategoryId());
586                    }
587    
588                    MBCategory importedCategory = null;
589    
590                    if (context.isDataStrategyMirror()) {
591                            MBCategory existingCategory = MBCategoryUtil.fetchByUUID_G(
592                                    category.getUuid(), context.getScopeGroupId());
593    
594                            if (existingCategory == null) {
595                                    serviceContext.setUuid(category.getUuid());
596    
597                                    importedCategory = MBCategoryLocalServiceUtil.addCategory(
598                                            userId, parentCategoryId, category.getName(),
599                                            category.getDescription(), emailAddress, inProtocol,
600                                            inServerName, inServerPort, inUseSSL, inUserName,
601                                            inPassword, inReadInterval, outEmailAddress, outCustom,
602                                            outServerName, outServerPort, outUseSSL, outUserName,
603                                            outPassword, mailingListActive, serviceContext);
604                            }
605                            else {
606                                    importedCategory = MBCategoryLocalServiceUtil.updateCategory(
607                                            existingCategory.getCategoryId(), parentCategoryId,
608                                            category.getName(), category.getDescription(), emailAddress,
609                                            inProtocol, inServerName, inServerPort, inUseSSL,
610                                            inUserName, inPassword, inReadInterval, outEmailAddress,
611                                            outCustom, outServerName, outServerPort, outUseSSL,
612                                            outUserName, outPassword, mailingListActive, false,
613                                            serviceContext);
614                            }
615                    }
616                    else {
617                            importedCategory = MBCategoryLocalServiceUtil.addCategory(
618                                    userId, parentCategoryId, category.getName(),
619                                    category.getDescription(), emailAddress, inProtocol,
620                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
621                                    inReadInterval, outEmailAddress, outCustom, outServerName,
622                                    outServerPort, outUseSSL, outUserName, outPassword,
623                                    mailingListActive, serviceContext);
624                    }
625    
626                    categoryPKs.put(
627                            category.getCategoryId(), importedCategory.getCategoryId());
628    
629                    context.importPermissions(
630                            MBCategory.class, category.getCategoryId(),
631                            importedCategory.getCategoryId());
632            }
633    
634            protected void importMessage(
635                            PortletDataContext context, Element messageElement,
636                            MBMessage message)
637                    throws Exception {
638    
639                    long userId = context.getUserId(message.getUserUuid());
640                    String userName = message.getUserName();
641    
642                    Map<Long, Long> categoryPKs =
643                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBCategory.class);
644    
645                    long categoryId = MapUtil.getLong(
646                            categoryPKs, message.getCategoryId(), message.getCategoryId());
647    
648                    Map<Long, Long> threadPKs =
649                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBThread.class);
650    
651                    long threadId = MapUtil.getLong(
652                            threadPKs, message.getThreadId(), message.getThreadId());
653    
654                    Map<Long, Long> messagePKs =
655                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBMessage.class);
656    
657                    long parentMessageId = MapUtil.getLong(
658                            messagePKs, message.getParentMessageId(),
659                            message.getParentMessageId());
660    
661                    List<ObjectValuePair<String, byte[]>> files =
662                            new ArrayList<ObjectValuePair<String, byte[]>>();
663                    List<String> existingFiles = new ArrayList<String>();
664    
665                    if (context.getBooleanParameter(_NAMESPACE, "attachments") &&
666                            message.isAttachments()) {
667    
668                            List<Element> attachmentElements = messageElement.elements(
669                                    "attachment");
670    
671                            for (Element attachmentElement : attachmentElements) {
672                                    String name = attachmentElement.attributeValue("name");
673                                    String binPath = attachmentElement.attributeValue("bin-path");
674    
675                                    byte[] bytes = context.getZipEntryAsByteArray(binPath);
676    
677                                    files.add(new ObjectValuePair<String, byte[]>(name, bytes));
678                            }
679    
680                            if (files.size() <= 0) {
681                                    _log.error(
682                                            "Could not find attachments for message " +
683                                                    message.getMessageId());
684                            }
685                    }
686    
687                    String[] assetTagNames = null;
688    
689                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
690                            assetTagNames = context.getAssetTagNames(
691                                    MBMessage.class, message.getMessageId());
692                    }
693    
694                    ServiceContext serviceContext = new ServiceContext();
695    
696                    serviceContext.setAddCommunityPermissions(true);
697                    serviceContext.setAddGuestPermissions(true);
698                    serviceContext.setAssetTagNames(assetTagNames);
699                    serviceContext.setCreateDate(message.getCreateDate());
700                    serviceContext.setModifiedDate(message.getModifiedDate());
701                    serviceContext.setScopeGroupId(context.getScopeGroupId());
702    
703                    if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
704                            serviceContext.setWorkflowAction(
705                                    WorkflowConstants.ACTION_SAVE_DRAFT);
706                    }
707    
708                    if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
709                            (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
710                            (categoryId == message.getCategoryId())) {
711    
712                            String path = getImportCategoryPath(context, categoryId);
713    
714                            MBCategory category = (MBCategory)context.getZipEntryAsObject(path);
715    
716                            importCategory(context, category);
717    
718                            categoryId = MapUtil.getLong(
719                                    categoryPKs, message.getCategoryId(), message.getCategoryId());
720                    }
721    
722                    MBMessage importedMessage = null;
723    
724                    if (context.isDataStrategyMirror()) {
725                            MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(
726                                    message.getUuid(), context.getScopeGroupId());
727    
728                            if (existingMessage == null) {
729                                    serviceContext.setUuid(message.getUuid());
730    
731                                    importedMessage = MBMessageLocalServiceUtil.addMessage(
732                                            userId, userName, context.getScopeGroupId(), categoryId,
733                                            threadId, parentMessageId, message.getSubject(),
734                                            message.getBody(), files, message.getAnonymous(),
735                                            message.getPriority(), message.getAllowPingbacks(),
736                                            serviceContext);
737                            }
738                            else {
739                                    importedMessage = MBMessageLocalServiceUtil.updateMessage(
740                                            userId, existingMessage.getMessageId(),
741                                            message.getSubject(), message.getBody(), files,
742                                            existingFiles, message.getPriority(),
743                                            message.getAllowPingbacks(), serviceContext);
744                            }
745                    }
746                    else {
747                            importedMessage = MBMessageLocalServiceUtil.addMessage(
748                                    userId, userName, context.getScopeGroupId(), categoryId,
749                                    threadId, parentMessageId, message.getSubject(),
750                                    message.getBody(), files, message.getAnonymous(),
751                                    message.getPriority(), message.getAllowPingbacks(),
752                                    serviceContext);
753                    }
754    
755                    threadPKs.put(message.getThreadId(), importedMessage.getThreadId());
756                    messagePKs.put(message.getMessageId(), importedMessage.getMessageId());
757    
758                    context.importLocks(
759                            MBThread.class, String.valueOf(message.getThreadId()),
760                            String.valueOf(importedMessage.getThreadId()));
761    
762                    context.importPermissions(
763                            MBMessage.class, message.getMessageId(),
764                            importedMessage.getMessageId());
765    
766                    if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
767                            context.importRatingsEntries(
768                                    MBMessage.class, message.getMessageId(),
769                                    importedMessage.getMessageId());
770                    }
771            }
772    
773            protected void importMessageFlag(
774                            PortletDataContext context, MBMessageFlag flag)
775                    throws Exception {
776    
777                    long userId = context.getUserId(flag.getUserUuid());
778    
779                    Map<Long, Long> messagePKs =
780                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBMessage.class);
781    
782                    long messageId = MapUtil.getLong(
783                            messagePKs, flag.getMessageId(), flag.getMessageId());
784    
785                    MBMessage message = MBMessageUtil.findByPrimaryKey(messageId);
786    
787                    MBThread thread = message.getThread();
788    
789                    MBMessageFlagLocalServiceUtil.addReadFlags(userId, thread);
790            }
791    
792            private static final String _NAMESPACE = "message_board";
793    
794            private static Log _log = LogFactoryUtil.getLog(
795                    MBPortletDataHandlerImpl.class);
796    
797            private static PortletDataHandlerBoolean _attachments =
798                    new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
799    
800            private static PortletDataHandlerBoolean _categoriesAndMessages =
801                    new PortletDataHandlerBoolean(
802                            _NAMESPACE, "categories-and-messages", true, true);
803    
804            private static PortletDataHandlerBoolean _messageFlags =
805                    new PortletDataHandlerBoolean(_NAMESPACE, "message-flags");
806    
807            private static PortletDataHandlerBoolean _ratings =
808                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
809    
810            private static PortletDataHandlerBoolean _tags =
811                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
812    
813            private static PortletDataHandlerBoolean _userBans =
814                    new PortletDataHandlerBoolean(_NAMESPACE, "user-bans");
815    
816    }