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