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