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