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