001    /**
002     * Copyright (c) 2000-2010 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.Group;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.service.LayoutLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
035    import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
036    import com.liferay.portlet.journal.NoSuchArticleException;
037    import com.liferay.portlet.journal.NoSuchStructureException;
038    import com.liferay.portlet.journal.NoSuchTemplateException;
039    import com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl;
040    import com.liferay.portlet.journal.model.JournalArticle;
041    import com.liferay.portlet.journal.model.JournalStructure;
042    import com.liferay.portlet.journal.model.JournalTemplate;
043    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
044    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
045    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
046    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
047    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.List;
051    import java.util.Map;
052    
053    import javax.portlet.PortletPreferences;
054    
055    /**
056     * @author Raymond Augé
057     */
058    public class RSSPortletDataHandlerImpl extends JournalPortletDataHandlerImpl {
059    
060            public PortletDataHandlerControl[] getExportControls() {
061                    return new PortletDataHandlerControl[] {
062                            _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
063                            _tags
064                    };
065            }
066    
067            public PortletDataHandlerControl[] getImportControls() {
068                    return new PortletDataHandlerControl[] {
069                            _selectedArticles, _images, _comments, _ratings, _tags
070                    };
071            }
072    
073            public boolean isPublishToLiveByDefault() {
074                    return     _PUBLISH_TO_LIVE_BY_DEFAULT;
075            }
076    
077            protected PortletPreferences doDeleteData(
078                            PortletDataContext context, String portletId,
079                            PortletPreferences preferences)
080                    throws Exception {
081    
082                    preferences.setValue("footer-article-values", StringPool.BLANK);
083                    preferences.setValue("header-article-values", StringPool.BLANK);
084                    preferences.setValue("urls", StringPool.BLANK);
085                    preferences.setValue("titles", StringPool.BLANK);
086                    preferences.setValue("items-per-channel", StringPool.BLANK);
087                    preferences.setValue("expanded-items-per-channel", StringPool.BLANK);
088                    preferences.setValue("show-feed-title", StringPool.BLANK);
089                    preferences.setValue("show-feed-published-date", StringPool.BLANK);
090                    preferences.setValue("show-feed-description", StringPool.BLANK);
091                    preferences.setValue("show-feed-image", StringPool.BLANK);
092                    preferences.setValue("feed-image-alignment", StringPool.BLANK);
093                    preferences.setValue("show-feed-item-author", StringPool.BLANK);
094    
095                    return preferences;
096            }
097    
098            protected String doExportData(
099                            PortletDataContext context, String portletId,
100                            PortletPreferences preferences)
101                    throws Exception {
102    
103                    String[] footerArticleValues = preferences.getValues(
104                            "footer-article-values", new String[] {"0", ""});
105                    String[] headerArticleValues = preferences.getValues(
106                            "header-article-values", 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 dlFoldersElement = rootElement.addElement("dl-folders");
183                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
184                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
185                    Element igFoldersElement = rootElement.addElement("ig-folders");
186                    Element igImagesElement = rootElement.addElement("ig-images");
187    
188                    for (JournalArticle article : articles) {
189                            String path = JournalPortletDataHandlerImpl.getArticlePath(
190                                    context, article);
191    
192                            Element articleElement = null;
193    
194                            if (article == footerArticle) {
195                                    articleElement = rootElement.addElement("footer-article");
196                            }
197                            else {
198                                    articleElement = rootElement.addElement("header-article");
199                            }
200    
201                            articleElement.addAttribute("path", path);
202    
203                            JournalPortletDataHandlerImpl.exportArticle(
204                                    context, rootElement, rootElement, rootElement,
205                                    dlFoldersElement, dlFilesElement, dlFileRanksElement,
206                                    igFoldersElement, igImagesElement, article, false);
207    
208                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
209                                    article.getCompanyId());
210    
211                            String structureId = article.getStructureId();
212    
213                            if (Validator.isNotNull(structureId)) {
214                                    JournalStructure structure = null;
215    
216                                    try {
217                                            structure = JournalStructureLocalServiceUtil.getStructure(
218                                                    article.getGroupId(), structureId);
219                                    }
220                                    catch (NoSuchStructureException nsse) {
221                                            structure = JournalStructureLocalServiceUtil.getStructure(
222                                                    companyGroup.getGroupId(), structureId);
223                                    }
224    
225                                    JournalPortletDataHandlerImpl.exportStructure(
226                                            context, rootElement, structure);
227                            }
228    
229                            String templateId = article.getTemplateId();
230    
231                            if (Validator.isNotNull(templateId)) {
232                                    JournalTemplate template = null;
233    
234                                    try {
235                                            template = JournalTemplateLocalServiceUtil.getTemplate(
236                                                    context.getScopeGroupId(), templateId);
237                                    }
238                                    catch (NoSuchTemplateException nste) {
239                                            template = JournalTemplateLocalServiceUtil.getTemplate(
240                                                    companyGroup.getGroupId(), templateId);
241                                    }
242    
243                                    JournalPortletDataHandlerImpl.exportTemplate(
244                                            context, rootElement, dlFoldersElement, dlFilesElement,
245                                            dlFileRanksElement, igFoldersElement, igImagesElement,
246                                            template, false);
247                            }
248                    }
249    
250                    return document.formattedString();
251            }
252    
253            protected PortletPreferences doImportData(
254                            PortletDataContext context, String portletId,
255                            PortletPreferences preferences, String data)
256                    throws Exception {
257    
258                    if (Validator.isNull(data)) {
259                            return null;
260                    }
261    
262                    Document document = SAXReaderUtil.read(data);
263    
264                    Element rootElement = document.getRootElement();
265    
266                    Element dlFoldersElement = rootElement.element("dl-folders");
267    
268                    List<Element> dlFolderElements = Collections.EMPTY_LIST;
269    
270                    if (dlFoldersElement != null) {
271                            dlFolderElements = dlFoldersElement.elements("folder");
272                    }
273    
274                    for (Element folderElement : dlFolderElements) {
275                            DLPortletDataHandlerImpl.importFolder(context, folderElement);
276                    }
277    
278                    Element dlFileEntriesElement = rootElement.element("dl-file-entries");
279    
280                    List<Element> dlFileEntryElements = Collections.EMPTY_LIST;
281    
282                    if (dlFileEntriesElement != null) {
283                            dlFileEntryElements = dlFileEntriesElement.elements("file-entry");
284                    }
285    
286                    for (Element fileEntryElement : dlFileEntryElements) {
287                            DLPortletDataHandlerImpl.importFileEntry(context, fileEntryElement);
288                    }
289    
290                    Element dlFileRanksElement = rootElement.element("dl-file-ranks");
291    
292                    List<Element> dlFileRankElements = Collections.EMPTY_LIST;
293    
294                    if (dlFileRanksElement != null) {
295                            dlFileRankElements = dlFileRanksElement.elements("file-rank");
296                    }
297    
298                    for (Element fileRankElement : dlFileRankElements) {
299                            DLPortletDataHandlerImpl.importFileRank(context, fileRankElement);
300                    }
301    
302                    Element igFoldersElement = rootElement.element("ig-folders");
303    
304                    List<Element> igFolderElements = Collections.EMPTY_LIST;
305    
306                    if (igFoldersElement != null) {
307                            igFolderElements = igFoldersElement.elements("folder");
308                    }
309    
310                    for (Element folderElement : igFolderElements) {
311                            IGPortletDataHandlerImpl.importFolder(context, folderElement);
312                    }
313    
314                    Element igImagesElement = rootElement.element("ig-images");
315    
316                    List<Element> igImageElements = Collections.EMPTY_LIST;
317    
318                    if (igImagesElement != null) {
319                            igImageElements = igImagesElement.elements("image");
320                    }
321    
322                    for (Element imageElement : igImageElements) {
323                            IGPortletDataHandlerImpl.importImage(context, imageElement);
324                    }
325    
326                    List<Element> structureElements = rootElement.elements("structure");
327    
328                    for (Element structureElement : structureElements) {
329                            JournalPortletDataHandlerImpl.importStructure(
330                                    context, structureElement);
331                    }
332    
333                    List<Element> templateElements = rootElement.elements("template");
334    
335                    for (Element templateElement : templateElements) {
336                            JournalPortletDataHandlerImpl.importTemplate(
337                                    context, templateElement);
338                    }
339    
340                    Map<String, String> articleIds =
341                            (Map<String, String>)context.getNewPrimaryKeysMap(
342                                    JournalArticle.class);
343    
344                    Layout layout = LayoutLocalServiceUtil.getLayout(
345                            context.getPlid());
346    
347                    Element footerArticleElement = rootElement.element("footer-article");
348    
349                    if (footerArticleElement != null) {
350                            JournalPortletDataHandlerImpl.importArticle(
351                                    context, footerArticleElement);
352                    }
353    
354                    String[] footerArticleValues = preferences.getValues(
355                            "footer-article-values", new String[] {"0", ""});
356    
357                    String footerArticleId = footerArticleValues[1];
358    
359                    if (Validator.isNotNull(footerArticleId)) {
360                            footerArticleId = MapUtil.getString(
361                                    articleIds, footerArticleId, footerArticleId);
362    
363                            preferences.setValues(
364                                    "footer-article-values",
365                                    new String[] {
366                                            String.valueOf(context.getScopeGroupId()), footerArticleId
367                                    });
368    
369                            JournalContentSearchLocalServiceUtil.updateContentSearch(
370                                    context.getScopeGroupId(), layout.isPrivateLayout(),
371                                    layout.getLayoutId(), portletId, footerArticleId, true);
372                    }
373    
374                    Element headerArticleElement = rootElement.element("header-article");
375    
376                    if (headerArticleElement != null) {
377                            JournalPortletDataHandlerImpl.importArticle(
378                                    context, headerArticleElement);
379                    }
380    
381                    String[] headerArticleValues = preferences.getValues(
382                            "header-article-values", new String[] {"0", ""});
383    
384                    String headerArticleId = headerArticleValues[1];
385    
386                    if (Validator.isNotNull(headerArticleId)) {
387                            headerArticleId = MapUtil.getString(
388                                    articleIds, headerArticleId, headerArticleId);
389    
390                            preferences.setValues(
391                                    "header-article-values",
392                                    new String[] {
393                                            String.valueOf(context.getScopeGroupId()), headerArticleId
394                                    });
395    
396                            JournalContentSearchLocalServiceUtil.updateContentSearch(
397                                    context.getScopeGroupId(), layout.isPrivateLayout(),
398                                    layout.getLayoutId(), portletId, headerArticleId, true);
399                    }
400    
401                    return preferences;
402            }
403    
404            private static final String _NAMESPACE = "rss";
405    
406            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
407    
408            private static Log _log = LogFactoryUtil.getLog(
409                    RSSPortletDataHandlerImpl.class);
410    
411            private static PortletDataHandlerBoolean _comments =
412                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
413    
414            private static PortletDataHandlerBoolean _embeddedAssets =
415                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
416    
417            private static PortletDataHandlerBoolean _images =
418                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
419    
420            private static PortletDataHandlerBoolean _ratings =
421                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
422    
423            private static PortletDataHandlerBoolean _selectedArticles =
424                    new PortletDataHandlerBoolean(
425                            _NAMESPACE, "selected-web-content", true, true);
426    
427            private static PortletDataHandlerBoolean _tags =
428                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
429    
430    }