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.DefaultActionableDynamicQuery;
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
072 @Override
073 public TrashEntry addTrashEntry(
074 long userId, long groupId, String className, long classPK,
075 String classUuid, String referrerClassName, int status,
076 List<ObjectValuePair<Long, Integer>> statusOVPs,
077 UnicodeProperties typeSettingsProperties)
078 throws PortalException {
079
080 User user = userPersistence.findByPrimaryKey(userId);
081 long classNameId = classNameLocalService.getClassNameId(className);
082
083 TrashEntry trashEntry = trashEntryPersistence.fetchByC_C(
084 classNameId, classPK);
085
086 if (trashEntry != null) {
087 return trashEntry;
088 }
089
090 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
091 className);
092
093 SystemEvent systemEvent = trashHandler.addDeletionSystemEvent(
094 userId, groupId, classPK, classUuid, referrerClassName);
095
096 long entryId = counterLocalService.increment();
097
098 trashEntry = trashEntryPersistence.create(entryId);
099
100 trashEntry.setGroupId(groupId);
101 trashEntry.setCompanyId(user.getCompanyId());
102 trashEntry.setUserId(user.getUserId());
103 trashEntry.setUserName(user.getFullName());
104 trashEntry.setCreateDate(new Date());
105 trashEntry.setClassNameId(classNameId);
106 trashEntry.setClassPK(classPK);
107 trashEntry.setSystemEventSetKey(systemEvent.getSystemEventSetKey());
108
109 if (typeSettingsProperties != null) {
110 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
111 }
112
113 trashEntry.setStatus(status);
114
115 trashEntryPersistence.update(trashEntry);
116
117 if (statusOVPs != null) {
118 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
119 long versionId = counterLocalService.increment();
120
121 TrashVersion trashVersion = trashVersionPersistence.create(
122 versionId);
123
124 trashVersion.setEntryId(entryId);
125 trashVersion.setClassNameId(classNameId);
126 trashVersion.setClassPK(statusOVP.getKey());
127 trashVersion.setStatus(statusOVP.getValue());
128
129 trashVersionPersistence.update(trashVersion);
130 }
131 }
132
133 return trashEntry;
134 }
135
136 @Override
137 public void checkEntries() throws PortalException {
138 ActionableDynamicQuery actionableDynamicQuery =
139 trashEntryLocalService.getActionableDynamicQuery();
140
141 actionableDynamicQuery.setPerformActionMethod(
142 new ActionableDynamicQuery.PerformActionMethod<TrashEntry>() {
143
144 @Override
145 public void performAction(TrashEntry trashEntry)
146 throws PortalException {
147
148 Date createDate = trashEntry.getCreateDate();
149
150 Group group = groupPersistence.fetchByPrimaryKey(
151 trashEntry.getGroupId());
152
153 if (group == null) {
154 return;
155 }
156
157 Date date = getMaxAge(group);
158
159 if (createDate.before(date) ||
160 !TrashUtil.isTrashEnabled(group)) {
161
162 TrashHandler trashHandler =
163 TrashHandlerRegistryUtil.getTrashHandler(
164 trashEntry.getClassName());
165
166 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
167 }
168 }
169
170 });
171 actionableDynamicQuery.setTransactionAttribute(
172 DefaultActionableDynamicQuery.REQUIRES_NEW_TRANSACTION_ATTRIBUTE);
173
174 actionableDynamicQuery.performActions();
175 }
176
177 @Override
178 public void deleteEntries(long groupId) {
179 List<TrashEntry> entries = getEntries(groupId);
180
181 for (TrashEntry entry : entries) {
182 deleteEntry(entry);
183 }
184 }
185
186
192 @Override
193 public TrashEntry deleteEntry(long entryId) {
194 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
195
196 return deleteEntry(entry);
197 }
198
199
206 @Override
207 public TrashEntry deleteEntry(String className, long classPK) {
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
324 @Override
325 public TrashEntry getEntry(long entryId) throws PortalException {
326 return trashEntryPersistence.findByPrimaryKey(entryId);
327 }
328
329
336 @Override
337 public TrashEntry getEntry(String className, long classPK)
338 throws PortalException {
339
340 long classNameId = classNameLocalService.getClassNameId(className);
341
342 return trashEntryPersistence.findByC_C(classNameId, classPK);
343 }
344
345 @Override
346 public Hits search(
347 long companyId, long groupId, long userId, String keywords, int start,
348 int end, Sort sort) {
349
350 try {
351 Indexer<TrashEntry> indexer =
352 IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
353
354 SearchContext searchContext = buildSearchContext(
355 companyId, groupId, userId, keywords, start, end, sort);
356
357 return indexer.search(searchContext);
358 }
359 catch (Exception e) {
360 throw new SystemException(e);
361 }
362 }
363
364 @Override
365 public BaseModelSearchResult<TrashEntry> searchTrashEntries(
366 long companyId, long groupId, long userId, String keywords, int start,
367 int end, Sort sort) {
368
369 try {
370 Indexer<TrashEntry> indexer =
371 IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
372
373 SearchContext searchContext = buildSearchContext(
374 companyId, groupId, userId, keywords, start, end, sort);
375
376 Hits hits = indexer.search(searchContext);
377
378 List<TrashEntry> trashEntries = TrashUtil.getEntries(hits);
379
380 return new BaseModelSearchResult<>(trashEntries, hits.getLength());
381 }
382 catch (Exception e) {
383 throw new SystemException(e);
384 }
385 }
386
387 protected SearchContext buildSearchContext(
388 long companyId, long groupId, long userId, String keywords, int start,
389 int end, Sort sort) {
390
391 SearchContext searchContext = new SearchContext();
392
393 searchContext.setCompanyId(companyId);
394 searchContext.setEnd(end);
395 searchContext.setKeywords(keywords);
396 searchContext.setGroupIds(new long[] {groupId});
397
398 if (sort != null) {
399 searchContext.setSorts(sort);
400 }
401
402 searchContext.setStart(start);
403 searchContext.setUserId(userId);
404
405 QueryConfig queryConfig = searchContext.getQueryConfig();
406
407 queryConfig.setHighlightEnabled(false);
408 queryConfig.setScoreEnabled(false);
409
410 return searchContext;
411 }
412
413 protected Date getMaxAge(Group group) throws PortalException {
414 Calendar calendar = Calendar.getInstance();
415
416 calendar.setTime(new Date());
417
418 int maxAge = TrashUtil.getMaxAge(group);
419
420 calendar.add(Calendar.MINUTE, -maxAge);
421
422 return calendar.getTime();
423 }
424
425 }