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() {
137
138 @Override
139 public void performAction(Object object)
140 throws PortalException {
141
142 TrashEntry trashEntry = (TrashEntry)object;
143
144 Date createDate = trashEntry.getCreateDate();
145
146 Group group = groupPersistence.fetchByPrimaryKey(
147 trashEntry.getGroupId());
148
149 if (group == null) {
150 return;
151 }
152
153 Date date = getMaxAge(group);
154
155 if (createDate.before(date) ||
156 !TrashUtil.isTrashEnabled(group)) {
157
158 TrashHandler trashHandler =
159 TrashHandlerRegistryUtil.getTrashHandler(
160 trashEntry.getClassName());
161
162 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
163 }
164 }
165
166 });
167 actionableDynamicQuery.setTransactionAttribute(
168 BaseActionableDynamicQuery.REQUIRES_NEW_TRANSACTION_ATTRIBUTE);
169
170 actionableDynamicQuery.performActions();
171 }
172
173 @Override
174 public void deleteEntries(long groupId) {
175 List<TrashEntry> entries = getEntries(groupId);
176
177 for (TrashEntry entry : entries) {
178 deleteEntry(entry);
179 }
180 }
181
182
190 @Override
191 public TrashEntry deleteEntry(long entryId) throws PortalException {
192 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
193
194 return deleteEntry(entry);
195 }
196
197
206 @Override
207 public TrashEntry deleteEntry(String className, long classPK)
208 throws PortalException {
209
210 long classNameId = classNameLocalService.getClassNameId(className);
211
212 TrashEntry entry = trashEntryPersistence.fetchByC_C(
213 classNameId, classPK);
214
215 return deleteEntry(entry);
216 }
217
218 @Indexable(type = IndexableType.DELETE)
219 @Override
220 public TrashEntry deleteEntry(TrashEntry trashEntry) {
221 if (trashEntry != null) {
222 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
223
224 trashEntry = trashEntryPersistence.remove(trashEntry);
225
226 systemEventLocalService.deleteSystemEvents(
227 trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
228 }
229
230 return trashEntry;
231 }
232
233
239 @Override
240 public TrashEntry fetchEntry(long entryId) {
241 return trashEntryPersistence.fetchByPrimaryKey(entryId);
242 }
243
244
251 @Override
252 public TrashEntry fetchEntry(String className, long classPK) {
253 long classNameId = classNameLocalService.getClassNameId(className);
254
255 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
256 }
257
258
264 @Override
265 public List<TrashEntry> getEntries(long groupId) {
266 return trashEntryPersistence.findByGroupId(groupId);
267 }
268
269
278 @Override
279 public List<TrashEntry> getEntries(long groupId, int start, int end) {
280 return trashEntryPersistence.findByGroupId(groupId, start, end);
281 }
282
283
295 @Override
296 public List<TrashEntry> getEntries(
297 long groupId, int start, int end, OrderByComparator<TrashEntry> obc) {
298
299 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
300 }
301
302 @Override
303 public List<TrashEntry> getEntries(long groupId, String className) {
304 long classNameId = classNameLocalService.getClassNameId(className);
305
306 return trashEntryPersistence.findByG_C(groupId, classNameId);
307 }
308
309
315 @Override
316 public int getEntriesCount(long groupId) {
317 return trashEntryPersistence.countByGroupId(groupId);
318 }
319
320
328 @Override
329 public TrashEntry getEntry(long entryId) throws PortalException {
330 return trashEntryPersistence.findByPrimaryKey(entryId);
331 }
332
333
342 @Override
343 public TrashEntry getEntry(String className, long classPK)
344 throws PortalException {
345
346 long classNameId = classNameLocalService.getClassNameId(className);
347
348 return trashEntryPersistence.findByC_C(classNameId, classPK);
349 }
350
351 @Override
352 public Hits search(
353 long companyId, long groupId, long userId, String keywords, int start,
354 int end, Sort sort) {
355
356 try {
357 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
358 TrashEntry.class);
359
360 SearchContext searchContext = buildSearchContext(
361 companyId, groupId, userId, keywords, start, end, sort);
362
363 return indexer.search(searchContext);
364 }
365 catch (Exception e) {
366 throw new SystemException(e);
367 }
368 }
369
370 @Override
371 public BaseModelSearchResult<TrashEntry> searchTrashEntries(
372 long companyId, long groupId, long userId, String keywords, int start,
373 int end, Sort sort) {
374
375 try {
376 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
377 TrashEntry.class);
378
379 SearchContext searchContext = buildSearchContext(
380 companyId, groupId, userId, keywords, start, end, sort);
381
382 Hits hits = indexer.search(searchContext);
383
384 List<TrashEntry> trashEntries = TrashUtil.getEntries(hits);
385
386 return new BaseModelSearchResult<>(trashEntries, hits.getLength());
387 }
388 catch (Exception e) {
389 throw new SystemException(e);
390 }
391 }
392
393 protected SearchContext buildSearchContext(
394 long companyId, long groupId, long userId, String keywords, int start,
395 int end, Sort sort) {
396
397 SearchContext searchContext = new SearchContext();
398
399 searchContext.setCompanyId(companyId);
400 searchContext.setEnd(end);
401 searchContext.setKeywords(keywords);
402 searchContext.setGroupIds(new long[] {groupId});
403
404 if (sort != null) {
405 searchContext.setSorts(sort);
406 }
407
408 searchContext.setStart(start);
409 searchContext.setUserId(userId);
410
411 QueryConfig queryConfig = searchContext.getQueryConfig();
412
413 queryConfig.setHighlightEnabled(false);
414 queryConfig.setScoreEnabled(false);
415
416 return searchContext;
417 }
418
419 protected Date getMaxAge(Group group) throws PortalException {
420 Calendar calendar = Calendar.getInstance();
421
422 calendar.setTime(new Date());
423
424 int maxAge = TrashUtil.getMaxAge(group);
425
426 calendar.add(Calendar.MINUTE, -maxAge);
427
428 return calendar.getTime();
429 }
430
431 }