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