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