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.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                    Element igFoldersElement = rootElement.addElement("ig-folders");
188                    Element igImagesElement = rootElement.addElement("ig-images");
189    
190                    for (JournalArticle article : articles) {
191                            String path = JournalPortletDataHandlerImpl.getArticlePath(
192                                    portletDataContext, article);
193    
194                            Element articleElement = null;
195    
196                            if (article == footerArticle) {
197                                    articleElement = rootElement.addElement("footer-article");
198                            }
199                            else {
200                                    articleElement = rootElement.addElement("header-article");
201                            }
202    
203                            articleElement.addAttribute("path", path);
204    
205                            JournalPortletDataHandlerImpl.exportArticle(
206                                    portletDataContext, rootElement, rootElement, rootElement,
207                                    dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
208                                    dlFileRanksElement, igFoldersElement, igImagesElement, article,
209                                    false);
210                    }
211    
212                    return document.formattedString();
213            }
214    
215            @Override
216            protected PortletPreferences doImportData(
217                            PortletDataContext portletDataContext, String portletId,
218                            PortletPreferences portletPreferences, String data)
219                    throws Exception {
220    
221                    if (Validator.isNull(data)) {
222                            return null;
223                    }
224    
225                    Document document = SAXReaderUtil.read(data);
226    
227                    Element rootElement = document.getRootElement();
228    
229                    JournalPortletDataHandlerImpl.importReferencedData(
230                            portletDataContext, rootElement);
231    
232                    List<Element> structureElements = rootElement.elements("structure");
233    
234                    for (Element structureElement : structureElements) {
235                            JournalPortletDataHandlerImpl.importStructure(
236                                    portletDataContext, structureElement);
237                    }
238    
239                    List<Element> templateElements = rootElement.elements("template");
240    
241                    for (Element templateElement : templateElements) {
242                            JournalPortletDataHandlerImpl.importTemplate(
243                                    portletDataContext, templateElement);
244                    }
245    
246                    Map<String, String> articleIds =
247                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
248                                    JournalArticle.class);
249    
250                    Layout layout = LayoutLocalServiceUtil.getLayout(
251                            portletDataContext.getPlid());
252    
253                    Element footerArticleElement = rootElement.element("footer-article");
254    
255                    if (footerArticleElement != null) {
256                            JournalPortletDataHandlerImpl.importArticle(
257                                    portletDataContext, footerArticleElement);
258                    }
259    
260                    String[] footerArticleValues = portletPreferences.getValues(
261                            "footerArticleValues", new String[] {"0", ""});
262    
263                    String footerArticleId = footerArticleValues[1];
264    
265                    if (Validator.isNotNull(footerArticleId)) {
266                            footerArticleId = MapUtil.getString(
267                                    articleIds, footerArticleId, footerArticleId);
268    
269                            portletPreferences.setValues(
270                                    "footerArticleValues",
271                                    new String[] {
272                                            String.valueOf(portletDataContext.getScopeGroupId()),
273                                            footerArticleId
274                                    });
275    
276                            JournalContentSearchLocalServiceUtil.updateContentSearch(
277                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
278                                    layout.getLayoutId(), portletId, footerArticleId, true);
279                    }
280    
281                    Element headerArticleElement = rootElement.element("header-article");
282    
283                    if (headerArticleElement != null) {
284                            JournalPortletDataHandlerImpl.importArticle(
285                                    portletDataContext, headerArticleElement);
286                    }
287    
288                    String[] headerArticleValues = portletPreferences.getValues(
289                            "headerArticleValues", new String[] {"0", ""});
290    
291                    String headerArticleId = headerArticleValues[1];
292    
293                    if (Validator.isNotNull(headerArticleId)) {
294                            headerArticleId = MapUtil.getString(
295                                    articleIds, headerArticleId, headerArticleId);
296    
297                            portletPreferences.setValues(
298                                    "headerArticleValues",
299                                    new String[] {
300                                            String.valueOf(portletDataContext.getScopeGroupId()),
301                                            headerArticleId
302                                    });
303    
304                            JournalContentSearchLocalServiceUtil.updateContentSearch(
305                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
306                                    layout.getLayoutId(), portletId, headerArticleId, true);
307                    }
308    
309                    return portletPreferences;
310            }
311    
312            private static final boolean _ALWAYS_EXPORTABLE = false;
313    
314            private static final String _NAMESPACE = "rss";
315    
316            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
317    
318            private static Log _log = LogFactoryUtil.getLog(
319                    RSSPortletDataHandlerImpl.class);
320    
321            private static PortletDataHandlerBoolean _comments =
322                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
323    
324            private static PortletDataHandlerBoolean _embeddedAssets =
325                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
326    
327            private static PortletDataHandlerBoolean _images =
328                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
329    
330            private static PortletDataHandlerBoolean _ratings =
331                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
332    
333            private static PortletDataHandlerBoolean _selectedArticles =
334                    new PortletDataHandlerBoolean(
335                            _NAMESPACE, "selected-web-content", true, true);
336    
337            private static PortletDataHandlerBoolean _tags =
338                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
339    
340    }