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.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.search.Hits;
021 import com.liferay.portal.kernel.search.Indexable;
022 import com.liferay.portal.kernel.search.IndexableType;
023 import com.liferay.portal.kernel.search.Indexer;
024 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025 import com.liferay.portal.kernel.search.SearchContext;
026 import com.liferay.portal.kernel.search.Sort;
027 import com.liferay.portal.kernel.trash.TrashHandler;
028 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
029 import com.liferay.portal.kernel.util.ObjectValuePair;
030 import com.liferay.portal.kernel.util.OrderByComparator;
031 import com.liferay.portal.kernel.util.UnicodeProperties;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.SystemEvent;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portlet.trash.model.TrashEntry;
037 import com.liferay.portlet.trash.model.TrashVersion;
038 import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
039 import com.liferay.portlet.trash.service.persistence.TrashEntryActionableDynamicQuery;
040 import com.liferay.portlet.trash.util.TrashUtil;
041
042 import java.util.Calendar;
043 import java.util.Date;
044 import java.util.List;
045
046
052 public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
053
054
070 @Override
071 public TrashEntry addTrashEntry(
072 long userId, long groupId, String className, long classPK,
073 String classUuid, String referrerClassName, int status,
074 List<ObjectValuePair<Long, Integer>> statusOVPs,
075 UnicodeProperties typeSettingsProperties)
076 throws PortalException, SystemException {
077
078 User user = userPersistence.findByPrimaryKey(userId);
079 long classNameId = PortalUtil.getClassNameId(className);
080
081 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
082 className);
083
084 SystemEvent systemEvent = trashHandler.addDeletionSystemEvent(
085 userId, groupId, classPK, classUuid, referrerClassName);
086
087 long entryId = counterLocalService.increment();
088
089 TrashEntry trashEntry = trashEntryPersistence.create(entryId);
090
091 trashEntry.setGroupId(groupId);
092 trashEntry.setCompanyId(user.getCompanyId());
093 trashEntry.setUserId(user.getUserId());
094 trashEntry.setUserName(user.getFullName());
095 trashEntry.setCreateDate(new Date());
096 trashEntry.setClassNameId(classNameId);
097 trashEntry.setClassPK(classPK);
098 trashEntry.setSystemEventSetKey(systemEvent.getSystemEventSetKey());
099
100 if (typeSettingsProperties != null) {
101 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
102 }
103
104 trashEntry.setStatus(status);
105
106 trashEntryPersistence.update(trashEntry);
107
108 if (statusOVPs != null) {
109 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
110 long versionId = counterLocalService.increment();
111
112 TrashVersion trashVersion = trashVersionPersistence.create(
113 versionId);
114
115 trashVersion.setEntryId(entryId);
116 trashVersion.setClassNameId(classNameId);
117 trashVersion.setClassPK(statusOVP.getKey());
118 trashVersion.setStatus(statusOVP.getValue());
119
120 trashVersionPersistence.update(trashVersion);
121 }
122 }
123
124 return trashEntry;
125 }
126
127 @Override
128 public void checkEntries() throws PortalException, SystemException {
129 ActionableDynamicQuery actionableDynamicQuery =
130 new TrashEntryActionableDynamicQuery() {
131
132 @Override
133 protected void performAction(Object object)
134 throws PortalException, SystemException {
135
136 TrashEntry trashEntry = (TrashEntry)object;
137
138 Date createDate = trashEntry.getCreateDate();
139
140 Group group = groupPersistence.fetchByPrimaryKey(
141 trashEntry.getGroupId());
142
143 if (group == null) {
144 return;
145 }
146
147 Date date = getMaxAge(group);
148
149 if (createDate.before(date) ||
150 !TrashUtil.isTrashEnabled(group.getGroupId())) {
151
152 TrashHandler trashHandler =
153 TrashHandlerRegistryUtil.getTrashHandler(
154 trashEntry.getClassName());
155
156 if (trashHandler != null) {
157 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
158 }
159 }
160 }
161
162 };
163
164 actionableDynamicQuery.performActions();
165 }
166
167 @Override
168 public void deleteEntries(long groupId) throws SystemException {
169 List<TrashEntry> entries = getEntries(groupId);
170
171 for (TrashEntry entry : entries) {
172 deleteEntry(entry);
173 }
174 }
175
176
185 @Override
186 public TrashEntry deleteEntry(long entryId)
187 throws PortalException, SystemException {
188
189 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
190
191 return deleteEntry(entry);
192 }
193
194
204 @Override
205 public TrashEntry deleteEntry(String className, long classPK)
206 throws PortalException, SystemException {
207
208 long classNameId = PortalUtil.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 throws SystemException {
220
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
240 @Override
241 public TrashEntry fetchEntry(long entryId) throws SystemException {
242 return trashEntryPersistence.fetchByPrimaryKey(entryId);
243 }
244
245
253 @Override
254 public TrashEntry fetchEntry(String className, long classPK)
255 throws SystemException {
256
257 long classNameId = PortalUtil.getClassNameId(className);
258
259 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
260 }
261
262
269 @Override
270 public List<TrashEntry> getEntries(long groupId) throws SystemException {
271 return trashEntryPersistence.findByGroupId(groupId);
272 }
273
274
284 @Override
285 public List<TrashEntry> getEntries(long groupId, int start, int end)
286 throws SystemException {
287
288 return trashEntryPersistence.findByGroupId(groupId, start, end);
289 }
290
291
304 @Override
305 public List<TrashEntry> getEntries(
306 long groupId, int start, int end, OrderByComparator obc)
307 throws SystemException {
308
309 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
310 }
311
312 @Override
313 public List<TrashEntry> getEntries(long groupId, String className)
314 throws SystemException {
315
316 long classNameId = PortalUtil.getClassNameId(className);
317
318 return trashEntryPersistence.findByG_C(groupId, classNameId);
319 }
320
321
328 @Override
329 public int getEntriesCount(long groupId) throws SystemException {
330 return trashEntryPersistence.countByGroupId(groupId);
331 }
332
333
342 @Override
343 public TrashEntry getEntry(long entryId)
344 throws PortalException, SystemException {
345
346 return trashEntryPersistence.findByPrimaryKey(entryId);
347 }
348
349
359 @Override
360 public TrashEntry getEntry(String className, long classPK)
361 throws PortalException, SystemException {
362
363 long classNameId = PortalUtil.getClassNameId(className);
364
365 return trashEntryPersistence.findByC_C(classNameId, classPK);
366 }
367
368 @Override
369 public Hits search(
370 long companyId, long groupId, long userId, String keywords,
371 int start, int end, Sort sort)
372 throws SystemException {
373
374 try {
375 SearchContext searchContext = new SearchContext();
376
377 searchContext.setCompanyId(companyId);
378 searchContext.setEnd(end);
379 searchContext.setKeywords(keywords);
380 searchContext.setGroupIds(new long[] {groupId});
381
382 if (sort != null) {
383 searchContext.setSorts(sort);
384 }
385
386 searchContext.setStart(start);
387 searchContext.setUserId(userId);
388
389 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
390 TrashEntry.class);
391
392 return indexer.search(searchContext);
393 }
394 catch (Exception e) {
395 throw new SystemException(e);
396 }
397 }
398
399 protected Date getMaxAge(Group group)
400 throws PortalException, SystemException {
401
402 Calendar calendar = Calendar.getInstance();
403
404 calendar.setTime(new Date());
405
406 int maxAge = TrashUtil.getMaxAge(group);
407
408 calendar.add(Calendar.MINUTE, -maxAge);
409
410 return calendar.getTime();
411 }
412
413 }