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