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  public class JournalRSSUtil {
61  
62      public static List<JournalArticle> getArticles(JournalFeed feed)
63          throws SystemException {
64  
65          long companyId = feed.getCompanyId();
66          long groupId = feed.getGroupId();
67          String articleId = null;
68          Double version = null;
69          String title = null;
70          String description = null;
71          String content = null;
72  
73          String type = feed.getType();
74  
75          if (Validator.isNull(type)) {
76              type = null;
77          }
78  
79          String structureId = feed.getStructureId();
80  
81          if (Validator.isNull(structureId)) {
82              structureId = null;
83          }
84  
85          String templateId = feed.getTemplateId();
86  
87          if (Validator.isNull(templateId)) {
88              templateId = null;
89          }
90  
91          Date displayDateGT = null;
92          Date displayDateLT = new Date();
93          Boolean approved = Boolean.TRUE;
94          Boolean expired = Boolean.FALSE;
95          Date reviewDate = null;
96          boolean andOperator = true;
97          int start = 0;
98          int end = feed.getDelta();
99  
100         String orderByCol = feed.getOrderByCol();
101         String orderByType = feed.getOrderByType();
102         boolean orderByAsc = orderByType.equals("asc");
103 
104         OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
105 
106         if (orderByCol.equals("display-date")) {
107             obc = new ArticleDisplayDateComparator(orderByAsc);
108         }
109 
110         return JournalArticleLocalServiceUtil.search(
111             companyId, groupId, articleId, version, title, description, content,
112             type, structureId, templateId, displayDateGT, displayDateLT,
113             approved, expired, reviewDate, andOperator, start, end, obc);
114     }
115 
116     public static List<SyndEnclosure> getDLEnclosures(
117         String portalURL, String url) {
118 
119         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
120 
121         DLFileEntry fileEntry = getDLFileEntry(url);
122 
123         if (fileEntry != null) {
124             SyndEnclosure enclosure = new SyndEnclosureImpl();
125 
126             enclosure.setLength(fileEntry.getSize());
127 
128             enclosure.setType(
129                 MimeTypesUtil.getContentType(
130                     fileEntry.getTitleWithExtension()));
131 
132             enclosure.setUrl(portalURL + url);
133 
134             enclosures.add(enclosure);
135         }
136 
137         return enclosures;
138     }
139 
140     public static DLFileEntry getDLFileEntry(String url) {
141         DLFileEntry fileEntry = null;
142 
143         String queryString = HttpUtil.getQueryString(url);
144 
145         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
146             queryString);
147 
148         if (parameters.containsKey("folderId") &&
149             parameters.containsKey("name")) {
150 
151             try {
152                 long folderId = GetterUtil.getLong(
153                     parameters.get("folderId")[0]);
154                 String name = parameters.get("name")[0];
155 
156                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
157                     folderId, name);
158             }
159             catch (Exception e) {
160                 if (_log.isWarnEnabled()) {
161                     _log.warn(e, e);
162                 }
163             }
164         }
165         else if (parameters.containsKey("uuid") &&
166                  parameters.containsKey("groupId")) {
167 
168             try {
169                 String uuid = parameters.get("uuid")[0];
170                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
171 
172                 fileEntry =
173                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
174                         uuid, groupId);
175             }
176             catch (Exception e) {
177                 if (_log.isWarnEnabled()) {
178                     _log.warn(e, e);
179                 }
180             }
181         }
182 
183         return fileEntry;
184     }
185 
186     public static List<SyndLink> getDLLinks(String portalURL, String url) {
187         List<SyndLink> links = new ArrayList<SyndLink>();
188 
189         DLFileEntry fileEntry = getDLFileEntry(url);
190 
191         if (fileEntry != null) {
192             SyndLink link = new SyndLinkImpl();
193 
194             link.setHref(portalURL + url);
195 
196             link.setLength(fileEntry.getSize());
197 
198             link.setRel("enclosure");
199 
200             link.setType(
201                 MimeTypesUtil.getContentType(
202                     fileEntry.getTitleWithExtension()));
203 
204             links.add(link);
205         }
206 
207         return links;
208     }
209 
210     public static List<SyndEnclosure> getIGEnclosures(
211         String portalURL, String url) {
212 
213         List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
214 
215         Image image = getImage(url);
216 
217         if (image != null) {
218             SyndEnclosure enclosure = new SyndEnclosureImpl();
219 
220             enclosure.setLength(image.getSize());
221 
222             enclosure.setType(
223                 MimeTypesUtil.getContentType("*." + image.getType()));
224 
225             enclosure.setUrl(portalURL + url);
226 
227             enclosures.add(enclosure);
228         }
229 
230         return enclosures;
231     }
232 
233     public static List<SyndLink> getIGLinks(String portalURL, String url) {
234         List<SyndLink> links = new ArrayList<SyndLink>();
235 
236         Image image = getImage(url);
237 
238         if (image != null) {
239             SyndLink link = new SyndLinkImpl();
240 
241             link.setHref(portalURL + url);
242 
243             link.setLength(image.getSize());
244 
245             link.setRel("enclosure");
246 
247             link.setType(
248                 MimeTypesUtil.getContentType("*." + image.getType()));
249 
250             links.add(link);
251         }
252 
253         return links;
254     }
255 
256     public static Image getImage(String url) {
257         Image image = null;
258 
259         String queryString = HttpUtil.getQueryString(url);
260 
261         Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
262             queryString);
263 
264         if (parameters.containsKey("image_id") ||
265             parameters.containsKey("img_id") ||
266             parameters.containsKey("i_id")) {
267 
268             try {
269                 long imageId = 0;
270 
271                 if (parameters.containsKey("image_id")) {
272                     imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
273                 }
274                 else if (parameters.containsKey("img_id")) {
275                     imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
276                 }
277                 else if (parameters.containsKey("i_id")) {
278                     imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
279                 }
280 
281                 image = ImageLocalServiceUtil.getImage(imageId);
282             }
283             catch (Exception e) {
284                 if (_log.isWarnEnabled()) {
285                     _log.warn(e, e);
286                 }
287             }
288         }
289         else if (parameters.containsKey("uuid") &&
290                  parameters.containsKey("groupId")) {
291 
292             try {
293                 String uuid = parameters.get("uuid")[0];
294                 long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
295 
296                 IGImage igImage =
297                     IGImageLocalServiceUtil.getImageByUuidAndGroupId(
298                         uuid, groupId);
299 
300                 image = ImageLocalServiceUtil.getImage(
301                     igImage.getLargeImageId());
302             }
303             catch (Exception e) {
304                 if (_log.isWarnEnabled()) {
305                     _log.warn(e, e);
306                 }
307             }
308         }
309 
310         return image;
311     }
312 
313     private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
314 
315 }