001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.cache.Lifecycle;
018 import com.liferay.portal.kernel.cache.ThreadLocalCache;
019 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
020 import com.liferay.portal.kernel.dao.orm.QueryUtil;
021 import com.liferay.portal.kernel.exception.PortalException;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.util.ArrayUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.security.permission.ActionKeys;
027 import com.liferay.portal.security.permission.PermissionChecker;
028 import com.liferay.portal.util.PropsValues;
029 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
030 import com.liferay.portlet.asset.model.AssetEntry;
031 import com.liferay.portlet.asset.model.AssetEntryDisplay;
032 import com.liferay.portlet.asset.model.AssetRendererFactory;
033 import com.liferay.portlet.asset.service.base.AssetEntryServiceBaseImpl;
034 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
035 import com.liferay.portlet.asset.service.permission.AssetTagPermission;
036 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
037 import com.liferay.portlet.social.model.SocialActivityConstants;
038
039 import java.util.ArrayList;
040 import java.util.Date;
041 import java.util.List;
042
043
049 public class AssetEntryServiceImpl extends AssetEntryServiceBaseImpl {
050
051 public List<AssetEntry> getCompanyEntries(
052 long companyId, int start, int end)
053 throws SystemException {
054
055 return assetEntryLocalService.getCompanyEntries(companyId, start, end);
056 }
057
058 public int getCompanyEntriesCount(long companyId) throws SystemException {
059 return assetEntryLocalService.getCompanyEntriesCount(companyId);
060 }
061
062 public AssetEntryDisplay[] getCompanyEntryDisplays(
063 long companyId, int start, int end, String languageId)
064 throws SystemException {
065
066 return assetEntryLocalService.getCompanyEntryDisplays(
067 companyId, start, end, languageId);
068 }
069
070 public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
071 throws PortalException, SystemException {
072
073 AssetEntryQuery filteredEntryQuery = setupQuery(entryQuery);
074
075 if (isRemovedFilters(entryQuery, filteredEntryQuery)) {
076 return new ArrayList<AssetEntry>();
077 }
078
079 Object[] results = filterQuery(entryQuery);
080
081 return (List<AssetEntry>)results[0];
082 }
083
084 public int getEntriesCount(AssetEntryQuery entryQuery)
085 throws PortalException, SystemException {
086
087 AssetEntryQuery filteredEntryQuery = setupQuery(entryQuery);
088
089 if (isRemovedFilters(entryQuery, filteredEntryQuery)) {
090 return 0;
091 }
092
093 Object[] results = filterQuery(entryQuery);
094
095 return (Integer)results[1];
096 }
097
098 public AssetEntry getEntry(long entryId)
099 throws PortalException, SystemException {
100
101 return assetEntryLocalService.getEntry(entryId);
102 }
103
104 public AssetEntry incrementViewCounter(String className, long classPK)
105 throws PortalException, SystemException {
106
107 if (!PropsValues.ASSET_ENTRY_INCREMENT_VIEW_COUNTER_ENABLED) {
108 return null;
109 }
110
111 User user = getGuestOrUser();
112
113 assetEntryLocalService.incrementViewCounter(
114 user.getUserId(), className, classPK, 1);
115
116 AssetEntry assetEntry = assetEntryLocalService.getEntry(
117 className, classPK);
118
119 if (!user.isDefaultUser()) {
120 socialActivityLocalService.addActivity(
121 user.getUserId(), assetEntry.getGroupId(), className, classPK,
122 SocialActivityConstants.TYPE_VIEW, StringPool.BLANK, 0);
123 }
124
125 return assetEntry;
126 }
127
128 public AssetEntryDisplay[] searchEntryDisplays(
129 long companyId, long[] groupIds, String className, String keywords,
130 String languageId, int start, int end)
131 throws SystemException {
132
133 return assetEntryLocalService.searchEntryDisplays(
134 companyId, groupIds, className, keywords, languageId, start, end);
135 }
136
137 public int searchEntryDisplaysCount(
138 long companyId, long[] groupIds, String className, String keywords,
139 String languageId)
140 throws SystemException {
141
142 return assetEntryLocalService.searchEntryDisplaysCount(
143 companyId, groupIds, className, keywords, languageId);
144 }
145
146 public AssetEntry updateEntry(
147 long groupId, String className, long classPK, String classUuid,
148 long classTypeId, long[] categoryIds, String[] tagNames,
149 boolean visible, Date startDate, Date endDate, Date publishDate,
150 Date expirationDate, String mimeType, String title,
151 String description, String summary, String url, String layoutUuid,
152 int height, int width, Integer priority, boolean sync)
153 throws PortalException, SystemException {
154
155 return assetEntryLocalService.updateEntry(
156 getUserId(), groupId, className, classPK, classUuid, classTypeId,
157 categoryIds, tagNames, visible, startDate, endDate, publishDate,
158 expirationDate, mimeType, title, description, summary, url,
159 layoutUuid, height, width, priority, sync);
160 }
161
162 protected long[] filterCategoryIds(long[] categoryIds)
163 throws PortalException, SystemException {
164
165 List<Long> viewableCategoryIds = new ArrayList<Long>();
166
167 for (long categoryId : categoryIds) {
168 if (AssetCategoryPermission.contains(
169 getPermissionChecker(), categoryId, ActionKeys.VIEW)) {
170
171 viewableCategoryIds.add(categoryId);
172 }
173 }
174
175 return ArrayUtil.toArray(
176 viewableCategoryIds.toArray(new Long[viewableCategoryIds.size()]));
177 }
178
179 protected long[] filterTagIds(long[] tagIds)
180 throws PortalException, SystemException {
181
182 List<Long> viewableTagIds = new ArrayList<Long>();
183
184 for (long tagId : tagIds) {
185 if (AssetTagPermission.contains(
186 getPermissionChecker(), tagId, ActionKeys.VIEW)) {
187
188 viewableTagIds.add(tagId);
189 }
190 }
191
192 return ArrayUtil.toArray(
193 viewableTagIds.toArray(new Long[viewableTagIds.size()]));
194 }
195
196 protected Object[] filterQuery(AssetEntryQuery entryQuery)
197 throws PortalException, SystemException {
198
199 ThreadLocalCache<Object[]> threadLocalCache =
200 ThreadLocalCacheManager.getThreadLocalCache(
201 Lifecycle.REQUEST, AssetEntryServiceImpl.class.getName());
202
203 String key = entryQuery.toString();
204
205 Object[] results = threadLocalCache.get(key);
206
207 if (results != null) {
208 return results;
209 }
210
211 int end = entryQuery.getEnd();
212 int start = entryQuery.getStart();
213
214 if (entryQuery.isEnablePermissions()) {
215 entryQuery.setEnd(end + PropsValues.ASSET_FILTER_SEARCH_LIMIT);
216 entryQuery.setStart(0);
217 }
218
219 List<AssetEntry> entries = assetEntryLocalService.getEntries(
220 entryQuery);
221
222 List<AssetEntry> filteredEntries = null;
223 int filteredEntriesCount = 0;
224
225 if (entryQuery.isEnablePermissions()) {
226 PermissionChecker permissionChecker = getPermissionChecker();
227
228 filteredEntries = new ArrayList<AssetEntry>();
229
230 for (AssetEntry entry : entries) {
231 String className = entry.getClassName();
232 long classPK = entry.getClassPK();
233
234 AssetRendererFactory assetRendererFactory =
235 AssetRendererFactoryRegistryUtil.
236 getAssetRendererFactoryByClassName(className);
237
238 try {
239 if (assetRendererFactory.hasPermission(
240 permissionChecker, classPK, ActionKeys.VIEW)) {
241
242 filteredEntries.add(entry);
243 }
244 }
245 catch (Exception e) {
246 }
247
248 if (filteredEntries.size() > end) {
249 break;
250 }
251 }
252
253 filteredEntriesCount = filteredEntries.size();
254
255 if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
256 if (end > filteredEntriesCount) {
257 end = filteredEntriesCount;
258 }
259
260 if (start > filteredEntriesCount) {
261 start = filteredEntriesCount;
262 }
263
264 filteredEntries = filteredEntries.subList(start, end);
265 }
266
267 entryQuery.setEnd(end);
268 entryQuery.setStart(start);
269 }
270 else {
271 filteredEntries = entries;
272 filteredEntriesCount = entries.size();
273 }
274
275 results = new Object[] {filteredEntries, filteredEntriesCount};
276
277 threadLocalCache.put(key, results);
278
279 return results;
280 }
281
282 protected boolean isRemovedFilters(
283 AssetEntryQuery entryQuery, AssetEntryQuery filteredEntryQuery) {
284
285 if (((entryQuery.getAllCategoryIds().length > 0) &&
286 (filteredEntryQuery.getAllCategoryIds().length == 0)) ||
287 ((entryQuery.getAllTagIds().length > 0) &&
288 (filteredEntryQuery.getAllTagIds().length == 0)) ||
289 ((entryQuery.getAnyCategoryIds().length > 0) &&
290 (filteredEntryQuery.getAnyCategoryIds().length == 0)) ||
291 ((entryQuery.getAnyTagIds().length > 0) &&
292 (filteredEntryQuery.getAnyTagIds().length == 0))) {
293
294 return true;
295 }
296 else {
297 return false;
298 }
299 }
300
301 protected AssetEntryQuery setupQuery(AssetEntryQuery entryQuery)
302 throws PortalException, SystemException {
303
304 AssetEntryQuery filteredEntryQuery = new AssetEntryQuery(entryQuery);
305
306 filteredEntryQuery.setAllCategoryIds(
307 filterCategoryIds(entryQuery.getAllCategoryIds()));
308 filteredEntryQuery.setAllTagIds(
309 filterTagIds(entryQuery.getAllTagIds()));
310 filteredEntryQuery.setAnyCategoryIds(
311 filterCategoryIds(entryQuery.getAnyCategoryIds()));
312 filteredEntryQuery.setAnyTagIds(
313 filterTagIds(entryQuery.getAnyTagIds()));
314
315 return filteredEntryQuery;
316 }
317
318 }