1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.exception.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.MimeTypesUtil;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.workflow.StatusConstants;
26  import com.liferay.portal.model.Image;
27  import com.liferay.portal.service.ImageLocalServiceUtil;
28  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.model.JournalFeed;
34  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
35  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
36  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
37  
38  import com.sun.syndication.feed.synd.SyndEnclosure;
39  import com.sun.syndication.feed.synd.SyndEnclosureImpl;
40  import com.sun.syndication.feed.synd.SyndLink;
41  import com.sun.syndication.feed.synd.SyndLinkImpl;
42  
43  import java.util.ArrayList;
44  import java.util.Date;
45  import java.util.List;
46  import java.util.Map;
47  
48  /**
49   * <a href="JournalRSSUtil.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Raymond Augé
52   */
53  public class JournalRSSUtil {
54  
55      public static List<JournalArticle> getArticles(JournalFeed feed)
56          throws SystemException {
57  
58          long companyId = feed.getCompanyId();
59          long groupId = feed.getGroupId();
60          String articleId = null;
61          Double version = null;
62          String title = null;
63          String description = null;
64          String content = null;
65  
66          String type = feed.getType();
67  
68          if (Validator.isNull(type)) {
69              type = null;
70          }
71  
72          String structureId = feed.getStructureId();
73  
74          if (Validator.isNull(structureId)) {
75              structureId = null;
76          }
77  
78          String templateId = feed.getTemplateId();
79  
80          if (Validator.isNull(templateId)) {
81              templateId = null;
82          }
83  
84          Date displayDateGT = null;
85          Date displayDateLT = new Date();
86          int status = StatusConstants.APPROVED;
87          Date reviewDate = null;
88          boolean andOperator = true;
89          int start = 0;
90          int end = feed.getDelta();
91  
92          String orderByCol = feed.getOrderByCol();
93          String orderByType = feed.getOrderByType();
94          boolean orderByAsc = orderByType.equals("asc");
95  
96          OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
97  
98          if (orderByCol.equals("display-date")) {
99              obc = new ArticleDisplayDateComparator(orderByAsc);
100         }
101 
102         return JournalArticleLocalServiceUtil.search(
103             companyId, groupId, articleId, version, title, description, content,
104             type, structureId, templateId, displayDateGT, displayDateLT,
105             status, reviewDate, andOperator, start, end, obc);
106     }
107 
108     public static List<SyndEnclosure> getDLEnclosures(
109         String portalURL, String url) {
110 
111         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
112 
113         DLFileEntry fileEntry = getDLFileEntry(url);
114 
115         if (fileEntry != null) {
116             SyndEnclosure enclosure = new SyndEnclosureImpl();
117 
118             enclosure.setLength(fileEntry.getSize());
119 
120             enclosure.setType(
121                 MimeTypesUtil.getContentType(fileEntry.getTitle()));
122 
123             enclosure.setUrl(portalURL + url);
124 
125             enclosures.add(enclosure);
126         }
127 
128         return enclosures;
129     }
130 
131     public static DLFileEntry getDLFileEntry(String url) {
132         DLFileEntry fileEntry = null;
133 
134         String queryString = HttpUtil.getQueryString(url);
135 
136         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
137             queryString);
138 
139         if (parameters.containsKey("folderId") &&
140             parameters.containsKey("name")) {
141 
142             try {
143                 long groupId = GetterUtil.getLong(
144                     parameters.get("groupId")[0]);
145                 long folderId = GetterUtil.getLong(
146                     parameters.get("folderId")[0]);
147                 String name = parameters.get("name")[0];
148 
149                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
150                     groupId, folderId, name);
151             }
152             catch (Exception e) {
153                 if (_log.isWarnEnabled()) {
154                     _log.warn(e, e);
155                 }
156             }
157         }
158         else if (parameters.containsKey("uuid") &&
159                  parameters.containsKey("groupId")) {
160 
161             try {
162                 String uuid = parameters.get("uuid")[0];
163                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
164 
165                 fileEntry =
166                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
167                         uuid, groupId);
168             }
169             catch (Exception e) {
170                 if (_log.isWarnEnabled()) {
171                     _log.warn(e, e);
172                 }
173             }
174         }
175 
176         return fileEntry;
177     }
178 
179     public static List<SyndLink> getDLLinks(String portalURL, String url) {
180         List<SyndLink> links = new ArrayList<SyndLink>();
181 
182         DLFileEntry fileEntry = getDLFileEntry(url);
183 
184         if (fileEntry != null) {
185             SyndLink link = new SyndLinkImpl();
186 
187             link.setHref(portalURL + url);
188 
189             link.setLength(fileEntry.getSize());
190 
191             link.setRel("enclosure");
192 
193             link.setType(MimeTypesUtil.getContentType(fileEntry.getTitle()));
194 
195             links.add(link);
196         }
197 
198         return links;
199     }
200 
201     public static List<SyndEnclosure> getIGEnclosures(
202         String portalURL, String url) {
203 
204         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
205 
206         Image image = getImage(url);
207 
208         if (image != null) {
209             SyndEnclosure enclosure = new SyndEnclosureImpl();
210 
211             enclosure.setLength(image.getSize());
212 
213             enclosure.setType(
214                 MimeTypesUtil.getContentType("*." + image.getType()));
215 
216             enclosure.setUrl(portalURL + url);
217 
218             enclosures.add(enclosure);
219         }
220 
221         return enclosures;
222     }
223 
224     public static List<SyndLink> getIGLinks(String portalURL, String url) {
225         List<SyndLink> links = new ArrayList<SyndLink>();
226 
227         Image image = getImage(url);
228 
229         if (image != null) {
230             SyndLink link = new SyndLinkImpl();
231 
232             link.setHref(portalURL + url);
233 
234             link.setLength(image.getSize());
235 
236             link.setRel("enclosure");
237 
238             link.setType(
239                 MimeTypesUtil.getContentType("*." + image.getType()));
240 
241             links.add(link);
242         }
243 
244         return links;
245     }
246 
247     public static Image getImage(String url) {
248         Image image = null;
249 
250         String queryString = HttpUtil.getQueryString(url);
251 
252         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
253             queryString);
254 
255         if (parameters.containsKey("image_id") ||
256             parameters.containsKey("img_id") ||
257             parameters.containsKey("i_id")) {
258 
259             try {
260                 long imageId = 0;
261 
262                 if (parameters.containsKey("image_id")) {
263                     imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
264                 }
265                 else if (parameters.containsKey("img_id")) {
266                     imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
267                 }
268                 else if (parameters.containsKey("i_id")) {
269                     imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
270                 }
271 
272                 image = ImageLocalServiceUtil.getImage(imageId);
273             }
274             catch (Exception e) {
275                 if (_log.isWarnEnabled()) {
276                     _log.warn(e, e);
277                 }
278             }
279         }
280         else if (parameters.containsKey("uuid") &&
281                  parameters.containsKey("groupId")) {
282 
283             try {
284                 String uuid = parameters.get("uuid")[0];
285                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
286 
287                 IGImage igImage =
288                     IGImageLocalServiceUtil.getImageByUuidAndGroupId(
289                         uuid, groupId);
290 
291                 image = ImageLocalServiceUtil.getImage(
292                     igImage.getLargeImageId());
293             }
294             catch (Exception e) {
295                 if (_log.isWarnEnabled()) {
296                     _log.warn(e, e);
297                 }
298             }
299         }
300 
301         return image;
302     }
303 
304     private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
305 
306 }