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.asset.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
027    import com.liferay.portlet.asset.model.AssetEntry;
028    import com.liferay.portlet.asset.model.AssetEntryDisplay;
029    import com.liferay.portlet.asset.model.AssetRendererFactory;
030    import com.liferay.portlet.asset.service.base.AssetEntryServiceBaseImpl;
031    import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
032    import com.liferay.portlet.asset.service.permission.AssetTagPermission;
033    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
034    import com.liferay.util.RSSUtil;
035    
036    import com.sun.syndication.feed.synd.SyndContent;
037    import com.sun.syndication.feed.synd.SyndContentImpl;
038    import com.sun.syndication.feed.synd.SyndEntry;
039    import com.sun.syndication.feed.synd.SyndEntryImpl;
040    import com.sun.syndication.feed.synd.SyndFeed;
041    import com.sun.syndication.feed.synd.SyndFeedImpl;
042    import com.sun.syndication.io.FeedException;
043    
044    import java.util.ArrayList;
045    import java.util.Date;
046    import java.util.List;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Jorge Ferrer
051     * @author Bruno Farache
052     */
053    public class AssetEntryServiceImpl extends AssetEntryServiceBaseImpl {
054    
055            public void deleteEntry(long entryId)
056                    throws PortalException, SystemException {
057    
058                    assetEntryLocalService.deleteEntry(entryId);
059            }
060    
061            public List<AssetEntry> getCompanyEntries(
062                            long companyId, int start, int end)
063                    throws SystemException {
064    
065                    return assetEntryLocalService.getCompanyEntries(companyId, start, end);
066            }
067    
068            public int getCompanyEntriesCount(long companyId) throws SystemException {
069                    return assetEntryLocalService.getCompanyEntriesCount(companyId);
070            }
071    
072            public String getCompanyEntriesRSS(
073                            long companyId, int max, String type, double version,
074                            String displayStyle, String feedURL, String tagURL)
075                    throws PortalException, SystemException {
076    
077                    Company company = companyPersistence.findByPrimaryKey(companyId);
078    
079                    String name = company.getName();
080    
081                    List<AssetEntry> entries = getCompanyEntries(companyId, 0, max);
082    
083                    return exportToRSS(
084                            name, null, type, version, displayStyle, feedURL, tagURL, entries);
085            }
086    
087            public AssetEntryDisplay[] getCompanyEntryDisplays(
088                            long companyId, int start, int end, String languageId)
089                    throws SystemException {
090    
091                    return assetEntryLocalService.getCompanyEntryDisplays(
092                            companyId, start, end, languageId);
093            }
094    
095            public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
096                    throws PortalException, SystemException {
097    
098                    filterQuery(entryQuery);
099    
100                    return assetEntryLocalService.getEntries(entryQuery);
101            }
102    
103            public int getEntriesCount(AssetEntryQuery entryQuery)
104                    throws PortalException, SystemException {
105    
106                    filterQuery(entryQuery);
107    
108                    return assetEntryLocalService.getEntriesCount(entryQuery);
109            }
110    
111            public String getEntriesRSS(
112                            AssetEntryQuery entryQuery, String name, String type,
113                            double version, String displayStyle, String feedURL, String tagURL)
114                    throws PortalException, SystemException {
115    
116                    filterQuery(entryQuery);
117    
118                    List<AssetEntry> entries = assetEntryLocalService.getEntries(
119                            entryQuery);
120    
121                    return exportToRSS(
122                            name, null, type, version, displayStyle, feedURL, tagURL, entries);
123            }
124    
125            public AssetEntry getEntry(long entryId)
126                    throws PortalException, SystemException {
127    
128                    return assetEntryLocalService.getEntry(entryId);
129            }
130    
131            public void incrementViewCounter(String className, long classPK)
132                    throws PortalException, SystemException {
133    
134                    assetEntryLocalService.incrementViewCounter(
135                            getGuestOrUserId(), className, classPK);
136            }
137    
138            public AssetEntryDisplay[] searchEntryDisplays(
139                            long companyId, String portletId, String keywords,
140                            String languageId, int start, int end)
141                    throws SystemException {
142    
143                    return assetEntryLocalService.searchEntryDisplays(
144                            companyId, portletId, keywords, languageId, start, end);
145            }
146    
147            public int searchEntryDisplaysCount(
148                            long companyId, String portletId, String keywords,
149                            String languageId)
150                    throws SystemException {
151    
152                    return assetEntryLocalService.searchEntryDisplaysCount(
153                            companyId, portletId, keywords, languageId);
154            }
155    
156            public AssetEntry updateEntry(
157                            long groupId, String className, long classPK, String classUuid,
158                            long[] categoryIds, String[] tagNames, boolean visible,
159                            Date startDate, Date endDate, Date publishDate, Date expirationDate,
160                            String mimeType, String title, String description, String summary,
161                            String url, int height, int width, Integer priority, boolean sync)
162                    throws PortalException, SystemException {
163    
164                    return assetEntryLocalService.updateEntry(
165                            getUserId(), groupId, className, classPK, classUuid, categoryIds,
166                            tagNames, visible, startDate, endDate, publishDate, expirationDate,
167                            mimeType, title, description, summary, url, height, width, priority,
168                            sync);
169            }
170    
171            protected String exportToRSS(
172                            String name, String description, String type, double version,
173                            String displayStyle, String feedURL, String tagURL,
174                            List<AssetEntry> assetEntries)
175                    throws SystemException {
176    
177                    SyndFeed syndFeed = new SyndFeedImpl();
178    
179                    syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
180                    syndFeed.setTitle(name);
181                    syndFeed.setLink(feedURL);
182                    syndFeed.setDescription(GetterUtil.getString(description, name));
183    
184                    List<SyndEntry> entries = new ArrayList<SyndEntry>();
185    
186                    syndFeed.setEntries(entries);
187    
188                    for (AssetEntry entry : assetEntries) {
189                            AssetRendererFactory assetRendererFactory =
190                                    AssetRendererFactoryRegistryUtil.
191                                            getAssetRendererFactoryByClassName(entry.getClassName());
192    
193                            String author = HtmlUtil.escape(
194                                    PortalUtil.getUserName(entry.getUserId(), entry.getUserName()));
195    
196                            StringBundler sb = new StringBundler(4);
197    
198                            sb.append(tagURL);
199                            sb.append(assetRendererFactory.getType());
200                            sb.append("/id/");
201                            sb.append(entry.getEntryId());
202    
203                            String link = sb.toString();
204    
205                            String value = entry.getSummary();
206    
207                            SyndEntry syndEntry = new SyndEntryImpl();
208    
209                            syndEntry.setAuthor(author);
210                            syndEntry.setTitle(entry.getTitle());
211                            syndEntry.setLink(link);
212                            syndEntry.setUri(syndEntry.getLink());
213                            syndEntry.setPublishedDate(entry.getCreateDate());
214                            syndEntry.setUpdatedDate(entry.getModifiedDate());
215    
216                            SyndContent syndContent = new SyndContentImpl();
217    
218                            syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
219                            syndContent.setValue(value);
220    
221                            syndEntry.setDescription(syndContent);
222    
223                            entries.add(syndEntry);
224                    }
225    
226                    try {
227                            return RSSUtil.export(syndFeed);
228                    }
229                    catch (FeedException fe) {
230                            throw new SystemException(fe);
231                    }
232            }
233    
234            protected long[] filterCategoryIds(long[] categoryIds)
235                    throws PortalException, SystemException {
236    
237                    List<Long> viewableCategoryIds = new ArrayList<Long>();
238    
239                    for (long categoryId : categoryIds) {
240                            if (AssetCategoryPermission.contains(
241                                            getPermissionChecker(), categoryId, ActionKeys.VIEW)) {
242    
243                                    viewableCategoryIds.add(categoryId);
244                            }
245                    }
246    
247                    return ArrayUtil.toArray(
248                            viewableCategoryIds.toArray(new Long[viewableCategoryIds.size()]));
249            }
250    
251            protected void filterQuery(AssetEntryQuery entryQuery)
252                    throws PortalException, SystemException {
253    
254                    entryQuery.setAllCategoryIds(filterCategoryIds(
255                            entryQuery.getAllCategoryIds()));
256                    entryQuery.setAnyCategoryIds(filterCategoryIds(
257                            entryQuery.getAnyCategoryIds()));
258    
259                    entryQuery.setAllTagIds(filterTagIds(entryQuery.getAllTagIds()));
260                    entryQuery.setAnyTagIds(filterTagIds(entryQuery.getAnyTagIds()));
261            }
262    
263            protected long[] filterTagIds(long[] tagIds)
264                    throws PortalException, SystemException {
265    
266                    List<Long> viewableTagIds = new ArrayList<Long>();
267    
268                    for (long tagId : tagIds) {
269                            if (AssetTagPermission.contains(
270                                            getPermissionChecker(), tagId, ActionKeys.VIEW)) {
271    
272                                    viewableTagIds.add(tagId);
273                            }
274                    }
275    
276                    return ArrayUtil.toArray(
277                            viewableTagIds.toArray(new Long[viewableTagIds.size()]));
278            }
279    
280    }