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.exportStagedModel(
216 portletDataContext, article);
217
218 portletDataContext.addReferenceElement(
219 article, articleElement, article,
220 PortletDataContext.REFERENCE_TYPE_WEAK, false);
221 }
222
223 return getExportDataRootElementString(rootElement);
224 }
225
226 @Override
227 protected PortletPreferences doImportData(
228 PortletDataContext portletDataContext, String portletId,
229 PortletPreferences portletPreferences, String data)
230 throws Exception {
231
232 Map<String, String> articleIds =
233 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
234 JournalArticle.class + ".articleId");
235
236 Layout layout = LayoutLocalServiceUtil.getLayout(
237 portletDataContext.getPlid());
238
239 Element rootElement = portletDataContext.getImportDataRootElement();
240
241 Element footerArticleElement = rootElement.element("footer-article");
242
243 importReferenceArticle(portletDataContext, footerArticleElement);
244
245 String[] footerArticleValues = portletPreferences.getValues(
246 "footerArticleValues", new String[] {"0", ""});
247
248 String footerArticleId = footerArticleValues[1];
249
250 footerArticleId = MapUtil.getString(
251 articleIds, footerArticleId, footerArticleId);
252
253 if (Validator.isNotNull(footerArticleId)) {
254 footerArticleId = MapUtil.getString(
255 articleIds, footerArticleId, footerArticleId);
256
257 portletPreferences.setValues(
258 "footerArticleValues",
259 new String[] {
260 String.valueOf(portletDataContext.getScopeGroupId()),
261 footerArticleId
262 });
263
264 JournalContentSearchLocalServiceUtil.updateContentSearch(
265 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
266 layout.getLayoutId(), portletId, footerArticleId, true);
267 }
268
269 Element headerArticleElement = rootElement.element("header-article");
270
271 importReferenceArticle(portletDataContext, headerArticleElement);
272
273 String[] headerArticleValues = portletPreferences.getValues(
274 "headerArticleValues", new String[] {"0", ""});
275
276 String headerArticleId = headerArticleValues[1];
277
278 headerArticleId = MapUtil.getString(
279 articleIds, headerArticleId, headerArticleId);
280
281 if (Validator.isNotNull(headerArticleId)) {
282 headerArticleId = MapUtil.getString(
283 articleIds, headerArticleId, headerArticleId);
284
285 portletPreferences.setValues(
286 "headerArticleValues",
287 new String[] {
288 String.valueOf(portletDataContext.getScopeGroupId()),
289 headerArticleId
290 });
291
292 JournalContentSearchLocalServiceUtil.updateContentSearch(
293 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
294 layout.getLayoutId(), portletId, headerArticleId, true);
295 }
296
297 return portletPreferences;
298 }
299
300 protected void importReferenceArticle(
301 PortletDataContext portletDataContext, Element parentElement)
302 throws PortletDataException {
303
304 List<Element> referenceDataElements =
305 portletDataContext.getReferenceDataElements(
306 parentElement, JournalArticle.class);
307
308 if (referenceDataElements.isEmpty()) {
309 return;
310 }
311
312 for (Element referenceDataElement : referenceDataElements) {
313 StagedModelDataHandlerUtil.importStagedModel(
314 portletDataContext, referenceDataElement);
315 }
316 }
317
318 private static Log _log = LogFactoryUtil.getLog(
319 RSSPortletDataHandler.class);
320
321 }