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 ddmStructureKey = feed.getDDMStructureKey();
068
069 if (Validator.isNull(ddmStructureKey)) {
070 ddmStructureKey = null;
071 }
072
073 String ddmTemplateKey = feed.getDDMTemplateKey();
074
075 if (Validator.isNull(ddmTemplateKey)) {
076 ddmTemplateKey = null;
077 }
078
079 Date displayDateGT = null;
080 Date displayDateLT = new Date();
081 int status = WorkflowConstants.STATUS_APPROVED;
082 Date reviewDate = null;
083 boolean andOperator = true;
084 int start = 0;
085 int end = feed.getDelta();
086
087 String orderByCol = feed.getOrderByCol();
088 String orderByType = feed.getOrderByType();
089 boolean orderByAsc = orderByType.equals("asc");
090
091 OrderByComparator<JournalArticle> obc =
092 new ArticleModifiedDateComparator(orderByAsc);
093
094 if (orderByCol.equals("display-date")) {
095 obc = new ArticleDisplayDateComparator(orderByAsc);
096 }
097
098 return JournalArticleLocalServiceUtil.search(
099 companyId, groupId, folderIds,
100 JournalArticleConstants.CLASSNAME_ID_DEFAULT, articleId, version,
101 title, description, content, ddmStructureKey, ddmTemplateKey,
102 displayDateGT, displayDateLT, status, reviewDate, andOperator,
103 start, end, obc);
104 }
105
106 public static List<SyndEnclosure> getDLEnclosures(
107 String portalURL, String url) {
108
109 List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
110
111 FileEntry fileEntry = getFileEntry(url);
112
113 if (fileEntry == null) {
114 return syndEnclosures;
115 }
116
117 SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
118
119 syndEnclosure.setLength(fileEntry.getSize());
120 syndEnclosure.setType(fileEntry.getMimeType());
121 syndEnclosure.setUrl(portalURL + url);
122
123 syndEnclosures.add(syndEnclosure);
124
125 return syndEnclosures;
126 }
127
128 public static List<SyndLink> getDLLinks(String portalURL, String url) {
129 List<SyndLink> syndLinks = new ArrayList<SyndLink>();
130
131 FileEntry fileEntry = getFileEntry(url);
132
133 if (fileEntry == null) {
134 return syndLinks;
135 }
136
137 SyndLink syndLink = new SyndLinkImpl();
138
139 syndLink.setHref(portalURL + url);
140 syndLink.setLength(fileEntry.getSize());
141 syndLink.setRel("enclosure");
142 syndLink.setType(fileEntry.getMimeType());
143
144 syndLinks.add(syndLink);
145
146 return syndLinks;
147 }
148
149 public static FileEntry getFileEntry(String url) {
150 FileEntry fileEntry = null;
151
152 String queryString = HttpUtil.getQueryString(url);
153
154 Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
155 queryString);
156
157 if (url.startsWith("/documents/")) {
158 String[] pathArray = StringUtil.split(url, CharPool.SLASH);
159
160 String uuid = null;
161 long groupId = GetterUtil.getLong(pathArray[2]);
162 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
163 String title = null;
164
165 if (pathArray.length == 4) {
166 uuid = pathArray[3];
167 }
168 else if (pathArray.length == 5) {
169 folderId = GetterUtil.getLong(pathArray[3]);
170 title = HttpUtil.decodeURL(pathArray[4]);
171 }
172 else if (pathArray.length > 5) {
173 uuid = pathArray[5];
174 }
175
176 try {
177 if (Validator.isNotNull(uuid)) {
178 fileEntry =
179 DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
180 uuid, groupId);
181 }
182 else {
183 fileEntry = DLAppLocalServiceUtil.getFileEntry(
184 groupId, folderId, title);
185 }
186 }
187 catch (Exception e) {
188 if (_log.isWarnEnabled()) {
189 _log.warn(e, e);
190 }
191 }
192 }
193 else if (parameters.containsKey("folderId") &&
194 parameters.containsKey("name")) {
195
196 try {
197 long fileEntryId = GetterUtil.getLong(
198 parameters.get("fileEntryId")[0]);
199
200 fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
201 }
202 catch (Exception e) {
203 if (_log.isWarnEnabled()) {
204 _log.warn(e, e);
205 }
206 }
207 }
208 else if (parameters.containsKey("uuid") &&
209 parameters.containsKey("groupId")) {
210
211 try {
212 String uuid = parameters.get("uuid")[0];
213 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
214
215 fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
216 uuid, groupId);
217 }
218 catch (Exception e) {
219 if (_log.isWarnEnabled()) {
220 _log.warn(e, e);
221 }
222 }
223 }
224
225 return fileEntry;
226 }
227
228 public static List<SyndEnclosure> getIGEnclosures(
229 String portalURL, String url) {
230
231 List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
232
233 Object[] imageProperties = getImageProperties(url);
234
235 if (imageProperties == null) {
236 return syndEnclosures;
237 }
238
239 SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
240
241 syndEnclosure.setLength((Long)imageProperties[1]);
242 syndEnclosure.setType(
243 MimeTypesUtil.getExtensionContentType(
244 imageProperties[0].toString()));
245 syndEnclosure.setUrl(portalURL + url);
246
247 syndEnclosures.add(syndEnclosure);
248
249 return syndEnclosures;
250 }
251
252 public static List<SyndLink> getIGLinks(String portalURL, String url) {
253 List<SyndLink> syndLinks = new ArrayList<SyndLink>();
254
255 Object[] imageProperties = getImageProperties(url);
256
257 if (imageProperties == null) {
258 return syndLinks;
259 }
260
261 SyndLink syndLink = new SyndLinkImpl();
262
263 syndLink.setHref(portalURL + url);
264 syndLink.setLength((Long)imageProperties[1]);
265 syndLink.setRel("enclosure");
266 syndLink.setType(
267 MimeTypesUtil.getExtensionContentType(
268 imageProperties[0].toString()));
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 final Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
343
344 }