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.journal.lar;
016    
017    import com.liferay.portal.kernel.lar.DataLevel;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
036    import com.liferay.portlet.journal.model.JournalArticle;
037    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
038    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
039    import com.liferay.portlet.journal.service.permission.JournalPermission;
040    
041    import java.util.Map;
042    
043    import javax.portlet.PortletPreferences;
044    
045    /**
046     * <p>
047     * Provides the Journal Content portlet export and import functionality, which
048     * is to clone the article, structure, and template referenced in the Journal
049     * Content portlet if the article is associated with the layout's group. Upon
050     * import, a new instance of the corresponding article, structure, and template
051     * will be created or updated. The author of the newly created objects are
052     * determined by the JournalCreationStrategy class defined in
053     * <i>portal.properties</i>.
054     * </p>
055     *
056     * <p>
057     * This <code>PortletDataHandler</code> differs from from
058     * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
059     * referenced in Journal Content portlets. Articles not displayed in Journal
060     * Content portlets will not be exported unless
061     * <code>JournalPortletDataHandlerImpl</code> is activated.
062     * </p>
063     *
064     * @author Joel Kozikowski
065     * @author Raymond Aug??
066     * @author Bruno Farache
067     * @author Daniel Kocsis
068     * @see    com.liferay.portal.kernel.lar.PortletDataHandler
069     * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
070     * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandler
071     */
072    public class JournalContentPortletDataHandler
073            extends JournalPortletDataHandler {
074    
075            public JournalContentPortletDataHandler() {
076                    setDataLevel(DataLevel.PORTLET_INSTANCE);
077                    setDataPortletPreferences("articleId", "ddmTemplateKey", "groupId");
078                    setExportControls(
079                            new PortletDataHandlerBoolean(
080                                    null, "selected-web-content", true, true, null,
081                                    JournalArticle.class.getName()));
082                    setPublishToLiveByDefault(
083                            PropsValues.JOURNAL_CONTENT_PUBLISH_TO_LIVE_BY_DEFAULT);
084            }
085    
086            @Override
087            protected PortletPreferences doDeleteData(
088                            PortletDataContext portletDataContext, String portletId,
089                            PortletPreferences portletPreferences)
090                    throws Exception {
091    
092                    if (portletPreferences == null) {
093                            return portletPreferences;
094                    }
095    
096                    portletPreferences.setValue("articleId", StringPool.BLANK);
097                    portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
098                    portletPreferences.setValue("groupId", StringPool.BLANK);
099    
100                    return portletPreferences;
101            }
102    
103            @Override
104            protected PortletPreferences doProcessExportPortletPreferences(
105                            PortletDataContext portletDataContext, String portletId,
106                            PortletPreferences portletPreferences)
107                    throws Exception {
108    
109                    portletDataContext.addPortletPermissions(
110                            JournalPermission.RESOURCE_NAME);
111    
112                    String articleId = portletPreferences.getValue("articleId", null);
113    
114                    if (articleId == null) {
115                            if (_log.isDebugEnabled()) {
116                                    _log.debug(
117                                            "No article ID found in preferences of portlet " +
118                                                    portletId);
119                            }
120    
121                            return portletPreferences;
122                    }
123    
124                    long articleGroupId = GetterUtil.getLong(
125                            portletPreferences.getValue("groupId", StringPool.BLANK));
126    
127                    if (articleGroupId <= 0) {
128                            if (_log.isWarnEnabled()) {
129                                    _log.warn(
130                                            "No group ID found in preferences of portlet " + portletId);
131                            }
132    
133                            return portletPreferences;
134                    }
135    
136                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
137    
138                    if (articleGroupId != previousScopeGroupId) {
139                            portletDataContext.setScopeGroupId(articleGroupId);
140                    }
141    
142                    JournalArticle article = null;
143    
144                    article = JournalArticleLocalServiceUtil.fetchLatestArticle(
145                            articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
146    
147                    if (article == null) {
148                            article = JournalArticleLocalServiceUtil.fetchLatestArticle(
149                                    articleGroupId, articleId, WorkflowConstants.STATUS_EXPIRED);
150                    }
151    
152                    if (article == null) {
153                            if (_log.isWarnEnabled()) {
154                                    _log.warn(
155                                            "Portlet " + portletId +
156                                                    " refers to an invalid article ID " + articleId);
157                            }
158    
159                            portletDataContext.setScopeGroupId(previousScopeGroupId);
160    
161                            return portletPreferences;
162                    }
163    
164                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
165                            portletDataContext, portletId, article);
166    
167                    String defaultTemplateId = article.getTemplateId();
168                    String preferenceTemplateId = portletPreferences.getValue(
169                            "ddmTemplateKey", null);
170    
171                    if (Validator.isNotNull(defaultTemplateId) &&
172                            Validator.isNotNull(preferenceTemplateId) &&
173                            !defaultTemplateId.equals(preferenceTemplateId)) {
174    
175                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
176                                    article.getGroupId(),
177                                    PortalUtil.getClassNameId(DDMStructure.class),
178                                    preferenceTemplateId, true);
179    
180                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
181                                    portletDataContext, article, ddmTemplate,
182                                    PortletDataContext.REFERENCE_TYPE_STRONG);
183                    }
184    
185                    portletDataContext.setScopeGroupId(previousScopeGroupId);
186    
187                    return portletPreferences;
188            }
189    
190            @Override
191            protected PortletPreferences doProcessImportPortletPreferences(
192                            PortletDataContext portletDataContext, String portletId,
193                            PortletPreferences portletPreferences)
194                    throws Exception {
195    
196                    portletDataContext.importPortletPermissions(
197                            JournalPermission.RESOURCE_NAME);
198    
199                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
200    
201                    Map<Long, Long> groupIds =
202                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
203                                    Group.class);
204    
205                    long importGroupId = GetterUtil.getLong(
206                            portletPreferences.getValue("groupId", null));
207    
208                    long groupId = MapUtil.getLong(groupIds, importGroupId, importGroupId);
209    
210                    portletDataContext.setScopeGroupId(groupId);
211    
212                    StagedModelDataHandlerUtil.importReferenceStagedModels(
213                            portletDataContext, DDMStructure.class);
214    
215                    StagedModelDataHandlerUtil.importReferenceStagedModels(
216                            portletDataContext, DDMTemplate.class);
217    
218                    StagedModelDataHandlerUtil.importReferenceStagedModels(
219                            portletDataContext, JournalArticle.class);
220    
221                    String articleId = portletPreferences.getValue("articleId", null);
222    
223                    if (Validator.isNotNull(articleId)) {
224                            Map<String, String> articleIds =
225                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
226                                            JournalArticle.class + ".articleId");
227    
228                            articleId = MapUtil.getString(articleIds, articleId, articleId);
229    
230                            portletPreferences.setValue("articleId", articleId);
231    
232                            portletPreferences.setValue("groupId", String.valueOf(groupId));
233    
234                            Layout layout = LayoutLocalServiceUtil.getLayout(
235                                    portletDataContext.getPlid());
236    
237                            JournalContentSearchLocalServiceUtil.updateContentSearch(
238                                    layout.getGroupId(), layout.isPrivateLayout(),
239                                    layout.getLayoutId(), portletId, articleId, true);
240                    }
241                    else {
242                            portletPreferences.setValue("groupId", StringPool.BLANK);
243                            portletPreferences.setValue("articleId", StringPool.BLANK);
244                    }
245    
246                    String ddmTemplateKey = portletPreferences.getValue(
247                            "ddmTemplateKey", null);
248    
249                    if (Validator.isNotNull(ddmTemplateKey)) {
250                            Map<String, String> ddmTemplateKeys =
251                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
252                                            DDMTemplate.class + ".ddmTemplateKey");
253    
254                            ddmTemplateKey = MapUtil.getString(
255                                    ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
256    
257                            portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
258                    }
259                    else {
260                            portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
261                    }
262    
263                    portletDataContext.setScopeGroupId(previousScopeGroupId);
264    
265                    return portletPreferences;
266            }
267    
268            private static final Log _log = LogFactoryUtil.getLog(
269                    JournalContentPortletDataHandler.class);
270    
271    }