001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
089                            "showFeedPublishedDate", StringPool.BLANK);
090                    portletPreferences.setValue("showFeedDescription", StringPool.BLANK);
091                    portletPreferences.setValue("showFeedImage", StringPool.BLANK);
092                    portletPreferences.setValue("feedImageAlignment", StringPool.BLANK);
093                    portletPreferences.setValue("showFeedItemAuthor", StringPool.BLANK);
094    
095                    return portletPreferences;
096            }
097    
098            @Override
099            protected String doExportData(
100                            PortletDataContext portletDataContext, String portletId,
101                            PortletPreferences portletPreferences)
102                    throws Exception {
103    
104                    String[] footerArticleValues = portletPreferences.getValues(
105                            "footerArticleValues", new String[] {"0", ""});
106                    String[] headerArticleValues = portletPreferences.getValues(
107                            "headerArticleValues", new String[] {"0", ""});
108    
109                    String footerArticleId = footerArticleValues[1];
110                    String headerArticleId = headerArticleValues[1];
111    
112                    if (Validator.isNull(footerArticleId) &&
113                            Validator.isNull(headerArticleId)) {
114    
115                            if (_log.isWarnEnabled()) {
116                                    _log.warn(
117                                            "No article ids found in preferences of portlet " +
118                                                    portletId);
119                            }
120    
121                            return StringPool.BLANK;
122                    }
123    
124                    long footerArticleGroupId = GetterUtil.getLong(footerArticleValues[0]);
125                    long headerArticleGroupId = GetterUtil.getLong(headerArticleValues[0]);
126    
127                    if ((footerArticleGroupId <= 0) && (headerArticleGroupId <= 0)) {
128                            if (_log.isWarnEnabled()) {
129                                    _log.warn(
130                                            "No group ids found in preferences of portlet " +
131                                                    portletId);
132                            }
133    
134                            return StringPool.BLANK;
135                    }
136    
137                    List<JournalArticle> articles = new ArrayList<JournalArticle>(2);
138    
139                    JournalArticle footerArticle = null;
140    
141                    try {
142                            footerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
143                                    footerArticleGroupId, footerArticleId,
144                                    WorkflowConstants.STATUS_APPROVED);
145    
146                            articles.add(footerArticle);
147                    }
148                    catch (NoSuchArticleException nsae) {
149                            if (_log.isWarnEnabled()) {
150                                    _log.warn(
151                                            "No approved article found with group id " +
152                                                    footerArticleGroupId + " and article id " +
153                                                            footerArticleId);
154                            }
155                    }
156    
157                    JournalArticle headerArticle = null;
158    
159                    try {
160                            headerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
161                                    headerArticleGroupId, headerArticleId,
162                                    WorkflowConstants.STATUS_APPROVED);
163    
164                            articles.add(headerArticle);
165                    }
166                    catch (NoSuchArticleException nsae) {
167                            if (_log.isWarnEnabled()) {
168                                    _log.warn(
169                                            "No approved article found with group id " +
170                                                    headerArticleGroupId + " and article id " +
171                                                            headerArticleId);
172                            }
173                    }
174    
175                    if ((footerArticle == null) && (headerArticle == null)) {
176                            return StringPool.BLANK;
177                    }
178    
179                    Document document = SAXReaderUtil.createDocument();
180    
181                    Element rootElement = document.addElement("journal-content-data");
182    
183                    Element dlFileEntryTypesElement = rootElement.addElement(
184                            "dl-file-entry-types");
185                    Element dlFoldersElement = rootElement.addElement("dl-folders");
186                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
187                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
188                    Element igFoldersElement = rootElement.addElement("ig-folders");
189                    Element igImagesElement = rootElement.addElement("ig-images");
190    
191                    for (JournalArticle article : articles) {
192                            String path = JournalPortletDataHandlerImpl.getArticlePath(
193                                    portletDataContext, article);
194    
195                            Element articleElement = null;
196    
197                            if (article == footerArticle) {
198                                    articleElement = rootElement.addElement("footer-article");
199                            }
200                            else {
201                                    articleElement = rootElement.addElement("header-article");
202                            }
203    
204                            articleElement.addAttribute("path", path);
205    
206                            JournalPortletDataHandlerImpl.exportArticle(
207                                    portletDataContext, rootElement, rootElement, rootElement,
208                                    dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
209                                    dlFileRanksElement, igFoldersElement, igImagesElement, article,
210                                    false);
211                    }
212    
213                    return document.formattedString();
214            }
215    
216            @Override
217            protected PortletPreferences doImportData(
218                            PortletDataContext portletDataContext, String portletId,
219                            PortletPreferences portletPreferences, String data)
220                    throws Exception {
221    
222                    if (Validator.isNull(data)) {
223                            return null;
224                    }
225    
226                    Document document = SAXReaderUtil.read(data);
227    
228                    Element rootElement = document.getRootElement();
229    
230                    JournalPortletDataHandlerImpl.importReferencedData(
231                            portletDataContext, rootElement);
232    
233                    List<Element> structureElements = rootElement.elements("structure");
234    
235                    for (Element structureElement : structureElements) {
236                            JournalPortletDataHandlerImpl.importStructure(
237                                    portletDataContext, structureElement);
238                    }
239    
240                    List<Element> templateElements = rootElement.elements("template");
241    
242                    for (Element templateElement : templateElements) {
243                            JournalPortletDataHandlerImpl.importTemplate(
244                                    portletDataContext, templateElement);
245                    }
246    
247                    Map<String, String> articleIds =
248                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
249                                    JournalArticle.class);
250    
251                    Layout layout = LayoutLocalServiceUtil.getLayout(
252                            portletDataContext.getPlid());
253    
254                    Element footerArticleElement = rootElement.element("footer-article");
255    
256                    if (footerArticleElement != null) {
257                            JournalPortletDataHandlerImpl.importArticle(
258                                    portletDataContext, footerArticleElement);
259                    }
260    
261                    String[] footerArticleValues = portletPreferences.getValues(
262                            "footerArticleValues", new String[] {"0", ""});
263    
264                    String footerArticleId = footerArticleValues[1];
265    
266                    if (Validator.isNotNull(footerArticleId)) {
267                            footerArticleId = MapUtil.getString(
268                                    articleIds, footerArticleId, footerArticleId);
269    
270                            portletPreferences.setValues(
271                                    "footerArticleValues",
272                                    new String[] {
273                                            String.valueOf(portletDataContext.getScopeGroupId()),
274                                            footerArticleId
275                                    });
276    
277                            JournalContentSearchLocalServiceUtil.updateContentSearch(
278                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
279                                    layout.getLayoutId(), portletId, footerArticleId, true);
280                    }
281    
282                    Element headerArticleElement = rootElement.element("header-article");
283    
284                    if (headerArticleElement != null) {
285                            JournalPortletDataHandlerImpl.importArticle(
286                                    portletDataContext, headerArticleElement);
287                    }
288    
289                    String[] headerArticleValues = portletPreferences.getValues(
290                            "headerArticleValues", new String[] {"0", ""});
291    
292                    String headerArticleId = headerArticleValues[1];
293    
294                    if (Validator.isNotNull(headerArticleId)) {
295                            headerArticleId = MapUtil.getString(
296                                    articleIds, headerArticleId, headerArticleId);
297    
298                            portletPreferences.setValues(
299                                    "headerArticleValues",
300                                    new String[] {
301                                            String.valueOf(portletDataContext.getScopeGroupId()),
302                                            headerArticleId
303                                    });
304    
305                            JournalContentSearchLocalServiceUtil.updateContentSearch(
306                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
307                                    layout.getLayoutId(), portletId, headerArticleId, true);
308                    }
309    
310                    return portletPreferences;
311            }
312    
313            private static final boolean _ALWAYS_EXPORTABLE = false;
314    
315            private static final String _NAMESPACE = "rss";
316    
317            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
318    
319            private static Log _log = LogFactoryUtil.getLog(
320                    RSSPortletDataHandlerImpl.class);
321    
322            private static PortletDataHandlerBoolean _comments =
323                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
324    
325            private static PortletDataHandlerBoolean _embeddedAssets =
326                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
327    
328            private static PortletDataHandlerBoolean _images =
329                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
330    
331            private static PortletDataHandlerBoolean _ratings =
332                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
333    
334            private static PortletDataHandlerBoolean _selectedArticles =
335                    new PortletDataHandlerBoolean(
336                            _NAMESPACE, "selected-web-content", true, true);
337    
338            private static PortletDataHandlerBoolean _tags =
339                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
340    
341    }