001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.MimeTypesUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.model.Image;
027    import com.liferay.portal.service.ImageLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    import com.liferay.portlet.imagegallery.model.IGImage;
031    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.journal.model.JournalFeed;
034    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
035    import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
036    import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
037    
038    import com.sun.syndication.feed.synd.SyndEnclosure;
039    import com.sun.syndication.feed.synd.SyndEnclosureImpl;
040    import com.sun.syndication.feed.synd.SyndLink;
041    import com.sun.syndication.feed.synd.SyndLinkImpl;
042    
043    import java.util.ArrayList;
044    import java.util.Date;
045    import java.util.List;
046    import java.util.Map;
047    
048    /**
049     * @author Raymond Augé
050     */
051    public class JournalRSSUtil {
052    
053            public static List<JournalArticle> getArticles(JournalFeed feed)
054                    throws SystemException {
055    
056                    long companyId = feed.getCompanyId();
057                    long groupId = feed.getGroupId();
058                    String articleId = null;
059                    Double version = null;
060                    String title = null;
061                    String description = null;
062                    String content = null;
063    
064                    String type = feed.getType();
065    
066                    if (Validator.isNull(type)) {
067                            type = null;
068                    }
069    
070                    String structureId = feed.getStructureId();
071    
072                    if (Validator.isNull(structureId)) {
073                            structureId = null;
074                    }
075    
076                    String templateId = feed.getTemplateId();
077    
078                    if (Validator.isNull(templateId)) {
079                            templateId = null;
080                    }
081    
082                    Date displayDateGT = null;
083                    Date displayDateLT = new Date();
084                    int status = WorkflowConstants.STATUS_APPROVED;
085                    Date reviewDate = null;
086                    boolean andOperator = true;
087                    int start = 0;
088                    int end = feed.getDelta();
089    
090                    String orderByCol = feed.getOrderByCol();
091                    String orderByType = feed.getOrderByType();
092                    boolean orderByAsc = orderByType.equals("asc");
093    
094                    OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
095    
096                    if (orderByCol.equals("display-date")) {
097                            obc = new ArticleDisplayDateComparator(orderByAsc);
098                    }
099    
100                    return JournalArticleLocalServiceUtil.search(
101                            companyId, groupId, articleId, version, title, description, content,
102                            type, structureId, templateId, displayDateGT, displayDateLT,
103                            status, reviewDate, andOperator, start, end, obc);
104            }
105    
106            public static List<SyndEnclosure> getDLEnclosures(
107                    String portalURL, String url) {
108    
109                    List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
110    
111                    DLFileEntry fileEntry = getDLFileEntry(url);
112    
113                    if (fileEntry != null) {
114                            SyndEnclosure enclosure = new SyndEnclosureImpl();
115    
116                            enclosure.setLength(fileEntry.getSize());
117    
118                            enclosure.setType(
119                                    MimeTypesUtil.getContentType(fileEntry.getTitle()));
120    
121                            enclosure.setUrl(portalURL + url);
122    
123                            enclosures.add(enclosure);
124                    }
125    
126                    return enclosures;
127            }
128    
129            public static DLFileEntry getDLFileEntry(String url) {
130                    DLFileEntry fileEntry = null;
131    
132                    String queryString = HttpUtil.getQueryString(url);
133    
134                    Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
135                            queryString);
136    
137                    if (parameters.containsKey("folderId") &&
138                            parameters.containsKey("name")) {
139    
140                            try {
141                                    long groupId = GetterUtil.getLong(
142                                            parameters.get("groupId")[0]);
143                                    long folderId = GetterUtil.getLong(
144                                            parameters.get("folderId")[0]);
145                                    String name = parameters.get("name")[0];
146    
147                                    fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
148                                            groupId, folderId, name);
149                            }
150                            catch (Exception e) {
151                                    if (_log.isWarnEnabled()) {
152                                            _log.warn(e, e);
153                                    }
154                            }
155                    }
156                    else if (parameters.containsKey("uuid") &&
157                                     parameters.containsKey("groupId")) {
158    
159                            try {
160                                    String uuid = parameters.get("uuid")[0];
161                                    long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
162    
163                                    fileEntry =
164                                            DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
165                                                    uuid, groupId);
166                            }
167                            catch (Exception e) {
168                                    if (_log.isWarnEnabled()) {
169                                            _log.warn(e, e);
170                                    }
171                            }
172                    }
173    
174                    return fileEntry;
175            }
176    
177            public static List<SyndLink> getDLLinks(String portalURL, String url) {
178                    List<SyndLink> links = new ArrayList<SyndLink>();
179    
180                    DLFileEntry fileEntry = getDLFileEntry(url);
181    
182                    if (fileEntry != null) {
183                            SyndLink link = new SyndLinkImpl();
184    
185                            link.setHref(portalURL + url);
186    
187                            link.setLength(fileEntry.getSize());
188    
189                            link.setRel("enclosure");
190    
191                            link.setType(MimeTypesUtil.getContentType(fileEntry.getTitle()));
192    
193                            links.add(link);
194                    }
195    
196                    return links;
197            }
198    
199            public static List<SyndEnclosure> getIGEnclosures(
200                    String portalURL, String url) {
201    
202                    List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
203    
204                    Image image = getImage(url);
205    
206                    if (image != null) {
207                            SyndEnclosure enclosure = new SyndEnclosureImpl();
208    
209                            enclosure.setLength(image.getSize());
210    
211                            enclosure.setType(
212                                    MimeTypesUtil.getContentType("*." + image.getType()));
213    
214                            enclosure.setUrl(portalURL + url);
215    
216                            enclosures.add(enclosure);
217                    }
218    
219                    return enclosures;
220            }
221    
222            public static List<SyndLink> getIGLinks(String portalURL, String url) {
223                    List<SyndLink> links = new ArrayList<SyndLink>();
224    
225                    Image image = getImage(url);
226    
227                    if (image != null) {
228                            SyndLink link = new SyndLinkImpl();
229    
230                            link.setHref(portalURL + url);
231    
232                            link.setLength(image.getSize());
233    
234                            link.setRel("enclosure");
235    
236                            link.setType(
237                                    MimeTypesUtil.getContentType("*." + image.getType()));
238    
239                            links.add(link);
240                    }
241    
242                    return links;
243            }
244    
245            public static Image getImage(String url) {
246                    Image image = null;
247    
248                    String queryString = HttpUtil.getQueryString(url);
249    
250                    Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
251                            queryString);
252    
253                    if (parameters.containsKey("image_id") ||
254                            parameters.containsKey("img_id") ||
255                            parameters.containsKey("i_id")) {
256    
257                            try {
258                                    long imageId = 0;
259    
260                                    if (parameters.containsKey("image_id")) {
261                                            imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
262                                    }
263                                    else if (parameters.containsKey("img_id")) {
264                                            imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
265                                    }
266                                    else if (parameters.containsKey("i_id")) {
267                                            imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
268                                    }
269    
270                                    image = ImageLocalServiceUtil.getImage(imageId);
271                            }
272                            catch (Exception e) {
273                                    if (_log.isWarnEnabled()) {
274                                            _log.warn(e, e);
275                                    }
276                            }
277                    }
278                    else if (parameters.containsKey("uuid") &&
279                                     parameters.containsKey("groupId")) {
280    
281                            try {
282                                    String uuid = parameters.get("uuid")[0];
283                                    long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
284    
285                                    IGImage igImage =
286                                            IGImageLocalServiceUtil.getImageByUuidAndGroupId(
287                                                    uuid, groupId);
288    
289                                    image = ImageLocalServiceUtil.getImage(
290                                            igImage.getLargeImageId());
291                            }
292                            catch (Exception e) {
293                                    if (_log.isWarnEnabled()) {
294                                            _log.warn(e, e);
295                                    }
296                            }
297                    }
298    
299                    return image;
300            }
301    
302            private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
303    
304    }