1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.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.model.Image;
26  import com.liferay.portal.service.ImageLocalServiceUtil;
27  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
29  import com.liferay.portlet.imagegallery.model.IGImage;
30  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
31  import com.liferay.portlet.journal.model.JournalArticle;
32  import com.liferay.portlet.journal.model.JournalFeed;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
35  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
36  
37  import com.sun.syndication.feed.synd.SyndEnclosure;
38  import com.sun.syndication.feed.synd.SyndEnclosureImpl;
39  import com.sun.syndication.feed.synd.SyndLink;
40  import com.sun.syndication.feed.synd.SyndLinkImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * <a href="JournalRSSUtil.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Raymond Augé
51   */
52  public class JournalRSSUtil {
53  
54      public static List<JournalArticle> getArticles(JournalFeed feed)
55          throws SystemException {
56  
57          long companyId = feed.getCompanyId();
58          long groupId = feed.getGroupId();
59          String articleId = null;
60          Double version = null;
61          String title = null;
62          String description = null;
63          String content = null;
64  
65          String type = feed.getType();
66  
67          if (Validator.isNull(type)) {
68              type = null;
69          }
70  
71          String structureId = feed.getStructureId();
72  
73          if (Validator.isNull(structureId)) {
74              structureId = null;
75          }
76  
77          String templateId = feed.getTemplateId();
78  
79          if (Validator.isNull(templateId)) {
80              templateId = null;
81          }
82  
83          Date displayDateGT = null;
84          Date displayDateLT = new Date();
85          Boolean approved = Boolean.TRUE;
86          Boolean expired = Boolean.FALSE;
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             approved, expired, 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(
122                     fileEntry.getTitleWithExtension()));
123 
124             enclosure.setUrl(portalURL + url);
125 
126             enclosures.add(enclosure);
127         }
128 
129         return enclosures;
130     }
131 
132     public static DLFileEntry getDLFileEntry(String url) {
133         DLFileEntry fileEntry = null;
134 
135         String queryString = HttpUtil.getQueryString(url);
136 
137         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
138             queryString);
139 
140         if (parameters.containsKey("folderId") &&
141             parameters.containsKey("name")) {
142 
143             try {
144                 long folderId = GetterUtil.getLong(
145                     parameters.get("folderId")[0]);
146                 String name = parameters.get("name")[0];
147 
148                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
149                     folderId, name);
150             }
151             catch (Exception e) {
152                 if (_log.isWarnEnabled()) {
153                     _log.warn(e, e);
154                 }
155             }
156         }
157         else if (parameters.containsKey("uuid") &&
158                  parameters.containsKey("groupId")) {
159 
160             try {
161                 String uuid = parameters.get("uuid")[0];
162                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
163 
164                 fileEntry =
165                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
166                         uuid, groupId);
167             }
168             catch (Exception e) {
169                 if (_log.isWarnEnabled()) {
170                     _log.warn(e, e);
171                 }
172             }
173         }
174 
175         return fileEntry;
176     }
177 
178     public static List<SyndLink> getDLLinks(String portalURL, String url) {
179         List<SyndLink> links = new ArrayList<SyndLink>();
180 
181         DLFileEntry fileEntry = getDLFileEntry(url);
182 
183         if (fileEntry != null) {
184             SyndLink link = new SyndLinkImpl();
185 
186             link.setHref(portalURL + url);
187 
188             link.setLength(fileEntry.getSize());
189 
190             link.setRel("enclosure");
191 
192             link.setType(
193                 MimeTypesUtil.getContentType(
194                     fileEntry.getTitleWithExtension()));
195 
196             links.add(link);
197         }
198 
199         return links;
200     }
201 
202     public static List<SyndEnclosure> getIGEnclosures(
203         String portalURL, String url) {
204 
205         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
206 
207         Image image = getImage(url);
208 
209         if (image != null) {
210             SyndEnclosure enclosure = new SyndEnclosureImpl();
211 
212             enclosure.setLength(image.getSize());
213 
214             enclosure.setType(
215                 MimeTypesUtil.getContentType("*." + image.getType()));
216 
217             enclosure.setUrl(portalURL + url);
218 
219             enclosures.add(enclosure);
220         }
221 
222         return enclosures;
223     }
224 
225     public static List<SyndLink> getIGLinks(String portalURL, String url) {
226         List<SyndLink> links = new ArrayList<SyndLink>();
227 
228         Image image = getImage(url);
229 
230         if (image != null) {
231             SyndLink link = new SyndLinkImpl();
232 
233             link.setHref(portalURL + url);
234 
235             link.setLength(image.getSize());
236 
237             link.setRel("enclosure");
238 
239             link.setType(
240                 MimeTypesUtil.getContentType("*." + image.getType()));
241 
242             links.add(link);
243         }
244 
245         return links;
246     }
247 
248     public static Image getImage(String url) {
249         Image image = null;
250 
251         String queryString = HttpUtil.getQueryString(url);
252 
253         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
254             queryString);
255 
256         if (parameters.containsKey("image_id") ||
257             parameters.containsKey("img_id") ||
258             parameters.containsKey("i_id")) {
259 
260             try {
261                 long imageId = 0;
262 
263                 if (parameters.containsKey("image_id")) {
264                     imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
265                 }
266                 else if (parameters.containsKey("img_id")) {
267                     imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
268                 }
269                 else if (parameters.containsKey("i_id")) {
270                     imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
271                 }
272 
273                 image = ImageLocalServiceUtil.getImage(imageId);
274             }
275             catch (Exception e) {
276                 if (_log.isWarnEnabled()) {
277                     _log.warn(e, e);
278                 }
279             }
280         }
281         else if (parameters.containsKey("uuid") &&
282                  parameters.containsKey("groupId")) {
283 
284             try {
285                 String uuid = parameters.get("uuid")[0];
286                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
287 
288                 IGImage igImage =
289                     IGImageLocalServiceUtil.getImageByUuidAndGroupId(
290                         uuid, groupId);
291 
292                 image = ImageLocalServiceUtil.getImage(
293                     igImage.getLargeImageId());
294             }
295             catch (Exception e) {
296                 if (_log.isWarnEnabled()) {
297                     _log.warn(e, e);
298                 }
299             }
300         }
301 
302         return image;
303     }
304 
305     private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
306 
307 }