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.rss.lar;
016    
017    import com.liferay.portal.kernel.lar.PortletDataContext;
018    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.kernel.xml.Document;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
033    import com.liferay.portlet.journal.NoSuchArticleException;
034    import com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl;
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    
039    import java.util.ArrayList;
040    import java.util.List;
041    import java.util.Map;
042    
043    import javax.portlet.PortletPreferences;
044    
045    /**
046     * @author Raymond Augé
047     */
048    public class RSSPortletDataHandlerImpl extends JournalPortletDataHandlerImpl {
049    
050            @Override
051            public PortletDataHandlerControl[] getExportControls() {
052                    return new PortletDataHandlerControl[] {
053                            _selectedArticles, _embeddedAssets
054                    };
055            }
056    
057            @Override
058            public PortletDataHandlerControl[] getExportMetadataControls() {
059                    return new PortletDataHandlerControl[] {
060                            new PortletDataHandlerBoolean(
061                                    _NAMESPACE, "web-content", true,
062                                    JournalPortletDataHandlerImpl.getMetadataControls()
063                            ),
064                            new PortletDataHandlerBoolean(
065                                    _NAMESPACE, "folders-and-documents", true,
066                                    DLPortletDataHandlerImpl.getMetadataControls()
067                            )
068                    };
069            }
070    
071            @Override
072            public PortletDataHandlerControl[] getImportControls() {
073                    return new PortletDataHandlerControl[] {
074                            _selectedArticles
075                    };
076            }
077    
078            @Override
079            public PortletDataHandlerControl[] getImportMetadataControls() {
080                    return new PortletDataHandlerControl[] {
081                            new PortletDataHandlerBoolean(
082                                    _NAMESPACE, "web-content", true,
083                                    JournalPortletDataHandlerImpl.getMetadataControls()
084                            ),
085                            new PortletDataHandlerBoolean(
086                                    _NAMESPACE, "folders-and-documents", true,
087                                    DLPortletDataHandlerImpl.getMetadataControls()
088                            )
089                    };
090            }
091    
092            @Override
093            public boolean isAlwaysExportable() {
094                    return _ALWAYS_EXPORTABLE;
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("footerArticleValues", StringPool.BLANK);
109                    portletPreferences.setValue("headerArticleValues", StringPool.BLANK);
110                    portletPreferences.setValue("urls", StringPool.BLANK);
111                    portletPreferences.setValue("titles", StringPool.BLANK);
112                    portletPreferences.setValue("itemsPerChannel", StringPool.BLANK);
113                    portletPreferences.setValue(
114                            "expandedItemsPerChannel", StringPool.BLANK);
115                    portletPreferences.setValue("showFeedTitle", StringPool.BLANK);
116                    portletPreferences.setValue("showFeedPublishedDate", StringPool.BLANK);
117                    portletPreferences.setValue("showFeedDescription", StringPool.BLANK);
118                    portletPreferences.setValue("showFeedImage", StringPool.BLANK);
119                    portletPreferences.setValue("feedImageAlignment", StringPool.BLANK);
120                    portletPreferences.setValue("showFeedItemAuthor", StringPool.BLANK);
121    
122                    return portletPreferences;
123            }
124    
125            @Override
126            protected String doExportData(
127                            PortletDataContext portletDataContext, String portletId,
128                            PortletPreferences portletPreferences)
129                    throws Exception {
130    
131                    String[] footerArticleValues = portletPreferences.getValues(
132                            "footerArticleValues", new String[] {"0", ""});
133                    String[] headerArticleValues = portletPreferences.getValues(
134                            "headerArticleValues", new String[] {"0", ""});
135    
136                    String footerArticleId = footerArticleValues[1];
137                    String headerArticleId = headerArticleValues[1];
138    
139                    if (Validator.isNull(footerArticleId) &&
140                            Validator.isNull(headerArticleId)) {
141    
142                            if (_log.isWarnEnabled()) {
143                                    _log.warn(
144                                            "No article ids found in preferences of portlet " +
145                                                    portletId);
146                            }
147    
148                            return StringPool.BLANK;
149                    }
150    
151                    long footerArticleGroupId = GetterUtil.getLong(footerArticleValues[0]);
152                    long headerArticleGroupId = GetterUtil.getLong(headerArticleValues[0]);
153    
154                    if ((footerArticleGroupId <= 0) && (headerArticleGroupId <= 0)) {
155                            if (_log.isWarnEnabled()) {
156                                    _log.warn(
157                                            "No group ids found in preferences of portlet " +
158                                                    portletId);
159                            }
160    
161                            return StringPool.BLANK;
162                    }
163    
164                    List<JournalArticle> articles = new ArrayList<JournalArticle>(2);
165    
166                    JournalArticle footerArticle = null;
167    
168                    try {
169                            footerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
170                                    footerArticleGroupId, footerArticleId,
171                                    WorkflowConstants.STATUS_APPROVED);
172    
173                            articles.add(footerArticle);
174                    }
175                    catch (NoSuchArticleException nsae) {
176                            if (_log.isWarnEnabled()) {
177                                    _log.warn(
178                                            "No approved article found with group id " +
179                                                    footerArticleGroupId + " and article id " +
180                                                            footerArticleId);
181                            }
182                    }
183    
184                    JournalArticle headerArticle = null;
185    
186                    try {
187                            headerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
188                                    headerArticleGroupId, headerArticleId,
189                                    WorkflowConstants.STATUS_APPROVED);
190    
191                            articles.add(headerArticle);
192                    }
193                    catch (NoSuchArticleException nsae) {
194                            if (_log.isWarnEnabled()) {
195                                    _log.warn(
196                                            "No approved article found with group id " +
197                                                    headerArticleGroupId + " and article id " +
198                                                            headerArticleId);
199                            }
200                    }
201    
202                    if ((footerArticle == null) && (headerArticle == null)) {
203                            return StringPool.BLANK;
204                    }
205    
206                    Document document = SAXReaderUtil.createDocument();
207    
208                    Element rootElement = document.addElement("journal-content-data");
209    
210                    Element dlFileEntryTypesElement = rootElement.addElement(
211                            "dl-file-entry-types");
212                    Element dlFoldersElement = rootElement.addElement("dl-folders");
213                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
214                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
215    
216                    for (JournalArticle article : articles) {
217                            String path = JournalPortletDataHandlerImpl.getArticlePath(
218                                    portletDataContext, article);
219    
220                            Element articleElement = null;
221    
222                            if (article == footerArticle) {
223                                    articleElement = rootElement.addElement("footer-article");
224                            }
225                            else {
226                                    articleElement = rootElement.addElement("header-article");
227                            }
228    
229                            articleElement.addAttribute("path", path);
230    
231                            JournalPortletDataHandlerImpl.exportArticle(
232                                    portletDataContext, rootElement, rootElement, rootElement,
233                                    dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
234                                    dlFileRanksElement, article, false);
235                    }
236    
237                    return document.formattedString();
238            }
239    
240            @Override
241            protected PortletPreferences doImportData(
242                            PortletDataContext portletDataContext, String portletId,
243                            PortletPreferences portletPreferences, String data)
244                    throws Exception {
245    
246                    if (Validator.isNull(data)) {
247                            return null;
248                    }
249    
250                    Document document = SAXReaderUtil.read(data);
251    
252                    Element rootElement = document.getRootElement();
253    
254                    JournalPortletDataHandlerImpl.importReferencedData(
255                            portletDataContext, rootElement);
256    
257                    List<Element> structureElements = rootElement.elements("structure");
258    
259                    for (Element structureElement : structureElements) {
260                            JournalPortletDataHandlerImpl.importStructure(
261                                    portletDataContext, structureElement);
262                    }
263    
264                    List<Element> templateElements = rootElement.elements("template");
265    
266                    for (Element templateElement : templateElements) {
267                            JournalPortletDataHandlerImpl.importTemplate(
268                                    portletDataContext, templateElement);
269                    }
270    
271                    Map<String, String> articleIds =
272                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
273                                    JournalArticle.class);
274    
275                    Layout layout = LayoutLocalServiceUtil.getLayout(
276                            portletDataContext.getPlid());
277    
278                    Element footerArticleElement = rootElement.element("footer-article");
279    
280                    if (footerArticleElement != null) {
281                            JournalPortletDataHandlerImpl.importArticle(
282                                    portletDataContext, footerArticleElement);
283                    }
284    
285                    String[] footerArticleValues = portletPreferences.getValues(
286                            "footerArticleValues", new String[] {"0", ""});
287    
288                    String footerArticleId = footerArticleValues[1];
289    
290                    if (Validator.isNotNull(footerArticleId)) {
291                            footerArticleId = MapUtil.getString(
292                                    articleIds, footerArticleId, footerArticleId);
293    
294                            portletPreferences.setValues(
295                                    "footerArticleValues",
296                                    new String[] {
297                                            String.valueOf(portletDataContext.getScopeGroupId()),
298                                            footerArticleId
299                                    });
300    
301                            JournalContentSearchLocalServiceUtil.updateContentSearch(
302                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
303                                    layout.getLayoutId(), portletId, footerArticleId, true);
304                    }
305    
306                    Element headerArticleElement = rootElement.element("header-article");
307    
308                    if (headerArticleElement != null) {
309                            JournalPortletDataHandlerImpl.importArticle(
310                                    portletDataContext, headerArticleElement);
311                    }
312    
313                    String[] headerArticleValues = portletPreferences.getValues(
314                            "headerArticleValues", new String[] {"0", ""});
315    
316                    String headerArticleId = headerArticleValues[1];
317    
318                    if (Validator.isNotNull(headerArticleId)) {
319                            headerArticleId = MapUtil.getString(
320                                    articleIds, headerArticleId, headerArticleId);
321    
322                            portletPreferences.setValues(
323                                    "headerArticleValues",
324                                    new String[] {
325                                            String.valueOf(portletDataContext.getScopeGroupId()),
326                                            headerArticleId
327                                    });
328    
329                            JournalContentSearchLocalServiceUtil.updateContentSearch(
330                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
331                                    layout.getLayoutId(), portletId, headerArticleId, true);
332                    }
333    
334                    return portletPreferences;
335            }
336    
337            private static final boolean _ALWAYS_EXPORTABLE = false;
338    
339            private static final String _NAMESPACE = "rss";
340    
341            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
342    
343            private static Log _log = LogFactoryUtil.getLog(
344                    RSSPortletDataHandlerImpl.class);
345    
346            private static PortletDataHandlerBoolean _embeddedAssets =
347                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
348    
349            private static PortletDataHandlerBoolean _selectedArticles =
350                    new PortletDataHandlerBoolean(
351                            _NAMESPACE, "selected-web-content", true, true);
352    
353    }