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