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