001    /**
002     * Copyright (c) 2000-2011 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                    Element igFoldersElement = rootElement.addElement("ig-folders");
196                    Element igImagesElement = rootElement.addElement("ig-images");
197    
198                    JournalPortletDataHandlerImpl.exportArticle(
199                            portletDataContext, rootElement, rootElement, rootElement,
200                            dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
201                            dlFileRanksElement, igFoldersElement, igImagesElement, article,
202                            false);
203    
204                    portletDataContext.setScopeGroupId(previousScopeGroupId);
205    
206                    return document.formattedString();
207            }
208    
209            @Override
210            protected PortletPreferences doImportData(
211                            PortletDataContext portletDataContext, String portletId,
212                            PortletPreferences portletPreferences, String data)
213                    throws Exception {
214    
215                    portletDataContext.importPermissions(
216                            "com.liferay.portlet.journal",
217                            portletDataContext.getSourceGroupId(),
218                            portletDataContext.getScopeGroupId());
219    
220                    if (Validator.isNull(data)) {
221                            return null;
222                    }
223    
224                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
225    
226                    long importGroupId = GetterUtil.getLong(
227                            portletPreferences.getValue("groupId", null));
228    
229                    if (importGroupId == portletDataContext.getSourceGroupId()) {
230                            portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
231                    }
232    
233                    Document document = SAXReaderUtil.read(data);
234    
235                    Element rootElement = document.getRootElement();
236    
237                    JournalPortletDataHandlerImpl.importReferencedData(
238                            portletDataContext, rootElement);
239    
240                    Element structureElement = rootElement.element("structure");
241    
242                    if (structureElement != null) {
243                            JournalPortletDataHandlerImpl.importStructure(
244                                    portletDataContext, structureElement);
245                    }
246    
247                    Element templateElement = rootElement.element("template");
248    
249                    if (templateElement != null) {
250                            JournalPortletDataHandlerImpl.importTemplate(
251                                    portletDataContext, templateElement);
252                    }
253    
254                    Element articleElement = rootElement.element("article");
255    
256                    if (articleElement != null) {
257                            JournalPortletDataHandlerImpl.importArticle(
258                                    portletDataContext, articleElement);
259                    }
260    
261                    String articleId = portletPreferences.getValue("articleId", null);
262    
263                    if (Validator.isNotNull(articleId) && (articleElement != null)) {
264                            String importedArticleGroupId = articleElement.attributeValue(
265                                    "imported-article-group-id");
266    
267                            if (Validator.isNull(importedArticleGroupId)) {
268                                    importedArticleGroupId = String.valueOf(
269                                            portletDataContext.getScopeGroupId());
270                            }
271    
272                            portletPreferences.setValue("groupId", importedArticleGroupId);
273    
274                            Map<String, String> articleIds =
275                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
276                                            JournalArticle.class + ".articleId");
277    
278                            articleId = MapUtil.getString(articleIds, articleId, articleId);
279    
280                            portletPreferences.setValue("articleId", articleId);
281    
282                            Layout layout = LayoutLocalServiceUtil.getLayout(
283                                    portletDataContext.getPlid());
284    
285                            JournalContentSearchLocalServiceUtil.updateContentSearch(
286                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
287                                    layout.getLayoutId(), portletId, articleId, true);
288                    }
289                    else {
290                            portletPreferences.setValue("groupId", StringPool.BLANK);
291                            portletPreferences.setValue("articleId", StringPool.BLANK);
292                    }
293    
294                    String templateId = portletPreferences.getValue("templateId", null);
295    
296                    if (Validator.isNotNull(templateId)) {
297                            Map<String, String> templateIds =
298                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
299                                            JournalTemplate.class + ".templateId");
300    
301                            templateId = MapUtil.getString(templateIds, templateId, templateId);
302    
303                            portletPreferences.setValue("templateId", templateId);
304                    }
305                    else {
306                            portletPreferences.setValue("templateId", StringPool.BLANK);
307                    }
308    
309                    portletDataContext.setScopeGroupId(previousScopeGroupId);
310    
311                    return portletPreferences;
312            }
313    
314            private static final boolean _ALWAYS_EXPORTABLE = true;
315    
316            private static final boolean _ALWAYS_STAGED = true;
317    
318            private static final String _NAMESPACE = "journal";
319    
320            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
321    
322            private static PortletDataHandlerBoolean _comments =
323                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
324    
325            private static PortletDataHandlerBoolean _embeddedAssets =
326                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
327    
328            private static PortletDataHandlerBoolean _images =
329                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
330    
331            private static Log _log = LogFactoryUtil.getLog(
332                    JournalContentPortletDataHandlerImpl.class);
333    
334            private static PortletDataHandlerBoolean _ratings =
335                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
336    
337            private static PortletDataHandlerBoolean _selectedArticles =
338                    new PortletDataHandlerBoolean(
339                            _NAMESPACE, "selected-web-content", true, true);
340    
341            private static PortletDataHandlerBoolean _tags =
342                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
343    
344    }