1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.MimeTypesUtil;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Image;
34  import com.liferay.portal.service.ImageLocalServiceUtil;
35  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
36  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
37  import com.liferay.portlet.imagegallery.model.IGImage;
38  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
39  import com.liferay.portlet.journal.model.JournalArticle;
40  import com.liferay.portlet.journal.model.JournalFeed;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
43  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
44  
45  import com.sun.syndication.feed.synd.SyndEnclosure;
46  import com.sun.syndication.feed.synd.SyndEnclosureImpl;
47  import com.sun.syndication.feed.synd.SyndLink;
48  import com.sun.syndication.feed.synd.SyndLinkImpl;
49  
50  import java.util.ArrayList;
51  import java.util.Date;
52  import java.util.List;
53  import java.util.Map;
54  
55  /**
56   * <a href="JournalRSSUtil.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Raymond Augé
59   *
60   */
61  public class JournalRSSUtil {
62  
63      public static List<JournalArticle> getArticles(JournalFeed feed)
64          throws SystemException {
65  
66          long companyId = feed.getCompanyId();
67          long groupId = feed.getGroupId();
68          String articleId = null;
69          Double version = null;
70          String title = null;
71          String description = null;
72          String content = null;
73  
74          String type = feed.getType();
75  
76          if (Validator.isNull(type)) {
77              type = null;
78          }
79  
80          String structureId = feed.getStructureId();
81  
82          if (Validator.isNull(structureId)) {
83              structureId = null;
84          }
85  
86          String templateId = feed.getTemplateId();
87  
88          if (Validator.isNull(templateId)) {
89              templateId = null;
90          }
91  
92          Date displayDateGT = null;
93          Date displayDateLT = new Date();
94          Boolean approved = Boolean.TRUE;
95          Boolean expired = Boolean.FALSE;
96          Date reviewDate = null;
97          boolean andOperator = true;
98          int start = 0;
99          int end = feed.getDelta();
100 
101         String orderByCol = feed.getOrderByCol();
102         String orderByType = feed.getOrderByType();
103         boolean orderByAsc = orderByType.equals("asc");
104 
105         OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
106 
107         if (orderByCol.equals("display-date")) {
108             obc = new ArticleDisplayDateComparator(orderByAsc);
109         }
110 
111         return JournalArticleLocalServiceUtil.search(
112             companyId, groupId, articleId, version, title, description, content,
113             type, structureId, templateId, displayDateGT, displayDateLT,
114             approved, expired, reviewDate, andOperator, start, end, obc);
115     }
116 
117     public static List<SyndEnclosure> getDLEnclosures(
118         String portalURL, String url) {
119 
120         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
121 
122         DLFileEntry fileEntry = getDLFileEntry(url);
123 
124         if (fileEntry != null) {
125             SyndEnclosure enclosure = new SyndEnclosureImpl();
126 
127             enclosure.setLength(fileEntry.getSize());
128 
129             enclosure.setType(
130                 MimeTypesUtil.getContentType(
131                     fileEntry.getTitleWithExtension()));
132 
133             enclosure.setUrl(portalURL + url);
134 
135             enclosures.add(enclosure);
136         }
137 
138         return enclosures;
139     }
140 
141     public static DLFileEntry getDLFileEntry(String url) {
142         DLFileEntry fileEntry = null;
143 
144         String queryString = HttpUtil.getQueryString(url);
145 
146         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
147             queryString);
148 
149         if (parameters.containsKey("folderId") &&
150             parameters.containsKey("name")) {
151 
152             try {
153                 long folderId = GetterUtil.getLong(
154                     parameters.get("folderId")[0]);
155                 String name = parameters.get("name")[0];
156 
157                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
158                     folderId, name);
159             }
160             catch (Exception e) {
161                 if (_log.isWarnEnabled()) {
162                     _log.warn(e, e);
163                 }
164             }
165         }
166         else if (parameters.containsKey("uuid") &&
167                  parameters.containsKey("groupId")) {
168 
169             try {
170                 String uuid = parameters.get("uuid")[0];
171                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
172 
173                 fileEntry =
174                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
175                         uuid, groupId);
176             }
177             catch (Exception e) {
178                 if (_log.isWarnEnabled()) {
179                     _log.warn(e, e);
180                 }
181             }
182         }
183 
184         return fileEntry;
185     }
186 
187     public static List<SyndLink> getDLLinks(String portalURL, String url) {
188         List<SyndLink> links = new ArrayList<SyndLink>();
189 
190         DLFileEntry fileEntry = getDLFileEntry(url);
191 
192         if (fileEntry != null) {
193             SyndLink link = new SyndLinkImpl();
194 
195             link.setHref(portalURL + url);
196 
197             link.setLength(fileEntry.getSize());
198 
199             link.setRel("enclosure");
200 
201             link.setType(
202                 MimeTypesUtil.getContentType(
203                     fileEntry.getTitleWithExtension()));
204 
205             links.add(link);
206         }
207 
208         return links;
209     }
210 
211     public static List<SyndEnclosure> getIGEnclosures(
212         String portalURL, String url) {
213 
214         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
215 
216         Image image = getImage(url);
217 
218         if (image != null) {
219             SyndEnclosure enclosure = new SyndEnclosureImpl();
220 
221             enclosure.setLength(image.getSize());
222 
223             enclosure.setType(
224                 MimeTypesUtil.getContentType("*." + image.getType()));
225 
226             enclosure.setUrl(portalURL + url);
227 
228             enclosures.add(enclosure);
229         }
230 
231         return enclosures;
232     }
233 
234     public static List<SyndLink> getIGLinks(String portalURL, String url) {
235         List<SyndLink> links = new ArrayList<SyndLink>();
236 
237         Image image = getImage(url);
238 
239         if (image != null) {
240             SyndLink link = new SyndLinkImpl();
241 
242             link.setHref(portalURL + url);
243 
244             link.setLength(image.getSize());
245 
246             link.setRel("enclosure");
247 
248             link.setType(
249                 MimeTypesUtil.getContentType("*." + image.getType()));
250 
251             links.add(link);
252         }
253 
254         return links;
255     }
256 
257     public static Image getImage(String url) {
258         Image image = null;
259 
260         String queryString = HttpUtil.getQueryString(url);
261 
262         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
263             queryString);
264 
265         if (parameters.containsKey("image_id") ||
266             parameters.containsKey("img_id") ||
267             parameters.containsKey("i_id")) {
268 
269             try {
270                 long imageId = 0;
271 
272                 if (parameters.containsKey("image_id")) {
273                     imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
274                 }
275                 else if (parameters.containsKey("img_id")) {
276                     imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
277                 }
278                 else if (parameters.containsKey("i_id")) {
279                     imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
280                 }
281 
282                 image = ImageLocalServiceUtil.getImage(imageId);
283             }
284             catch (Exception e) {
285                 if (_log.isWarnEnabled()) {
286                     _log.warn(e, e);
287                 }
288             }
289         }
290         else if (parameters.containsKey("uuid") &&
291                  parameters.containsKey("groupId")) {
292 
293             try {
294                 String uuid = parameters.get("uuid")[0];
295                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
296 
297                 IGImage igImage =
298                     IGImageLocalServiceUtil.getImageByUuidAndGroupId(
299                         uuid, groupId);
300 
301                 image = ImageLocalServiceUtil.getImage(
302                     igImage.getLargeImageId());
303             }
304             catch (Exception e) {
305                 if (_log.isWarnEnabled()) {
306                     _log.warn(e, e);
307                 }
308             }
309         }
310 
311         return image;
312     }
313 
314     private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
315 
316 }