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