001
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.ExportImportPathUtil;
020 import com.liferay.portal.kernel.lar.PortletDataContext;
021 import com.liferay.portal.kernel.lar.PortletDataException;
022 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
023 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
024 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
025 import com.liferay.portal.kernel.log.Log;
026 import com.liferay.portal.kernel.log.LogFactoryUtil;
027 import com.liferay.portal.kernel.util.ArrayUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.MapUtil;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.workflow.WorkflowConstants;
033 import com.liferay.portal.kernel.xml.Element;
034 import com.liferay.portal.model.Layout;
035 import com.liferay.portal.service.LayoutLocalServiceUtil;
036 import com.liferay.portal.util.PropsValues;
037 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandler;
038 import com.liferay.portlet.journal.NoSuchArticleException;
039 import com.liferay.portlet.journal.lar.JournalPortletDataHandler;
040 import com.liferay.portlet.journal.model.JournalArticle;
041 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
042 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
043
044 import java.util.ArrayList;
045 import java.util.List;
046 import java.util.Map;
047
048 import javax.portlet.PortletPreferences;
049
050
053 public class RSSPortletDataHandler extends BasePortletDataHandler {
054
055 public static final String NAMESPACE = "rss";
056
057 public RSSPortletDataHandler() {
058 setDataLevel(DataLevel.PORTLET_INSTANCE);
059 setDataPortletPreferences("footerArticleValues", "headerArticleValues");
060 setExportControls(
061 new PortletDataHandlerBoolean(
062 NAMESPACE, "selected-web-content", true, true,
063 new PortletDataHandlerControl[] {
064 new PortletDataHandlerBoolean(
065 NAMESPACE, "referenced-content")
066 },
067 JournalArticle.class.getName()));
068
069 JournalPortletDataHandler journalPortletDataHandler =
070 new JournalPortletDataHandler();
071 DLPortletDataHandler dlPortletDataHandler = new DLPortletDataHandler();
072
073 PortletDataHandlerControl[] exportMetadataControls = ArrayUtil.append(
074 journalPortletDataHandler.getExportMetadataControls(),
075 dlPortletDataHandler.getExportMetadataControls());
076
077 for (PortletDataHandlerControl portletDataHandlerControl :
078 exportMetadataControls) {
079
080 portletDataHandlerControl.setNamespace(NAMESPACE);
081 }
082
083 setExportMetadataControls(exportMetadataControls);
084
085 setImportControls(getExportControls()[0]);
086 setPublishToLiveByDefault(PropsValues.RSS_PUBLISH_TO_LIVE_BY_DEFAULT);
087 }
088
089 @Override
090 protected PortletPreferences doDeleteData(
091 PortletDataContext portletDataContext, String portletId,
092 PortletPreferences portletPreferences)
093 throws Exception {
094
095 if (portletPreferences == null) {
096 return portletPreferences;
097 }
098
099 portletPreferences.setValue(
100 "expandedItemsPerChannel", StringPool.BLANK);
101 portletPreferences.setValue("feedImageAlignment", StringPool.BLANK);
102 portletPreferences.setValues(
103 "footerArticleValues", new String[] {"0", ""});
104 portletPreferences.setValues(
105 "headerArticleValues", new String[] {"0", ""});
106 portletPreferences.setValue("itemsPerChannel", StringPool.BLANK);
107 portletPreferences.setValue("showFeedDescription", StringPool.BLANK);
108 portletPreferences.setValue("showFeedImage", StringPool.BLANK);
109 portletPreferences.setValue("showFeedItemAuthor", StringPool.BLANK);
110 portletPreferences.setValue("showFeedPublishedDate", StringPool.BLANK);
111 portletPreferences.setValue("showFeedTitle", StringPool.BLANK);
112 portletPreferences.setValue("titles", StringPool.BLANK);
113 portletPreferences.setValue("urls", StringPool.BLANK);
114
115 return portletPreferences;
116 }
117
118 @Override
119 protected String doExportData(
120 PortletDataContext portletDataContext, String portletId,
121 PortletPreferences portletPreferences)
122 throws Exception {
123
124 String[] footerArticleValues = portletPreferences.getValues(
125 "footerArticleValues", new String[] {"0", ""});
126 String[] headerArticleValues = portletPreferences.getValues(
127 "headerArticleValues", new String[] {"0", ""});
128
129 String footerArticleId = footerArticleValues[1];
130 String headerArticleId = headerArticleValues[1];
131
132 if (Validator.isNull(footerArticleId) &&
133 Validator.isNull(headerArticleId)) {
134
135 if (_log.isWarnEnabled()) {
136 _log.warn(
137 "No article ids found in preferences of portlet " +
138 portletId);
139 }
140
141 return StringPool.BLANK;
142 }
143
144 long footerArticleGroupId = GetterUtil.getLong(footerArticleValues[0]);
145 long headerArticleGroupId = GetterUtil.getLong(headerArticleValues[0]);
146
147 if ((footerArticleGroupId <= 0) && (headerArticleGroupId <= 0)) {
148 if (_log.isWarnEnabled()) {
149 _log.warn(
150 "No group ids found in preferences of portlet " +
151 portletId);
152 }
153
154 return StringPool.BLANK;
155 }
156
157 List<JournalArticle> articles = new ArrayList<JournalArticle>(2);
158
159 JournalArticle footerArticle = null;
160
161 try {
162 footerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
163 footerArticleGroupId, footerArticleId,
164 WorkflowConstants.STATUS_APPROVED);
165
166 articles.add(footerArticle);
167 }
168 catch (NoSuchArticleException nsae) {
169 if (_log.isWarnEnabled()) {
170 _log.warn(
171 "No approved article found with group id " +
172 footerArticleGroupId + " and article id " +
173 footerArticleId);
174 }
175 }
176
177 JournalArticle headerArticle = null;
178
179 try {
180 headerArticle = JournalArticleLocalServiceUtil.getLatestArticle(
181 headerArticleGroupId, headerArticleId,
182 WorkflowConstants.STATUS_APPROVED);
183
184 articles.add(headerArticle);
185 }
186 catch (NoSuchArticleException nsae) {
187 if (_log.isWarnEnabled()) {
188 _log.warn(
189 "No approved article found with group id " +
190 headerArticleGroupId + " and article id " +
191 headerArticleId);
192 }
193 }
194
195 if ((footerArticle == null) && (headerArticle == null)) {
196 return StringPool.BLANK;
197 }
198
199 Element rootElement = addExportDataRootElement(portletDataContext);
200
201 for (JournalArticle article : articles) {
202 String path = ExportImportPathUtil.getModelPath(article);
203
204 Element articleElement = null;
205
206 if (article == footerArticle) {
207 articleElement = rootElement.addElement("footer-article");
208 }
209 else {
210 articleElement = rootElement.addElement("header-article");
211 }
212
213 articleElement.addAttribute("path", path);
214
215 StagedModelDataHandlerUtil.exportReferenceStagedModel(
216 portletDataContext, article, article,
217 PortletDataContext.REFERENCE_TYPE_WEAK);
218 }
219
220 return getExportDataRootElementString(rootElement);
221 }
222
223 @Override
224 protected PortletPreferences doImportData(
225 PortletDataContext portletDataContext, String portletId,
226 PortletPreferences portletPreferences, String data)
227 throws Exception {
228
229 Layout layout = LayoutLocalServiceUtil.getLayout(
230 portletDataContext.getPlid());
231
232 Element rootElement = portletDataContext.getImportDataRootElement();
233
234 Element footerArticleElement = rootElement.element("footer-article");
235
236 importReferenceArticle(portletDataContext, footerArticleElement);
237
238 Map<String, String> articleIds =
239 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
240 JournalArticle.class + ".articleId");
241
242 String[] footerArticleValues = portletPreferences.getValues(
243 "footerArticleValues", new String[] {"0", ""});
244
245 String footerArticleId = footerArticleValues[1];
246
247 footerArticleId = MapUtil.getString(
248 articleIds, footerArticleId, footerArticleId);
249
250 if (Validator.isNotNull(footerArticleId)) {
251 footerArticleId = MapUtil.getString(
252 articleIds, footerArticleId, footerArticleId);
253
254 portletPreferences.setValues(
255 "footerArticleValues",
256 new String[] {
257 String.valueOf(portletDataContext.getScopeGroupId()),
258 footerArticleId
259 });
260
261 JournalContentSearchLocalServiceUtil.updateContentSearch(
262 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
263 layout.getLayoutId(), portletId, footerArticleId, true);
264 }
265
266 Element headerArticleElement = rootElement.element("header-article");
267
268 importReferenceArticle(portletDataContext, headerArticleElement);
269
270 String[] headerArticleValues = portletPreferences.getValues(
271 "headerArticleValues", new String[] {"0", ""});
272
273 String headerArticleId = headerArticleValues[1];
274
275 headerArticleId = MapUtil.getString(
276 articleIds, headerArticleId, headerArticleId);
277
278 if (Validator.isNotNull(headerArticleId)) {
279 headerArticleId = MapUtil.getString(
280 articleIds, headerArticleId, headerArticleId);
281
282 portletPreferences.setValues(
283 "headerArticleValues",
284 new String[] {
285 String.valueOf(portletDataContext.getScopeGroupId()),
286 headerArticleId
287 });
288
289 JournalContentSearchLocalServiceUtil.updateContentSearch(
290 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
291 layout.getLayoutId(), portletId, headerArticleId, true);
292 }
293
294 return portletPreferences;
295 }
296
297 protected void importReferenceArticle(
298 PortletDataContext portletDataContext, Element parentElement)
299 throws PortletDataException {
300
301 List<Element> referenceDataElements =
302 portletDataContext.getReferenceDataElements(
303 parentElement, JournalArticle.class);
304
305 if (referenceDataElements.isEmpty()) {
306 return;
307 }
308
309 for (Element referenceDataElement : referenceDataElements) {
310 StagedModelDataHandlerUtil.importReferenceStagedModel(
311 portletDataContext, referenceDataElement);
312 }
313 }
314
315 private static Log _log = LogFactoryUtil.getLog(
316 RSSPortletDataHandler.class);
317
318 }