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