001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.util.MapUtil;
019    import com.liferay.portal.kernel.workflow.WorkflowConstants;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portlet.asset.model.AssetEntry;
024    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
025    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
026    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
027    import com.liferay.portlet.exportimport.lar.PortletDataContext;
028    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
029    import com.liferay.portlet.messageboards.NoSuchDiscussionException;
030    import com.liferay.portlet.messageboards.model.MBDiscussion;
031    import com.liferay.portlet.messageboards.model.MBMessage;
032    import com.liferay.portlet.messageboards.model.MBThread;
033    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
034    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
035    
036    import java.util.List;
037    import java.util.Map;
038    
039    /**
040     * @author Daniel Kocsis
041     */
042    public class MBDiscussionStagedModelDataHandler
043            extends BaseStagedModelDataHandler<MBDiscussion> {
044    
045            public static final String[] CLASS_NAMES = {MBDiscussion.class.getName()};
046    
047            @Override
048            public void deleteStagedModel(MBDiscussion discussion) {
049                    MBDiscussionLocalServiceUtil.deleteMBDiscussion(discussion);
050            }
051    
052            @Override
053            public void deleteStagedModel(
054                    String uuid, long groupId, String className, String extraData) {
055    
056                    MBDiscussion discussion = fetchStagedModelByUuidAndGroupId(
057                            uuid, groupId);
058    
059                    if (discussion != null) {
060                            deleteStagedModel(discussion);
061                    }
062            }
063    
064            @Override
065            public MBDiscussion fetchStagedModelByUuidAndGroupId(
066                    String uuid, long groupId) {
067    
068                    return MBDiscussionLocalServiceUtil.fetchMBDiscussionByUuidAndGroupId(
069                            uuid, groupId);
070            }
071    
072            @Override
073            public List<MBDiscussion> fetchStagedModelsByUuidAndCompanyId(
074                    String uuid, long companyId) {
075    
076                    return MBDiscussionLocalServiceUtil.getMBDiscussionsByUuidAndCompanyId(
077                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
078                            new StagedModelModifiedDateComparator<MBDiscussion>());
079            }
080    
081            @Override
082            public String[] getClassNames() {
083                    return CLASS_NAMES;
084            }
085    
086            @Override
087            public String getDisplayName(MBDiscussion discussion) {
088                    try {
089                            AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
090                                    discussion.getClassName(), discussion.getClassPK());
091    
092                            return assetEntry.getTitleCurrentValue();
093                    }
094                    catch (Exception e) {
095                            return discussion.getUuid();
096                    }
097            }
098    
099            @Override
100            protected void doExportStagedModel(
101                            PortletDataContext portletDataContext, MBDiscussion discussion)
102                    throws Exception {
103    
104                    Element discussionElement = portletDataContext.getExportDataElement(
105                            discussion);
106    
107                    portletDataContext.addClassedModel(
108                            discussionElement, ExportImportPathUtil.getModelPath(discussion),
109                            discussion);
110            }
111    
112            @Override
113            protected void doImportStagedModel(
114                            PortletDataContext portletDataContext, MBDiscussion discussion)
115                    throws Exception {
116    
117                    long userId = portletDataContext.getUserId(discussion.getUserUuid());
118    
119                    String className = discussion.getClassName();
120    
121                    Map<Long, Long> relatedClassPKs =
122                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(className);
123    
124                    long newClassPK = MapUtil.getLong(
125                            relatedClassPKs, discussion.getClassPK(), discussion.getClassPK());
126    
127                    MBDiscussion existingDiscussion =
128                            MBDiscussionLocalServiceUtil.fetchDiscussion(
129                                    discussion.getClassName(), newClassPK);
130    
131                    if (existingDiscussion == null) {
132                            if (className.equals(Layout.class.getName()) &&
133                                    PropsValues.LAYOUT_COMMENTS_ENABLED) {
134    
135                                    MBMessage rootMessage =
136                                            MBMessageLocalServiceUtil.addDiscussionMessage(
137                                                    userId, discussion.getUserName(),
138                                                    portletDataContext.getScopeGroupId(), className,
139                                                    newClassPK, WorkflowConstants.ACTION_PUBLISH);
140    
141                                    existingDiscussion =
142                                            MBDiscussionLocalServiceUtil.getThreadDiscussion(
143                                                    rootMessage.getThreadId());
144                            }
145                            else {
146                                    throw new NoSuchDiscussionException();
147                            }
148                    }
149    
150                    Map<Long, Long> discussionIds =
151                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
152                                    MBDiscussion.class);
153    
154                    discussionIds.put(
155                            discussion.getDiscussionId(), existingDiscussion.getDiscussionId());
156    
157                    Map<Long, Long> threadIds =
158                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
159                                    MBThread.class);
160    
161                    threadIds.put(
162                            discussion.getThreadId(), existingDiscussion.getThreadId());
163            }
164    
165    }