001    /**
002     * Copyright (c) 2000-2012 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.repository.model.FileEntry;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.model.Image;
030    import com.liferay.portal.service.ImageLocalServiceUtil;
031    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
033    import com.liferay.portlet.journal.model.JournalArticle;
034    import com.liferay.portlet.journal.model.JournalFeed;
035    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
036    import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
037    import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
038    
039    import com.sun.syndication.feed.synd.SyndEnclosure;
040    import com.sun.syndication.feed.synd.SyndEnclosureImpl;
041    import com.sun.syndication.feed.synd.SyndLink;
042    import com.sun.syndication.feed.synd.SyndLinkImpl;
043    
044    import java.util.ArrayList;
045    import java.util.Date;
046    import java.util.List;
047    import java.util.Map;
048    import java.util.Set;
049    
050    /**
051     * @author Raymond Augé
052     */
053    public class JournalRSSUtil {
054    
055            public static List<JournalArticle> getArticles(JournalFeed feed)
056                    throws SystemException {
057    
058                    long companyId = feed.getCompanyId();
059                    long groupId = feed.getGroupId();
060                    String articleId = null;
061                    Double version = null;
062                    String title = null;
063                    String description = null;
064                    String content = null;
065    
066                    String type = feed.getType();
067    
068                    if (Validator.isNull(type)) {
069                            type = null;
070                    }
071    
072                    String structureId = feed.getStructureId();
073    
074                    if (Validator.isNull(structureId)) {
075                            structureId = null;
076                    }
077    
078                    String templateId = feed.getTemplateId();
079    
080                    if (Validator.isNull(templateId)) {
081                            templateId = null;
082                    }
083    
084                    Date displayDateGT = null;
085                    Date displayDateLT = new Date();
086                    int status = WorkflowConstants.STATUS_APPROVED;
087                    Date reviewDate = null;
088                    boolean andOperator = true;
089                    int start = 0;
090                    int end = feed.getDelta();
091    
092                    String orderByCol = feed.getOrderByCol();
093                    String orderByType = feed.getOrderByType();
094                    boolean orderByAsc = orderByType.equals("asc");
095    
096                    OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
097    
098                    if (orderByCol.equals("display-date")) {
099                            obc = new ArticleDisplayDateComparator(orderByAsc);
100                    }
101    
102                    return JournalArticleLocalServiceUtil.search(
103                            companyId, groupId, 0, articleId, version, title, description,
104                            content, type, structureId, templateId, displayDateGT,
105                            displayDateLT, status, reviewDate, andOperator, start, end, obc);
106            }
107    
108            public static List<SyndEnclosure> getDLEnclosures(
109                    String portalURL, String url) {
110    
111                    List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
112    
113                    FileEntry fileEntry = getFileEntry(url);
114    
115                    if (fileEntry == null) {
116                            return syndEnclosures;
117                    }
118    
119                    SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
120    
121                    syndEnclosure.setLength(fileEntry.getSize());
122                    syndEnclosure.setType(fileEntry.getMimeType());
123                    syndEnclosure.setUrl(portalURL + url);
124    
125                    syndEnclosures.add(syndEnclosure);
126    
127                    return syndEnclosures;
128            }
129    
130            public static List<SyndLink> getDLLinks(String portalURL, String url) {
131                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
132    
133                    FileEntry fileEntry = getFileEntry(url);
134    
135                    if (fileEntry == null) {
136                            return syndLinks;
137                    }
138    
139                    SyndLink syndLink = new SyndLinkImpl();
140    
141                    syndLink.setHref(portalURL + url);
142                    syndLink.setLength(fileEntry.getSize());
143                    syndLink.setRel("enclosure");
144                    syndLink.setType(fileEntry.getMimeType());
145    
146                    syndLinks.add(syndLink);
147    
148                    return syndLinks;
149            }
150    
151            public static FileEntry getFileEntry(String url) {
152                    FileEntry fileEntry = null;
153    
154                    String queryString = HttpUtil.getQueryString(url);
155    
156                    Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
157                            queryString);
158    
159                    if (url.startsWith("/documents/")) {
160                            String[] pathArray = StringUtil.split(url, CharPool.SLASH);
161    
162                            long groupId = GetterUtil.getLong(pathArray[2]);
163                            long folderId = GetterUtil.getLong(pathArray[3]);
164                            String title = HttpUtil.decodeURL(pathArray[4], true);
165    
166                            try {
167                                    fileEntry = DLAppLocalServiceUtil.getFileEntry(
168                                            groupId, folderId, title);
169                            }
170                            catch (Exception e) {
171                                    if (_log.isWarnEnabled()) {
172                                            _log.warn(e, e);
173                                    }
174                            }
175                    }
176                    else if (parameters.containsKey("folderId") &&
177                                     parameters.containsKey("name")) {
178    
179                            try {
180                                    long fileEntryId = GetterUtil.getLong(
181                                            parameters.get("fileEntryId")[0]);
182    
183                                    fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
184                            }
185                            catch (Exception e) {
186                                    if (_log.isWarnEnabled()) {
187                                            _log.warn(e, e);
188                                    }
189                            }
190                    }
191                    else if (parameters.containsKey("uuid") &&
192                                     parameters.containsKey("groupId")) {
193    
194                            try {
195                                    String uuid = parameters.get("uuid")[0];
196                                    long groupId = GetterUtil.getLong(parameters.get("groupId")[0]);
197    
198                                    fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
199                                            uuid, groupId);
200                            }
201                            catch (Exception e) {
202                                    if (_log.isWarnEnabled()) {
203                                            _log.warn(e, e);
204                                    }
205                            }
206                    }
207    
208                    return fileEntry;
209            }
210    
211            public static List<SyndEnclosure> getIGEnclosures(
212                    String portalURL, String url) {
213    
214                    List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
215    
216                    Object[] imageProperties = getImageProperties(url);
217    
218                    if (imageProperties == null) {
219                            return syndEnclosures;
220                    }
221    
222                    SyndEnclosure syndEnclosure = new SyndEnclosureImpl();
223    
224                    syndEnclosure.setLength((Long)imageProperties[1]);
225                    syndEnclosure.setType(
226                            MimeTypesUtil.getContentType("*." + imageProperties[0]));
227                    syndEnclosure.setUrl(portalURL + url);
228    
229                    syndEnclosures.add(syndEnclosure);
230    
231                    return syndEnclosures;
232            }
233    
234            public static List<SyndLink> getIGLinks(String portalURL, String url) {
235                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
236    
237                    Object[] imageProperties = getImageProperties(url);
238    
239                    if (imageProperties == null) {
240                            return syndLinks;
241                    }
242    
243                    SyndLink syndLink = new SyndLinkImpl();
244    
245                    syndLink.setHref(portalURL + url);
246                    syndLink.setLength((Long)imageProperties[1]);
247                    syndLink.setRel("enclosure");
248                    syndLink.setType(
249                            MimeTypesUtil.getContentType("*." + imageProperties[0]));
250    
251                    syndLinks.add(syndLink);
252    
253                    return syndLinks;
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    
290                    return image;
291            }
292    
293            protected static Object[] getImageProperties(String url) {
294                    String type = null;
295                    long size = 0;
296    
297                    Image image = getImage(url);
298    
299                    if (image != null) {
300                            type = image.getType();
301                            size = image.getSize();
302                    }
303                    else {
304                            FileEntry fileEntry = getFileEntry(url);
305    
306                            Set<String> imageMimeTypes = ImageProcessorUtil.getImageMimeTypes();
307    
308                            if ((fileEntry != null) &&
309                                    imageMimeTypes.contains(fileEntry.getMimeType())) {
310    
311                                    type = fileEntry.getExtension();
312                                    size = fileEntry.getSize();
313                            }
314                    }
315    
316                    if (Validator.isNotNull(type)) {
317                            return new Object[] {type, size};
318                    }
319    
320                    return null;
321            }
322    
323            private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
324    
325    }