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