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