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.DataLevel;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.PortletDataException;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.portal.util.PropsValues;
034    import com.liferay.portlet.journal.NoSuchArticleException;
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 RSSPortletDataHandler extends BasePortletDataHandler {
049    
050            public static final String NAMESPACE = "rss";
051    
052            public RSSPortletDataHandler() {
053                    setDataLevel(DataLevel.PORTLET_INSTANCE);
054                    setDataPortletPreferences("footerArticleValues", "headerArticleValues");
055                    setExportControls(new PortletDataHandlerControl[0]);
056                    setPublishToLiveByDefault(PropsValues.RSS_PUBLISH_TO_LIVE_BY_DEFAULT);
057            }
058    
059            @Override
060            protected PortletPreferences doDeleteData(
061                            PortletDataContext portletDataContext, String portletId,
062                            PortletPreferences portletPreferences)
063                    throws Exception {
064    
065                    if (portletPreferences == null) {
066                            return portletPreferences;
067                    }
068    
069                    portletPreferences.setValue(
070                            "expandedItemsPerChannel", StringPool.BLANK);
071                    portletPreferences.setValue("feedImageAlignment", StringPool.BLANK);
072                    portletPreferences.setValues(
073                            "footerArticleValues", new String[] {"0", ""});
074                    portletPreferences.setValues(
075                            "headerArticleValues", new String[] {"0", ""});
076                    portletPreferences.setValue("itemsPerChannel", StringPool.BLANK);
077                    portletPreferences.setValue("showFeedDescription", StringPool.BLANK);
078                    portletPreferences.setValue("showFeedImage", StringPool.BLANK);
079                    portletPreferences.setValue("showFeedItemAuthor", StringPool.BLANK);
080                    portletPreferences.setValue("showFeedPublishedDate", StringPool.BLANK);
081                    portletPreferences.setValue("showFeedTitle", StringPool.BLANK);
082                    portletPreferences.setValue("titles", StringPool.BLANK);
083                    portletPreferences.setValue("urls", StringPool.BLANK);
084    
085                    return portletPreferences;
086            }
087    
088            @Override
089            protected PortletPreferences doProcessExportPortletPreferences(
090                            PortletDataContext portletDataContext, String portletId,
091                            PortletPreferences portletPreferences)
092                    throws Exception {
093    
094                    String[] footerArticleValues = portletPreferences.getValues(
095                            "footerArticleValues", new String[] {"0", ""});
096                    String[] headerArticleValues = portletPreferences.getValues(
097                            "headerArticleValues", new String[] {"0", ""});
098    
099                    String footerArticleId = footerArticleValues[1];
100                    String headerArticleId = headerArticleValues[1];
101    
102                    if (Validator.isNull(footerArticleId) &&
103                            Validator.isNull(headerArticleId)) {
104    
105                            if (_log.isWarnEnabled()) {
106                                    _log.warn(
107                                            "No article ids found in preferences of portlet " +
108                                                    portletId);
109                            }
110    
111                            return portletPreferences;
112                    }
113    
114                    long footerArticleGroupId = GetterUtil.getLong(footerArticleValues[0]);
115                    long headerArticleGroupId = GetterUtil.getLong(headerArticleValues[0]);
116    
117                    if ((footerArticleGroupId <= 0) && (headerArticleGroupId <= 0)) {
118                            if (_log.isWarnEnabled()) {
119                                    _log.warn(
120                                            "No group ids found in preferences of portlet " +
121                                                    portletId);
122                            }
123    
124                            return portletPreferences;
125                    }
126    
127                    List<JournalArticle> articles = new ArrayList<JournalArticle>(2);
128    
129                    JournalArticle footerArticle = null;
130    
131                    try {
132                            footerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
133                                    footerArticleGroupId, footerArticleId,
134                                    WorkflowConstants.STATUS_APPROVED);
135    
136                            articles.add(footerArticle);
137                    }
138                    catch (NoSuchArticleException nsae) {
139                            if (_log.isWarnEnabled()) {
140                                    _log.warn(
141                                            "No approved article found with group id " +
142                                                    footerArticleGroupId + " and article id " +
143                                                            footerArticleId);
144                            }
145                    }
146    
147                    JournalArticle headerArticle = null;
148    
149                    try {
150                            headerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
151                                    headerArticleGroupId, headerArticleId,
152                                    WorkflowConstants.STATUS_APPROVED);
153    
154                            articles.add(headerArticle);
155                    }
156                    catch (NoSuchArticleException nsae) {
157                            if (_log.isWarnEnabled()) {
158                                    _log.warn(
159                                            "No approved article found with group id " +
160                                                    headerArticleGroupId + " and article id " +
161                                                            headerArticleId);
162                            }
163                    }
164    
165                    if (articles.isEmpty()) {
166                            return portletPreferences;
167                    }
168    
169                    for (JournalArticle article : articles) {
170                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
171                                    portletDataContext, portletId, article);
172                    }
173    
174                    return portletPreferences;
175            }
176    
177            @Override
178            protected PortletPreferences doProcessImportPortletPreferences(
179                            PortletDataContext portletDataContext, String portletId,
180                            PortletPreferences portletPreferences)
181                    throws Exception {
182    
183                    importReferenceArticle(portletDataContext);
184    
185                    Layout layout = LayoutLocalServiceUtil.getLayout(
186                            portletDataContext.getPlid());
187    
188                    Map<String, String> articleIds =
189                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
190                                    JournalArticle.class + ".articleId");
191    
192                    String[] footerArticleValues = portletPreferences.getValues(
193                            "footerArticleValues", new String[] {"0", ""});
194    
195                    String footerArticleId = footerArticleValues[1];
196    
197                    footerArticleId = MapUtil.getString(
198                            articleIds, footerArticleId, footerArticleId);
199    
200                    if (Validator.isNotNull(footerArticleId)) {
201                            footerArticleId = MapUtil.getString(
202                                    articleIds, footerArticleId, footerArticleId);
203    
204                            portletPreferences.setValues(
205                                    "footerArticleValues",
206                                    new String[] {
207                                            String.valueOf(portletDataContext.getScopeGroupId()),
208                                            footerArticleId
209                                    });
210    
211                            JournalContentSearchLocalServiceUtil.updateContentSearch(
212                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
213                                    layout.getLayoutId(), portletId, footerArticleId, true);
214                    }
215    
216                    String[] headerArticleValues = portletPreferences.getValues(
217                            "headerArticleValues", new String[] {"0", ""});
218    
219                    String headerArticleId = headerArticleValues[1];
220    
221                    headerArticleId = MapUtil.getString(
222                            articleIds, headerArticleId, headerArticleId);
223    
224                    if (Validator.isNotNull(headerArticleId)) {
225                            portletPreferences.setValues(
226                                    "headerArticleValues",
227                                    new String[] {
228                                            String.valueOf(portletDataContext.getScopeGroupId()),
229                                            headerArticleId
230                                    });
231    
232                            JournalContentSearchLocalServiceUtil.updateContentSearch(
233                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
234                                    layout.getLayoutId(), portletId, headerArticleId, true);
235                    }
236    
237                    return portletPreferences;
238            }
239    
240            protected void importReferenceArticle(PortletDataContext portletDataContext)
241                    throws PortletDataException {
242    
243                    Element articlesElement = portletDataContext.getImportDataGroupElement(
244                            JournalArticle.class);
245    
246                    if (articlesElement == null) {
247                            return;
248                    }
249    
250                    List<Element> articleElements = articlesElement.elements();
251    
252                    for (Element articleElement : articleElements) {
253                            StagedModelDataHandlerUtil.importReferenceStagedModel(
254                                    portletDataContext, articleElement);
255                    }
256            }
257    
258            private static Log _log = LogFactoryUtil.getLog(
259                    RSSPortletDataHandler.class);
260    
261    }