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