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