001    /**
002     * Copyright (c) 2000-2013 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.getExtensionContentType(
252                                    imageProperties[0].toString()));
253                    syndEnclosure.setUrl(portalURL + url);
254    
255                    syndEnclosures.add(syndEnclosure);
256    
257                    return syndEnclosures;
258            }
259    
260            public static List<SyndLink> getIGLinks(String portalURL, String url) {
261                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
262    
263                    Object[] imageProperties = getImageProperties(url);
264    
265                    if (imageProperties == null) {
266                            return syndLinks;
267                    }
268    
269                    SyndLink syndLink = new SyndLinkImpl();
270    
271                    syndLink.setHref(portalURL + url);
272                    syndLink.setLength((Long)imageProperties[1]);
273                    syndLink.setRel("enclosure");
274                    syndLink.setType(
275                            MimeTypesUtil.getExtensionContentType(
276                                    imageProperties[0].toString()));
277    
278                    syndLinks.add(syndLink);
279    
280                    return syndLinks;
281            }
282    
283            public static Image getImage(String url) {
284                    Image image = null;
285    
286                    String queryString = HttpUtil.getQueryString(url);
287    
288                    Map<String, String[]> parameters = HttpUtil.parameterMapFromString(
289                            queryString);
290    
291                    if (parameters.containsKey("image_id") ||
292                            parameters.containsKey("img_id") ||
293                            parameters.containsKey("i_id")) {
294    
295                            try {
296                                    long imageId = 0;
297    
298                                    if (parameters.containsKey("image_id")) {
299                                            imageId = GetterUtil.getLong(parameters.get("image_id")[0]);
300                                    }
301                                    else if (parameters.containsKey("img_id")) {
302                                            imageId = GetterUtil.getLong(parameters.get("img_id")[0]);
303                                    }
304                                    else if (parameters.containsKey("i_id")) {
305                                            imageId = GetterUtil.getLong(parameters.get("i_id")[0]);
306                                    }
307    
308                                    image = ImageLocalServiceUtil.getImage(imageId);
309                            }
310                            catch (Exception e) {
311                                    if (_log.isWarnEnabled()) {
312                                            _log.warn(e, e);
313                                    }
314                            }
315                    }
316    
317                    return image;
318            }
319    
320            protected static Object[] getImageProperties(String url) {
321                    String type = null;
322                    long size = 0;
323    
324                    Image image = getImage(url);
325    
326                    if (image != null) {
327                            type = image.getType();
328                            size = image.getSize();
329                    }
330                    else {
331                            FileEntry fileEntry = getFileEntry(url);
332    
333                            Set<String> imageMimeTypes = ImageProcessorUtil.getImageMimeTypes();
334    
335                            if ((fileEntry != null) &&
336                                    imageMimeTypes.contains(fileEntry.getMimeType())) {
337    
338                                    type = fileEntry.getExtension();
339                                    size = fileEntry.getSize();
340                            }
341                    }
342    
343                    if (Validator.isNotNull(type)) {
344                            return new Object[] {type, size};
345                    }
346    
347                    return null;
348            }
349    
350            private static Log _log = LogFactoryUtil.getLog(JournalRSSUtil.class);
351    
352    }