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