001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.repository.model.FileEntry;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.HttpUtil;
024 import com.liferay.portal.kernel.util.MimeTypesUtil;
025 import com.liferay.portal.kernel.util.OrderByComparator;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.workflow.WorkflowConstants;
029 import com.liferay.portal.model.Image;
030 import com.liferay.portal.service.ImageLocalServiceUtil;
031 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032 import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
033 import com.liferay.portlet.journal.model.JournalArticle;
034 import com.liferay.portlet.journal.model.JournalFeed;
035 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
036 import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
037 import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
038
039 import com.sun.syndication.feed.synd.SyndEnclosure;
040 import com.sun.syndication.feed.synd.SyndEnclosureImpl;
041 import com.sun.syndication.feed.synd.SyndLink;
042 import com.sun.syndication.feed.synd.SyndLinkImpl;
043
044 import java.util.ArrayList;
045 import java.util.Collections;
046 import java.util.Date;
047 import java.util.List;
048 import java.util.Map;
049 import java.util.Set;
050
051
054 public class JournalRSSUtil {
055
056 public static List<JournalArticle> getArticles(JournalFeed feed)
057 throws SystemException {
058
059 long companyId = feed.getCompanyId();
060 long groupId = feed.getGroupId();
061 List<Long> folderIds = Collections.emptyList();
062 String articleId = null;
063 Double version = null;
064 String title = null;
065 String description = null;
066 String content = null;
067
068 String type = feed.getType();
069
070 if (Validator.isNull(type)) {
071 type = null;
072 }
073
074 String structureId = feed.getStructureId();
075
076 if (Validator.isNull(structureId)) {
077 structureId = null;
078 }
079
080 String templateId = feed.getTemplateId();
081
082 if (Validator.isNull(templateId)) {
083 templateId = null;
084 }
085
086 Date displayDateGT = null;
087 Date displayDateLT = new Date();
088 int status = WorkflowConstants.STATUS_APPROVED;
089 Date reviewDate = null;
090 boolean andOperator = true;
091 int start = 0;
092 int end = feed.getDelta();
093
094 String orderByCol = feed.getOrderByCol();
095 String orderByType = feed.getOrderByType();
096 boolean orderByAsc = orderByType.equals("asc");
097
098 OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
099
100 if (orderByCol.equals("display-date")) {
101 obc = new ArticleDisplayDateComparator(orderByAsc);
102 }
103
104 return JournalArticleLocalServiceUtil.search(
105 companyId, groupId, folderIds, 0, articleId, version, title,
106 description, content, type, structureId, templateId, displayDateGT,
107 displayDateLT, status, reviewDate, andOperator, start, end, obc);
108 }
109
110 public static List<SyndEnclosure> getDLEnclosures(
111 String portalURL, String url) {
112
113 List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
114
115 FileEntry fileEntry = getFileEntry(url);
116
117 if (fileEntry == null) {
118 return syndEnclosures;
119 }
120
121 SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
122
123 syndEnclosure.setLength(fileEntry.getSize());
124 syndEnclosure.setType(fileEntry.getMimeType());
125 syndEnclosure.setUrl(portalURL + url);
126
127 syndEnclosures.add(syndEnclosure);
128
129 return syndEnclosures;
130 }
131
132 public static List<SyndLink> getDLLinks(String portalURL, String url) {
133 List<SyndLink> syndLinks = new ArrayList<SyndLink>();
134
135 FileEntry fileEntry = getFileEntry(url);
136
137 if (fileEntry == null) {
138 return syndLinks;
139 }
140
141 SyndLink syndLink = new SyndLinkImpl();
142
143 syndLink.setHref(portalURL + url);
144 syndLink.setLength(fileEntry.getSize());
145 syndLink.setRel("enclosure");
146 syndLink.setType(fileEntry.getMimeType());
147
148 syndLinks.add(syndLink);
149
150 return syndLinks;
151 }
152
153 public static FileEntry getFileEntry(String url) {
154 FileEntry fileEntry = null;
155
156 String queryString = HttpUtil.getQueryString(url);
157
158 Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
159 queryString);
160
161 if (url.startsWith("/documents/")) {
162 String[] pathArray = StringUtil.split(url, CharPool.SLASH);
163
164 String uuid = null;
165 long groupId = GetterUtil.getLong(pathArray[2]);
166 long folderId = 0;
167 String title = null;
168
169 if (pathArray.length == 4) {
170 uuid = pathArray[3];
171 }
172 else if (pathArray.length == 5) {
173 folderId = GetterUtil.getLong(pathArray[3]);
174 title = HttpUtil.decodeURL(pathArray[4], true);
175 }
176 else if (pathArray.length > 5) {
177 uuid = pathArray[5];
178 }
179
180 try {
181 if (Validator.isNotNull(uuid)) {
182 fileEntry =
183 DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
184 uuid, groupId);
185 }
186 else {
187 fileEntry = DLAppLocalServiceUtil.getFileEntry(
188 groupId, folderId, title);
189 }
190 }
191 catch (Exception e) {
192 if (_log.isWarnEnabled()) {
193 _log.warn(e, e);
194 }
195 }
196 }
197 else if (parameters.containsKey("folderId") &&
198 parameters.containsKey("name")) {
199
200 try {
201 long fileEntryId = GetterUtil.getLong(
202 parameters.get("fileEntryId")[0]);
203
204 fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
205 }
206 catch (Exception e) {
207 if (_log.isWarnEnabled()) {
208 _log.warn(e, e);
209 }
210 }
211 }
212 else if (parameters.containsKey("uuid") &&
213 parameters.containsKey("groupId")) {
214
215 try {
216 String uuid = parameters.get("uuid")[0];
217 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
218
219 fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
220 uuid, groupId);
221 }
222 catch (Exception e) {
223 if (_log.isWarnEnabled()) {
224 _log.warn(e, e);
225 }
226 }
227 }
228
229 return fileEntry;
230 }
231
232 public static List<SyndEnclosure> getIGEnclosures(
233 String portalURL, String url) {
234
235 List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
236
237 Object[] imageProperties = getImageProperties(url);
238
239 if (imageProperties == null) {
240 return syndEnclosures;
241 }
242
243 SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
244
245 syndEnclosure.setLength((Long)imageProperties[1]);
246 syndEnclosure.setType(
247 MimeTypesUtil.getContentType("*." + imageProperties[0]));
248 syndEnclosure.setUrl(portalURL + url);
249
250 syndEnclosures.add(syndEnclosure);
251
252 return syndEnclosures;
253 }
254
255 public static List<SyndLink> getIGLinks(String portalURL, String url) {
256 List<SyndLink> syndLinks = new ArrayList<SyndLink>();
257
258 Object[] imageProperties = getImageProperties(url);
259
260 if (imageProperties == null) {
261 return syndLinks;
262 }
263
264 SyndLink syndLink = new SyndLinkImpl();
265
266 syndLink.setHref(portalURL + url);
267 syndLink.setLength((Long)imageProperties[1]);
268 syndLink.setRel("enclosure");
269 syndLink.setType(
270 MimeTypesUtil.getContentType("*." + imageProperties[0]));
271
272 syndLinks.add(syndLink);
273
274 return syndLinks;
275 }
276
277 public static Image getImage(String url) {
278 Image image = null;
279
280 String queryString = HttpUtil.getQueryString(url);
281
282 Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
283 queryString);
284
285 if (parameters.containsKey("image_id") ||
286 parameters.containsKey("img_id") ||
287 parameters.containsKey("i_id")) {
288
289 try {
290 long imageId = 0;
291
292 if (parameters.containsKey("image_id")) {
293 imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
294 }
295 else if (parameters.containsKey("img_id")) {
296 imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
297 }
298 else if (parameters.containsKey("i_id")) {
299 imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
300 }
301
302 image = ImageLocalServiceUtil.getImage(imageId);
303 }
304 catch (Exception e) {
305 if (_log.isWarnEnabled()) {
306 _log.warn(e, e);
307 }
308 }
309 }
310
311 return image;
312 }
313
314 protected static Object[] getImageProperties(String url) {
315 String type = null;
316 long size = 0;
317
318 Image image = getImage(url);
319
320 if (image != null) {
321 type = image.getType();
322 size = image.getSize();
323 }
324 else {
325 FileEntry fileEntry = getFileEntry(url);
326
327 Set<String> imageMimeTypes = ImageProcessorUtil.getImageMimeTypes();
328
329 if ((fileEntry != null) &&
330 imageMimeTypes.contains(fileEntry.getMimeType())) {
331
332 type = fileEntry.getExtension();
333 size = fileEntry.getSize();
334 }
335 }
336
337 if (Validator.isNotNull(type)) {
338 return new Object[] {type, size};
339 }
340
341 return null;
342 }
343
344 private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
345
346 }