001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portlet.messageboards.util.MBUtil;
055    
056    import java.util.ArrayList;
057    import java.util.Iterator;
058    import java.util.List;
059    import java.util.Map;
060    
061    import javax.portlet.PortletPreferences;
062    
063    /**
064     * @author Bruno Farache
065     * @author Raymond Augé
066     */
067    public class MBPortletDataHandlerImpl extends BasePortletDataHandler {
068    
069            public PortletDataHandlerControl[] getExportControls() {
070                    return new PortletDataHandlerControl[] {
071                            _categoriesAndMessages, _attachments, _messageFlags, _userBans,
072                            _ratings, _tags
073                    };
074            }
075    
076            public PortletDataHandlerControl[] getImportControls() {
077                    return new PortletDataHandlerControl[] {
078                            _categoriesAndMessages, _attachments, _messageFlags, _userBans,
079                            _ratings, _tags
080                    };
081            }
082    
083            protected PortletPreferences doDeleteData(
084                            PortletDataContext context, String portletId,
085                            PortletPreferences preferences)
086                    throws Exception {
087    
088                    if (!context.addPrimaryKey(
089                                    MBPortletDataHandlerImpl.class, "deleteData")) {
090    
091                            MBCategoryLocalServiceUtil.deleteCategories(
092                                    context.getScopeGroupId());
093    
094                            MBThreadLocalServiceUtil.deleteThreads(
095                                    context.getScopeGroupId(),
096                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
097                    }
098    
099                    return null;
100            }
101    
102            protected String doExportData(
103                            PortletDataContext context, String portletId,
104                            PortletPreferences preferences)
105                    throws Exception {
106    
107                    context.addPermissions(
108                            "com.liferay.portlet.messageboards", context.getScopeGroupId());
109    
110                    Document document = SAXReaderUtil.createDocument();
111    
112                    Element rootElement = document.addElement("message-boards-data");
113    
114                    rootElement.addAttribute(
115                            "group-id", String.valueOf(context.getScopeGroupId()));
116    
117                    Element categoriesElement = rootElement.addElement("categories");
118                    Element messagesElement = rootElement.addElement("messages");
119                    Element messageFlagsElement = rootElement.addElement("message-flags");
120                    Element userBansElement = rootElement.addElement("user-bans");
121    
122                    List<MBCategory> categories = MBCategoryUtil.findByGroupId(
123                            context.getScopeGroupId());
124    
125                    for (MBCategory category : categories) {
126                            exportCategory(
127                                    context, categoriesElement, messagesElement,
128                                    messageFlagsElement, category);
129                    }
130    
131                    List<MBMessage> messages = MBMessageUtil.findByG_C(
132                            context.getScopeGroupId(),
133                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
134    
135                    for (MBMessage message : messages) {
136                            exportMessage(
137                                    context, categoriesElement, messagesElement,
138                                    messageFlagsElement, message);
139                    }
140    
141                    if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
142                            List<MBBan> bans = MBBanUtil.findByGroupId(
143                                    context.getScopeGroupId());
144    
145                            for (MBBan ban : bans) {
146                                    exportBan(context, userBansElement, ban);
147                            }
148                    }
149    
150                    return document.formattedString();
151            }
152    
153            protected PortletPreferences doImportData(
154                            PortletDataContext context, String portletId,
155                            PortletPreferences preferences, String data)
156                    throws Exception {
157    
158                    context.importPermissions(
159                            "com.liferay.portlet.messageboards", context.getSourceGroupId(),
160                            context.getScopeGroupId());
161    
162                    Document document = SAXReaderUtil.read(data);
163    
164                    Element rootElement = document.getRootElement();
165    
166                    Element categoriesElement = rootElement.element("categories");
167    
168                    for (Element categoryElement : categoriesElement.elements("category")) {
169                            String path = categoryElement.attributeValue("path");
170    
171                            if (!context.isPathNotProcessed(path)) {
172                                    continue;
173                            }
174    
175                            MBCategory category = (MBCategory)context.getZipEntryAsObject(path);
176    
177                            importCategory(context, category);
178                    }
179    
180                    Element messagesElement = rootElement.element("messages");
181    
182                    for (Element messageElement : messagesElement.elements("message")) {
183                            String path = messageElement.attributeValue("path");
184    
185                            if (!context.isPathNotProcessed(path)) {
186                                    continue;
187                            }
188    
189                            MBMessage message = (MBMessage)context.getZipEntryAsObject(
190                                    path);
191    
192                            importMessage(context, messageElement, message);
193                    }
194    
195                    if (context.getBooleanParameter(_NAMESPACE, "message-flags")) {
196                            Element messageFlagsElement = rootElement.element("message-flags");
197    
198                            for (Element messageFlagElement :
199                                            messageFlagsElement.elements("message-flag")) {
200    
201                                    String path = messageFlagElement.attributeValue("path");
202    
203                                    if (!context.isPathNotProcessed(path)) {
204                                            continue;
205                                    }
206    
207                                    MBMessageFlag flag = (MBMessageFlag)context.getZipEntryAsObject(
208                                            path);
209    
210                                    importMessageFlag(context, flag);
211                            }
212                    }
213    
214                    if (context.getBooleanParameter(_NAMESPACE, "user-bans")) {
215                            Element userBansElement = rootElement.element("user-bans");
216    
217                            for (Element userBanElement :
218                                            userBansElement.elements("user-ban")) {
219    
220                                    String path = userBanElement.attributeValue("path");
221    
222                                    if (!context.isPathNotProcessed(path)) {
223                                            continue;
224                                    }
225    
226                                    MBBan ban = (MBBan)context.getZipEntryAsObject(path);
227    
228                                    importBan(context, ban);
229                            }
230                    }
231    
232                    return null;
233            }
234    
235            protected void exportBan(
236                            PortletDataContext context, Element userBansElement, MBBan ban)
237                    throws Exception {
238    
239                    if (!context.isWithinDateRange(ban.getModifiedDate())) {
240                            return;
241                    }
242    
243                    String path = getUserBanPath(context, ban);
244    
245                    if (!context.isPathNotProcessed(path)) {
246                            return;
247                    }
248    
249                    Element userBanElement = userBansElement.addElement("user-ban");
250    
251                    userBanElement.addAttribute("path", path);
252    
253                    ban.setBanUserUuid(ban.getBanUserUuid());
254                    ban.setUserUuid(ban.getUserUuid());
255    
256                    context.addZipEntry(path, ban);
257            }
258    
259            protected void exportCategory(
260                            PortletDataContext context, Element categoriesElement,
261                            Element messagesElement, Element messageFlagsElement,
262                            MBCategory category)
263                    throws Exception {
264    
265                    if (context.isWithinDateRange(category.getModifiedDate())) {
266                            exportParentCategory(
267                                    context, categoriesElement, category.getParentCategoryId());
268    
269                            String path = getCategoryPath(context, category);
270    
271                            if (context.isPathNotProcessed(path)) {
272                                    Element categoryElement = categoriesElement.addElement(
273                                            "category");
274    
275                                    categoryElement.addAttribute("path", path);
276    
277                                    category.setUserUuid(category.getUserUuid());
278    
279                                    context.addPermissions(
280                                            MBCategory.class, category.getCategoryId());
281    
282                                    context.addZipEntry(path, category);
283                            }
284                    }
285    
286                    List<MBMessage> messages = MBMessageUtil.findByG_C(
287                            category.getGroupId(), category.getCategoryId());
288    
289                    for (MBMessage message : messages) {
290                            exportMessage(
291                                    context, categoriesElement, messagesElement,
292                                    messageFlagsElement, message);
293                    }
294            }
295    
296            protected void exportMessage(
297                            PortletDataContext context, Element categoriesElement,
298                            Element messagesElement, Element messageFlagsElement,
299                            MBMessage message)
300                    throws Exception {
301    
302                    if (!context.isWithinDateRange(message.getModifiedDate())) {
303                            return;
304                    }
305    
306                    if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
307                            return;
308                    }
309    
310                    exportParentCategory(
311                            context, categoriesElement, message.getCategoryId());
312    
313                    String path = getMessagePath(context, message);
314    
315                    if (context.isPathNotProcessed(path)) {
316                            Element messageElement = messagesElement.addElement("message");
317    
318                            messageElement.addAttribute("path", path);
319    
320                            message.setUserUuid(message.getUserUuid());
321                            message.setPriority(message.getPriority());
322    
323                            context.addPermissions(MBMessage.class, message.getMessageId());
324    
325                            context.addLocks(
326                                    MBThread.class, String.valueOf(message.getThreadId()));
327    
328                            if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
329                                    context.addRatingsEntries(
330                                            MBMessage.class, message.getMessageId());
331                            }
332    
333                            if (context.getBooleanParameter(_NAMESPACE, "tags")) {
334                                    context.addAssetTags(MBMessage.class, message.getMessageId());
335                            }
336    
337                            if (context.getBooleanParameter(_NAMESPACE, "attachments") &&
338                                    message.isAttachments()) {
339    
340                                    for (String attachment : message.getAttachmentsFiles()) {
341                                            int pos = attachment.lastIndexOf(CharPool.FORWARD_SLASH);
342    
343                                            String name = attachment.substring(pos + 1);
344                                            String binPath = getMessageAttachementBinPath(
345                                                    context, message, name);
346    
347                                            Element attachmentElement = messageElement.addElement(
348                                                    "attachment");
349    
350                                            attachmentElement.addAttribute("name", name);
351                                            attachmentElement.addAttribute("bin-path", binPath);
352    
353                                            byte[] bytes = DLServiceUtil.getFile(
354                                                    context.getCompanyId(), CompanyConstants.SYSTEM,
355                                                    attachment);
356    
357                                            context.addZipEntry(binPath, bytes);
358                                    }
359    
360                                    message.setAttachmentsDir(message.getAttachmentsDir());
361                            }
362    
363                            if (context.getBooleanParameter(_NAMESPACE, "message-flags")) {
364                                    List<MBMessageFlag> messageFlags =
365                                            MBMessageFlagUtil.findByMessageId(message.getMessageId());
366    
367                                    for (MBMessageFlag messageFlag : messageFlags) {
368                                            exportMessageFlag(
369                                                    context, messageFlagsElement, messageFlag);
370                                    }
371                            }
372    
373                            context.addZipEntry(path, message);
374                    }
375            }
376    
377            protected void exportMessageFlag(
378                            PortletDataContext context, Element messageFlagsElement,
379                            MBMessageFlag messageFlag)
380                    throws Exception {
381    
382                    String path = getMessageFlagPath(context, messageFlag);
383    
384                    if (!context.isPathNotProcessed(path)) {
385                            return;
386                    }
387    
388                    Element messageFlagElement = messageFlagsElement.addElement(
389                            "message-flag");
390    
391                    messageFlagElement.addAttribute("path", path);
392    
393                    messageFlag.setUserUuid(messageFlag.getUserUuid());
394    
395                    context.addZipEntry(path, messageFlag);
396            }
397    
398            protected void exportParentCategory(
399                            PortletDataContext context, Element categoriesElement,
400                            long categoryId)
401                    throws Exception {
402    
403                    if ((!context.hasDateRange()) ||
404                            MBUtil.isDefaultParentCategoryId(categoryId) ||
405                            MBUtil.isDiscussionCategoryId(categoryId)) {
406    
407                            return;
408                    }
409    
410                    MBCategory category = MBCategoryUtil.findByPrimaryKey(categoryId);
411    
412                    exportParentCategory(
413                            context, categoriesElement, category.getParentCategoryId());
414    
415                    String path = getCategoryPath(context, category);
416    
417                    if (context.isPathNotProcessed(path)) {
418                            Element categoryElement = categoriesElement.addElement("category");
419    
420                            categoryElement.addAttribute("path", path);
421    
422                            category.setUserUuid(category.getUserUuid());
423    
424                            context.addPermissions(MBCategory.class, category.getCategoryId());
425    
426                            context.addZipEntry(path, category);
427                    }
428            }
429    
430            protected String getCategoryPath(
431                    PortletDataContext context, MBCategory category) {
432    
433                    StringBundler sb = new StringBundler(4);
434    
435                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
436                    sb.append("/categories/");
437                    sb.append(category.getCategoryId());
438                    sb.append(".xml");
439    
440                    return sb.toString();
441            }
442    
443            protected String getImportCategoryPath(
444                    PortletDataContext context, long categoryId) {
445    
446                    StringBundler sb = new StringBundler(4);
447    
448                    sb.append(context.getSourcePortletPath(PortletKeys.MESSAGE_BOARDS));
449                    sb.append("/categories/");
450                    sb.append(categoryId);
451                    sb.append(".xml");
452    
453                    return sb.toString();
454            }
455    
456            protected String getMessageAttachementBinPath(
457                    PortletDataContext context, MBMessage message, String attachment) {
458    
459                    StringBundler sb = new StringBundler(5);
460    
461                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
462                    sb.append("/bin/");
463                    sb.append(message.getMessageId());
464                    sb.append(StringPool.SLASH);
465                    sb.append(PortalUUIDUtil.generate());
466    
467                    return sb.toString();
468            }
469    
470            protected String getMessageFlagPath(
471                    PortletDataContext context, MBMessageFlag messageFlag) {
472    
473                    StringBundler sb = new StringBundler(4);
474    
475                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
476                    sb.append("/message-flags/");
477                    sb.append(messageFlag.getMessageFlagId());
478                    sb.append(".xml");
479    
480                    return sb.toString();
481            }
482    
483            protected String getMessagePath(
484                    PortletDataContext context, MBMessage message) {
485    
486                    StringBundler sb = new StringBundler(4);
487    
488                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
489                    sb.append("/messages/");
490                    sb.append(message.getMessageId());
491                    sb.append(".xml");
492    
493                    return sb.toString();
494            }
495    
496            protected String getUserBanPath(PortletDataContext context, MBBan ban) {
497                    StringBundler sb = new StringBundler(4);
498    
499                    sb.append(context.getPortletPath(PortletKeys.MESSAGE_BOARDS));
500                    sb.append("/user-bans/");
501                    sb.append(ban.getBanId());
502                    sb.append(".xml");
503    
504                    return sb.toString();
505            }
506    
507            protected void importBan(PortletDataContext context, MBBan ban)
508                    throws Exception {
509    
510                    long userId = context.getUserId(ban.getUserUuid());
511    
512                    ServiceContext serviceContext = new ServiceContext();
513    
514                    serviceContext.setCreateDate(ban.getCreateDate());
515                    serviceContext.setModifiedDate(ban.getModifiedDate());
516                    serviceContext.setScopeGroupId(context.getScopeGroupId());
517    
518                    List<User> users = UserUtil.findByUuid(ban.getBanUserUuid());
519    
520                    Iterator<User> itr = users.iterator();
521    
522                    if (itr.hasNext()) {
523                            User user = itr.next();
524    
525                            MBBanLocalServiceUtil.addBan(
526                                    userId, user.getUserId(), serviceContext);
527                    }
528                    else {
529                            _log.error(
530                                    "Could not find banned user with uuid " + ban.getBanUserUuid());
531                    }
532            }
533    
534            protected void importCategory(
535                            PortletDataContext context, MBCategory category)
536                    throws Exception {
537    
538                    long userId = context.getUserId(category.getUserUuid());
539    
540                    Map<Long, Long> categoryPKs =
541                            (Map<Long, Long>)context.getNewPrimaryKeysMap(MBCategory.class);
542    
543                    long parentCategoryId = MapUtil.getLong(
544                            categoryPKs, category.getParentCategoryId(),
545                            category.getParentCategoryId());
546    
547                    String emailAddress = null;
548                    String inProtocol = null;
549                    String inServerName = null;
550                    int inServerPort = 0;
551                    boolean inUseSSL = false;
552                    String inUserName = null;
553                    String inPassword = null;
554                    int inReadInterval = 0;
555                    String outEmailAddress = null;
556                    boolean outCustom = false;
557                    String outServerName = null;
558                    int outServerPort = 0;
559                    boolean outUseSSL = false;
560                    String outUserName = null;
561                    String outPassword = null;
562                    boolean mailingListActive = false;
563    
564                    ServiceContext serviceContext = new ServiceContext();
565    
566                    serviceContext.setAddCommunityPermissions(true);
567                    serviceContext.setAddGuestPermissions(true);
568                    serviceContext.setCreateDate(category.getCreateDate());
569                    serviceContext.setModifiedDate(category.getModifiedDate());
570                    serviceContext.setScopeGroupId(context.getScopeGroupId());
571    
572                    if (!MBUtil.isDefaultParentCategoryId(parentCategoryId) &&
573                            !MBUtil.isDiscussionCategoryId(parentCategoryId) &&
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 (!MBUtil.isDefaultParentCategoryId(categoryId) &&
709                            !MBUtil.isDiscussionCategoryId(categoryId) &&
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    }