001    /**
002     * Copyright (c) 2000-2012 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.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
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.Document;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    import com.liferay.portlet.journal.NoSuchArticleException;
034    import com.liferay.portlet.journal.model.JournalArticle;
035    import com.liferay.portlet.journal.model.JournalTemplate;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
038    
039    import java.util.Map;
040    
041    import javax.portlet.PortletPreferences;
042    
043    /**
044     * <p>
045     * Provides the Journal Content portlet export and import functionality, which
046     * is to clone the article, structure, and template referenced in the Journal
047     * Content portlet if the article is associated with the layout's group. Upon
048     * import, a new instance of the corresponding article, structure, and template
049     * will be created or updated. The author of the newly created objects are
050     * determined by the JournalCreationStrategy class defined in
051     * <i>portal.properties</i>.
052     * </p>
053     *
054     * <p>
055     * This <code>PortletDataHandler</code> differs from from
056     * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
057     * referenced in Journal Content portlets. Articles not displayed in Journal
058     * Content portlets will not be exported unless
059     * <code>JournalPortletDataHandlerImpl</code> is activated.
060     * </p>
061     *
062     * @author Joel Kozikowski
063     * @author Raymond Augé
064     * @author Bruno Farache
065     * @see    com.liferay.portal.kernel.lar.PortletDataHandler
066     * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
067     * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
068     */
069    public class JournalContentPortletDataHandlerImpl
070            extends BasePortletDataHandler {
071    
072            @Override
073            public PortletDataHandlerControl[] getExportControls() {
074                    return new PortletDataHandlerControl[] {
075                            _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
076                            _tags
077                    };
078            }
079    
080            @Override
081            public PortletDataHandlerControl[] getImportControls() {
082                    return new PortletDataHandlerControl[] {
083                            _selectedArticles, _images, _comments, _ratings, _tags
084                    };
085            }
086    
087            @Override
088            public boolean isAlwaysExportable() {
089                    return _ALWAYS_EXPORTABLE;
090            }
091    
092            @Override
093            public boolean isAlwaysStaged() {
094                    return _ALWAYS_STAGED;
095            }
096    
097            @Override
098            public boolean isPublishToLiveByDefault() {
099                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
100            }
101    
102            @Override
103            protected PortletPreferences doDeleteData(
104                            PortletDataContext portletDataContext, String portletId,
105                            PortletPreferences portletPreferences)
106                    throws Exception {
107    
108                    portletPreferences.setValue("groupId", StringPool.BLANK);
109                    portletPreferences.setValue("articleId", StringPool.BLANK);
110    
111                    return portletPreferences;
112            }
113    
114            @Override
115            protected String doExportData(
116                            PortletDataContext portletDataContext, String portletId,
117                            PortletPreferences portletPreferences)
118                    throws Exception {
119    
120                    portletDataContext.addPermissions(
121                            "com.liferay.portlet.journal",
122                            portletDataContext.getScopeGroupId());
123    
124                    String articleId = portletPreferences.getValue("articleId", null);
125    
126                    if (articleId == null) {
127                            if (_log.isWarnEnabled()) {
128                                    _log.warn(
129                                            "No article id found in preferences of portlet " +
130                                                    portletId);
131                            }
132    
133                            return StringPool.BLANK;
134                    }
135    
136                    long articleGroupId = GetterUtil.getLong(
137                            portletPreferences.getValue("groupId", StringPool.BLANK));
138    
139                    if (articleGroupId <= 0) {
140                            if (_log.isWarnEnabled()) {
141                                    _log.warn(
142                                            "No group id found in preferences of portlet " + portletId);
143                            }
144    
145                            return StringPool.BLANK;
146                    }
147    
148                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
149    
150                    if (articleGroupId != portletDataContext.getScopeGroupId()) {
151                            portletDataContext.setScopeGroupId(articleGroupId);
152                    }
153    
154                    JournalArticle article = null;
155    
156                    try {
157                            article = JournalArticleLocalServiceUtil.getLatestArticle(
158                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
159                    }
160                    catch (NoSuchArticleException nsae) {
161                    }
162    
163                    if (article == null) {
164                            try {
165                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
166                                            articleGroupId, articleId,
167                                            WorkflowConstants.STATUS_EXPIRED);
168                            }
169                            catch (NoSuchArticleException nsae) {
170                            }
171                    }
172    
173                    Document document = SAXReaderUtil.createDocument();
174    
175                    Element rootElement = document.addElement("journal-content-data");
176    
177                    if (article == null) {
178                            portletDataContext.setScopeGroupId(previousScopeGroupId);
179    
180                            return document.formattedString();
181                    }
182    
183                    String path = JournalPortletDataHandlerImpl.getArticlePath(
184                            portletDataContext, article);
185    
186                    Element articleElement = rootElement.addElement("article");
187    
188                    articleElement.addAttribute("path", path);
189    
190                    Element dlFileEntryTypesElement = rootElement.addElement(
191                            "dl-file-entry-types");
192                    Element dlFoldersElement = rootElement.addElement("dl-folders");
193                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
194                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
195    
196                    JournalPortletDataHandlerImpl.exportArticle(
197                            portletDataContext, rootElement, rootElement, rootElement,
198                            dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
199                            dlFileRanksElement, article, false);
200    
201                    portletDataContext.setScopeGroupId(previousScopeGroupId);
202    
203                    return document.formattedString();
204            }
205    
206            @Override
207            protected PortletPreferences doImportData(
208                            PortletDataContext portletDataContext, String portletId,
209                            PortletPreferences portletPreferences, String data)
210                    throws Exception {
211    
212                    portletDataContext.importPermissions(
213                            "com.liferay.portlet.journal",
214                            portletDataContext.getSourceGroupId(),
215                            portletDataContext.getScopeGroupId());
216    
217                    if (Validator.isNull(data)) {
218                            return null;
219                    }
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                    Document document = SAXReaderUtil.read(data);
231    
232                    Element rootElement = document.getRootElement();
233    
234                    JournalPortletDataHandlerImpl.importReferencedData(
235                            portletDataContext, rootElement);
236    
237                    Element structureElement = rootElement.element("structure");
238    
239                    if (structureElement != null) {
240                            JournalPortletDataHandlerImpl.importStructure(
241                                    portletDataContext, structureElement);
242                    }
243    
244                    Element templateElement = rootElement.element("template");
245    
246                    if (templateElement != null) {
247                            JournalPortletDataHandlerImpl.importTemplate(
248                                    portletDataContext, templateElement);
249                    }
250    
251                    Element articleElement = rootElement.element("article");
252    
253                    if (articleElement != null) {
254                            JournalPortletDataHandlerImpl.importArticle(
255                                    portletDataContext, articleElement);
256                    }
257    
258                    String articleId = portletPreferences.getValue("articleId", null);
259    
260                    if (Validator.isNotNull(articleId) && (articleElement != null)) {
261                            String importedArticleGroupId = articleElement.attributeValue(
262                                    "imported-article-group-id");
263    
264                            if (Validator.isNull(importedArticleGroupId)) {
265                                    importedArticleGroupId = String.valueOf(
266                                            portletDataContext.getScopeGroupId());
267                            }
268    
269                            portletPreferences.setValue("groupId", importedArticleGroupId);
270    
271                            Map<String, String> articleIds =
272                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
273                                            JournalArticle.class + ".articleId");
274    
275                            articleId = MapUtil.getString(articleIds, articleId, articleId);
276    
277                            portletPreferences.setValue("articleId", articleId);
278    
279                            Layout layout = LayoutLocalServiceUtil.getLayout(
280                                    portletDataContext.getPlid());
281    
282                            JournalContentSearchLocalServiceUtil.updateContentSearch(
283                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
284                                    layout.getLayoutId(), portletId, articleId, true);
285                    }
286                    else {
287                            portletPreferences.setValue("groupId", StringPool.BLANK);
288                            portletPreferences.setValue("articleId", StringPool.BLANK);
289                    }
290    
291                    String templateId = portletPreferences.getValue("templateId", null);
292    
293                    if (Validator.isNotNull(templateId)) {
294                            Map<String, String> templateIds =
295                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
296                                            JournalTemplate.class + ".templateId");
297    
298                            templateId = MapUtil.getString(templateIds, templateId, templateId);
299    
300                            portletPreferences.setValue("templateId", templateId);
301                    }
302                    else {
303                            portletPreferences.setValue("templateId", StringPool.BLANK);
304                    }
305    
306                    portletDataContext.setScopeGroupId(previousScopeGroupId);
307    
308                    return portletPreferences;
309            }
310    
311            private static final boolean _ALWAYS_EXPORTABLE = true;
312    
313            private static final boolean _ALWAYS_STAGED = true;
314    
315            private static final String _NAMESPACE = "journal";
316    
317            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
318    
319            private static PortletDataHandlerBoolean _comments =
320                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
321    
322            private static PortletDataHandlerBoolean _embeddedAssets =
323                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
324    
325            private static PortletDataHandlerBoolean _images =
326                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
327    
328            private static Log _log = LogFactoryUtil.getLog(
329                    JournalContentPortletDataHandlerImpl.class);
330    
331            private static PortletDataHandlerBoolean _ratings =
332                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
333    
334            private static PortletDataHandlerBoolean _selectedArticles =
335                    new PortletDataHandlerBoolean(
336                            _NAMESPACE, "selected-web-content", true, true);
337    
338            private static PortletDataHandlerBoolean _tags =
339                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
340    
341    }