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