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