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