001
014
015 package com.liferay.portlet.trash.service.impl;
016
017 import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018 import com.liferay.portal.kernel.dao.orm.BaseActionableDynamicQuery;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.search.BaseModelSearchResult;
022 import com.liferay.portal.kernel.search.Hits;
023 import com.liferay.portal.kernel.search.Indexable;
024 import com.liferay.portal.kernel.search.IndexableType;
025 import com.liferay.portal.kernel.search.Indexer;
026 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027 import com.liferay.portal.kernel.search.QueryConfig;
028 import com.liferay.portal.kernel.search.SearchContext;
029 import com.liferay.portal.kernel.search.Sort;
030 import com.liferay.portal.kernel.trash.TrashHandler;
031 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
032 import com.liferay.portal.kernel.util.ObjectValuePair;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.UnicodeProperties;
035 import com.liferay.portal.model.Group;
036 import com.liferay.portal.model.SystemEvent;
037 import com.liferay.portal.model.User;
038 import com.liferay.portlet.trash.model.TrashEntry;
039 import com.liferay.portlet.trash.model.TrashVersion;
040 import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
041 import com.liferay.portlet.trash.util.TrashUtil;
042
043 import java.util.Calendar;
044 import java.util.Date;
045 import java.util.List;
046
047
053 public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
054
055
073 @Override
074 public TrashEntry addTrashEntry(
075 long userId, long groupId, String className, long classPK,
076 String classUuid, String referrerClassName, int status,
077 List<ObjectValuePair<Long, Integer>> statusOVPs,
078 UnicodeProperties typeSettingsProperties)
079 throws PortalException {
080
081 User user = userPersistence.findByPrimaryKey(userId);
082 long classNameId = classNameLocalService.getClassNameId(className);
083
084 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
085 className);
086
087 SystemEvent systemEvent = trashHandler.addDeletionSystemEvent(
088 userId, groupId, classPK, classUuid, referrerClassName);
089
090 long entryId = counterLocalService.increment();
091
092 TrashEntry trashEntry = trashEntryPersistence.create(entryId);
093
094 trashEntry.setGroupId(groupId);
095 trashEntry.setCompanyId(user.getCompanyId());
096 trashEntry.setUserId(user.getUserId());
097 trashEntry.setUserName(user.getFullName());
098 trashEntry.setCreateDate(new Date());
099 trashEntry.setClassNameId(classNameId);
100 trashEntry.setClassPK(classPK);
101 trashEntry.setSystemEventSetKey(systemEvent.getSystemEventSetKey());
102
103 if (typeSettingsProperties != null) {
104 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
105 }
106
107 trashEntry.setStatus(status);
108
109 trashEntryPersistence.update(trashEntry);
110
111 if (statusOVPs != null) {
112 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
113 long versionId = counterLocalService.increment();
114
115 TrashVersion trashVersion = trashVersionPersistence.create(
116 versionId);
117
118 trashVersion.setEntryId(entryId);
119 trashVersion.setClassNameId(classNameId);
120 trashVersion.setClassPK(statusOVP.getKey());
121 trashVersion.setStatus(statusOVP.getValue());
122
123 trashVersionPersistence.update(trashVersion);
124 }
125 }
126
127 return trashEntry;
128 }
129
130 @Override
131 public void checkEntries() throws PortalException {
132 ActionableDynamicQuery actionableDynamicQuery =
133 trashEntryLocalService.getActionableDynamicQuery();
134
135 actionableDynamicQuery.setPerformActionMethod(
136 new ActionableDynamicQuery.PerformActionMethod<TrashEntry>() {
137
138 @Override
139 public void performAction(TrashEntry trashEntry)
140 throws PortalException {
141
142 Date createDate = trashEntry.getCreateDate();
143
144 Group group = groupPersistence.fetchByPrimaryKey(
145 trashEntry.getGroupId());
146
147 if (group == null) {
148 return;
149 }
150
151 Date date = getMaxAge(group);
152
153 if (createDate.before(date) ||
154 !TrashUtil.isTrashEnabled(group)) {
155
156 TrashHandler trashHandler =
157 TrashHandlerRegistryUtil.getTrashHandler(
158 trashEntry.getClassName());
159
160 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
161 }
162 }
163
164 });
165 actionableDynamicQuery.setTransactionAttribute(
166 BaseActionableDynamicQuery.REQUIRES_NEW_TRANSACTION_ATTRIBUTE);
167
168 actionableDynamicQuery.performActions();
169 }
170
171 @Override
172 public void deleteEntries(long groupId) {
173 List<TrashEntry> entries = getEntries(groupId);
174
175 for (TrashEntry entry : entries) {
176 deleteEntry(entry);
177 }
178 }
179
180
188 @Override
189 public TrashEntry deleteEntry(long entryId) throws PortalException {
190 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
191
192 return deleteEntry(entry);
193 }
194
195
204 @Override
205 public TrashEntry deleteEntry(String className, long classPK)
206 throws PortalException {
207
208 long classNameId = classNameLocalService.getClassNameId(className);
209
210 TrashEntry entry = trashEntryPersistence.fetchByC_C(
211 classNameId, classPK);
212
213 return deleteEntry(entry);
214 }
215
216 @Indexable(type = IndexableType.DELETE)
217 @Override
218 public TrashEntry deleteEntry(TrashEntry trashEntry) {
219 if (trashEntry != null) {
220 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
221
222 trashEntry = trashEntryPersistence.remove(trashEntry);
223
224 systemEventLocalService.deleteSystemEvents(
225 trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
226 }
227
228 return trashEntry;
229 }
230
231
237 @Override
238 public TrashEntry fetchEntry(long entryId) {
239 return trashEntryPersistence.fetchByPrimaryKey(entryId);
240 }
241
242
249 @Override
250 public TrashEntry fetchEntry(String className, long classPK) {
251 long classNameId = classNameLocalService.getClassNameId(className);
252
253 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
254 }
255
256
262 @Override
263 public List<TrashEntry> getEntries(long groupId) {
264 return trashEntryPersistence.findByGroupId(groupId);
265 }
266
267
276 @Override
277 public List<TrashEntry> getEntries(long groupId, int start, int end) {
278 return trashEntryPersistence.findByGroupId(groupId, start, end);
279 }
280
281
293 @Override
294 public List<TrashEntry> getEntries(
295 long groupId, int start, int end, OrderByComparator<TrashEntry> obc) {
296
297 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
298 }
299
300 @Override
301 public List<TrashEntry> getEntries(long groupId, String className) {
302 long classNameId = classNameLocalService.getClassNameId(className);
303
304 return trashEntryPersistence.findByG_C(groupId, classNameId);
305 }
306
307
313 @Override
314 public int getEntriesCount(long groupId) {
315 return trashEntryPersistence.countByGroupId(groupId);
316 }
317
318
326 @Override
327 public TrashEntry getEntry(long entryId) throws PortalException {
328 return trashEntryPersistence.findByPrimaryKey(entryId);
329 }
330
331
340 @Override
341 public TrashEntry getEntry(String className, long classPK)
342 throws PortalException {
343
344 long classNameId = classNameLocalService.getClassNameId(className);
345
346 return trashEntryPersistence.findByC_C(classNameId, classPK);
347 }
348
349 @Override
350 public Hits search(
351 long companyId, long groupId, long userId, String keywords, int start,
352 int end, Sort sort) {
353
354 try {
355 Indexer<TrashEntry> indexer =
356 IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
357
358 SearchContext searchContext = buildSearchContext(
359 companyId, groupId, userId, keywords, start, end, sort);
360
361 return indexer.search(searchContext);
362 }
363 catch (Exception e) {
364 throw new SystemException(e);
365 }
366 }
367
368 @Override
369 public BaseModelSearchResult<TrashEntry> searchTrashEntries(
370 long companyId, long groupId, long userId, String keywords, int start,
371 int end, Sort sort) {
372
373 try {
374 Indexer<TrashEntry> indexer =
375 IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
376
377 SearchContext searchContext = buildSearchContext(
378 companyId, groupId, userId, keywords, start, end, sort);
379
380 Hits hits = indexer.search(searchContext);
381
382 List<TrashEntry> trashEntries = TrashUtil.getEntries(hits);
383
384 return new BaseModelSearchResult<>(trashEntries, hits.getLength());
385 }
386 catch (Exception e) {
387 throw new SystemException(e);
388 }
389 }
390
391 protected SearchContext buildSearchContext(
392 long companyId, long groupId, long userId, String keywords, int start,
393 int end, Sort sort) {
394
395 SearchContext searchContext = new SearchContext();
396
397 searchContext.setCompanyId(companyId);
398 searchContext.setEnd(end);
399 searchContext.setKeywords(keywords);
400 searchContext.setGroupIds(new long[] {groupId});
401
402 if (sort != null) {
403 searchContext.setSorts(sort);
404 }
405
406 searchContext.setStart(start);
407 searchContext.setUserId(userId);
408
409 QueryConfig queryConfig = searchContext.getQueryConfig();
410
411 queryConfig.setHighlightEnabled(false);
412 queryConfig.setScoreEnabled(false);
413
414 return searchContext;
415 }
416
417 protected Date getMaxAge(Group group) throws PortalException {
418 Calendar calendar = Calendar.getInstance();
419
420 calendar.setTime(new Date());
421
422 int maxAge = TrashUtil.getMaxAge(group);
423
424 calendar.add(Calendar.MINUTE, -maxAge);
425
426 return calendar.getTime();
427 }
428
429 }