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 entryQuery.setEnd(end + PropsValues.ASSET_FILTER_SEARCH_LIMIT);
215 entryQuery.setStart(0);
216
217 List<AssetEntry> entries = assetEntryLocalService.getEntries(
218 entryQuery);
219
220 PermissionChecker permissionChecker = getPermissionChecker();
221
222 List<AssetEntry> filteredEntries = new ArrayList<AssetEntry>();
223
224 for (AssetEntry entry : entries) {
225 String className = entry.getClassName();
226 long classPK = entry.getClassPK();
227
228 AssetRendererFactory assetRendererFactory =
229 AssetRendererFactoryRegistryUtil.
230 getAssetRendererFactoryByClassName(className);
231
232 try {
233 if (assetRendererFactory.hasPermission(
234 permissionChecker, classPK, ActionKeys.VIEW)) {
235
236 filteredEntries.add(entry);
237 }
238 }
239 catch (Exception e) {
240 }
241 }
242
243 int length = filteredEntries.size();
244
245 if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
246 if (end > length) {
247 end = length;
248 }
249
250 if (start > length) {
251 start = length;
252 }
253
254 filteredEntries = filteredEntries.subList(start, end);
255 }
256
257 entryQuery.setEnd(end);
258 entryQuery.setStart(start);
259
260 results = new Object[] {filteredEntries, length};
261
262 threadLocalCache.put(key, results);
263
264 return results;
265 }
266
267 protected boolean isRemovedFilters (
268 AssetEntryQuery entryQuery, AssetEntryQuery filteredEntryQuery) {
269
270 if (((entryQuery.getAllCategoryIds().length > 0) &&
271 (filteredEntryQuery.getAllCategoryIds().length == 0)) ||
272 ((entryQuery.getAllTagIds().length > 0) &&
273 (filteredEntryQuery.getAllTagIds().length == 0)) ||
274 ((entryQuery.getAnyCategoryIds().length > 0) &&
275 (filteredEntryQuery.getAnyCategoryIds().length == 0)) ||
276 ((entryQuery.getAnyTagIds().length > 0) &&
277 (filteredEntryQuery.getAnyTagIds().length == 0))) {
278
279 return true;
280 }
281 else {
282 return false;
283 }
284 }
285
286 protected AssetEntryQuery setupQuery(AssetEntryQuery entryQuery)
287 throws PortalException, SystemException {
288
289 AssetEntryQuery filteredEntryQuery = new AssetEntryQuery(entryQuery);
290
291 filteredEntryQuery.setAllCategoryIds(
292 filterCategoryIds(entryQuery.getAllCategoryIds()));
293 filteredEntryQuery.setAllTagIds(
294 filterTagIds(entryQuery.getAllTagIds()));
295 filteredEntryQuery.setAnyCategoryIds(
296 filterCategoryIds(entryQuery.getAnyCategoryIds()));
297 filteredEntryQuery.setAnyTagIds(
298 filterTagIds(entryQuery.getAnyTagIds()));
299
300 return filteredEntryQuery;
301 }
302
303 }