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