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