001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.util;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.OrderByComparator;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Image;
028: import com.liferay.portal.service.impl.ImageLocalUtil;
029: import com.liferay.portal.util.MimeTypesUtil;
030: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
032: import com.liferay.portlet.imagegallery.model.IGImage;
033: import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
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: import com.liferay.util.HttpUtil;
039:
040: import com.sun.syndication.feed.synd.SyndEnclosure;
041: import com.sun.syndication.feed.synd.SyndEnclosureImpl;
042: import com.sun.syndication.feed.synd.SyndLink;
043: import com.sun.syndication.feed.synd.SyndLinkImpl;
044:
045: import java.util.ArrayList;
046: import java.util.Date;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: /**
054: * <a href="JournalRSSUtil.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Raymond Augé
057: *
058: */
059: public class JournalRSSUtil {
060:
061: public static List getArticles(JournalFeed feed)
062: throws SystemException {
063: long companyId = feed.getCompanyId();
064: long groupId = feed.getGroupId();
065: String articleId = null;
066: Double version = null;
067: String title = null;
068: String description = null;
069: String content = null;
070:
071: String type = feed.getType();
072:
073: if (Validator.isNull(type)) {
074: type = null;
075: }
076:
077: String structureId = feed.getStructureId();
078:
079: if (Validator.isNull(structureId)) {
080: structureId = null;
081: }
082:
083: String templateId = feed.getTemplateId();
084:
085: if (Validator.isNull(templateId)) {
086: templateId = null;
087: }
088:
089: Date displayDateGT = null;
090: Date displayDateLT = new Date();
091: Boolean approved = Boolean.TRUE;
092: Boolean expired = Boolean.FALSE;
093: Date reviewDate = null;
094: boolean andOperator = true;
095: int begin = 0;
096: int end = feed.getDelta();
097:
098: String orderByCol = feed.getOrderByCol();
099: String orderByType = feed.getOrderByType();
100: boolean orderByAsc = orderByType.equals("asc");
101:
102: OrderByComparator obc = new ArticleModifiedDateComparator(
103: orderByAsc);
104:
105: if (orderByCol.equals("display-date")) {
106: obc = new ArticleDisplayDateComparator(orderByAsc);
107: }
108:
109: return JournalArticleLocalServiceUtil.search(companyId,
110: groupId, articleId, version, title, description,
111: content, type, structureId, templateId, displayDateGT,
112: displayDateLT, approved, expired, reviewDate,
113: andOperator, begin, end, obc);
114: }
115:
116: public static List getDLEnclosures(String portalURL, String url) {
117: List enclosures = new ArrayList();
118:
119: DLFileEntry fileEntry = getDLFileEntry(url);
120:
121: if (fileEntry != null) {
122: SyndEnclosure enclosure = new SyndEnclosureImpl();
123:
124: enclosure.setLength(fileEntry.getSize());
125:
126: enclosure.setType(MimeTypesUtil.getContentType(fileEntry
127: .getTitleWithExtension()));
128:
129: enclosure.setUrl(portalURL + url);
130:
131: enclosures.add(enclosure);
132: }
133:
134: return enclosures;
135: }
136:
137: public static DLFileEntry getDLFileEntry(String url) {
138: DLFileEntry fileEntry = null;
139:
140: String queryString = HttpUtil.getQueryString(url);
141:
142: Map parameters = HttpUtil.parameterMapFromString(queryString);
143:
144: if (parameters.containsKey("folderId")
145: && parameters.containsKey("name")) {
146:
147: try {
148: long folderId = GetterUtil
149: .getLong(((String[]) parameters.get("folderId"))[0]);
150: String name = ((String[]) parameters.get("name"))[0];
151:
152: fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
153: folderId, name);
154: } catch (Exception e) {
155: if (_log.isWarnEnabled()) {
156: _log.warn(e, e);
157: }
158: }
159: } else if (parameters.containsKey("uuid")
160: && parameters.containsKey("groupId")) {
161:
162: try {
163: String uuid = ((String[]) parameters.get("uuid"))[0];
164: long groupId = GetterUtil
165: .getLong(((String[]) parameters.get("groupId"))[0]);
166:
167: fileEntry = DLFileEntryLocalServiceUtil
168: .getFileEntryByUuidAndGroupId(uuid, groupId);
169: } catch (Exception e) {
170: if (_log.isWarnEnabled()) {
171: _log.warn(e, e);
172: }
173: }
174: }
175:
176: return fileEntry;
177: }
178:
179: public static List getDLLinks(String portalURL, String url) {
180: List links = new ArrayList();
181:
182: DLFileEntry fileEntry = getDLFileEntry(url);
183:
184: if (fileEntry != null) {
185: SyndLink link = new SyndLinkImpl();
186:
187: link.setHref(portalURL + url);
188:
189: link.setLength(fileEntry.getSize());
190:
191: link.setRel("enclosure");
192:
193: link.setType(MimeTypesUtil.getContentType(fileEntry
194: .getTitleWithExtension()));
195:
196: links.add(link);
197: }
198:
199: return links;
200: }
201:
202: public static List getIGEnclosures(String portalURL, String url) {
203: List enclosures = new ArrayList();
204:
205: Image image = getImage(url);
206:
207: if (image != null) {
208: SyndEnclosure enclosure = new SyndEnclosureImpl();
209:
210: enclosure.setLength(image.getSize());
211:
212: enclosure.setType(MimeTypesUtil.getContentType("*."
213: + image.getType()));
214:
215: enclosure.setUrl(portalURL + url);
216:
217: enclosures.add(enclosure);
218: }
219:
220: return enclosures;
221: }
222:
223: public static List getIGLinks(String portalURL, String url) {
224: List links = new ArrayList();
225:
226: Image image = getImage(url);
227:
228: if (image != null) {
229: SyndLink link = new SyndLinkImpl();
230:
231: link.setHref(portalURL + url);
232:
233: link.setLength(image.getSize());
234:
235: link.setRel("enclosure");
236:
237: link.setType(MimeTypesUtil.getContentType("*."
238: + image.getType()));
239:
240: links.add(link);
241: }
242:
243: return links;
244: }
245:
246: public static Image getImage(String url) {
247: Image image = null;
248:
249: String queryString = HttpUtil.getQueryString(url);
250:
251: Map parameters = HttpUtil.parameterMapFromString(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(((String[]) parameters
262: .get("image_id"))[0]);
263: } else if (parameters.containsKey("img_id")) {
264: imageId = GetterUtil.getLong(((String[]) parameters
265: .get("img_id"))[0]);
266: } else if (parameters.containsKey("i_id")) {
267: imageId = GetterUtil.getLong(((String[]) parameters
268: .get("i_id"))[0]);
269: }
270:
271: image = ImageLocalUtil.getImage(imageId);
272: } catch (Exception e) {
273: if (_log.isWarnEnabled()) {
274: _log.warn(e, e);
275: }
276: }
277: } else if (parameters.containsKey("uuid")
278: && parameters.containsKey("groupId")) {
279:
280: try {
281: String uuid = ((String[]) parameters.get("uuid"))[0];
282: long groupId = GetterUtil
283: .getLong(((String[]) parameters.get("groupId"))[0]);
284:
285: IGImage igImage = IGImageLocalServiceUtil
286: .getImageByUuidAndGroupId(uuid, groupId);
287:
288: image = ImageLocalUtil.getImage(igImage
289: .getLargeImageId());
290: } catch (Exception e) {
291: if (_log.isWarnEnabled()) {
292: _log.warn(e, e);
293: }
294: }
295: }
296:
297: return image;
298: }
299:
300: private static Log _log = LogFactory.getLog(JournalRSSUtil.class);
301:
302: }
|