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 Date date = getMaxAge(group);
144
145 if (createDate.before(date) ||
146 !TrashUtil.isTrashEnabled(group.getGroupId())) {
147
148 TrashHandler trashHandler =
149 TrashHandlerRegistryUtil.getTrashHandler(
150 trashEntry.getClassName());
151
152 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
153 }
154 }
155
156 };
157
158 actionableDynamicQuery.performActions();
159 }
160
161
170 @Override
171 public TrashEntry deleteEntry(long entryId)
172 throws PortalException, SystemException {
173
174 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
175
176 return deleteEntry(entry);
177 }
178
179
189 @Override
190 public TrashEntry deleteEntry(String className, long classPK)
191 throws PortalException, SystemException {
192
193 long classNameId = PortalUtil.getClassNameId(className);
194
195 TrashEntry entry = trashEntryPersistence.fetchByC_C(
196 classNameId, classPK);
197
198 return deleteEntry(entry);
199 }
200
201 @Indexable(type = IndexableType.DELETE)
202 @Override
203 public TrashEntry deleteEntry(TrashEntry trashEntry)
204 throws SystemException {
205
206 if (trashEntry != null) {
207 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
208
209 trashEntry = trashEntryPersistence.remove(trashEntry);
210
211 systemEventLocalService.deleteSystemEvents(
212 trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
213 }
214
215 return trashEntry;
216 }
217
218
225 @Override
226 public TrashEntry fetchEntry(long entryId) throws SystemException {
227 return trashEntryPersistence.fetchByPrimaryKey(entryId);
228 }
229
230
238 @Override
239 public TrashEntry fetchEntry(String className, long classPK)
240 throws SystemException {
241
242 long classNameId = PortalUtil.getClassNameId(className);
243
244 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
245 }
246
247
254 @Override
255 public List<TrashEntry> getEntries(long groupId) throws SystemException {
256 return trashEntryPersistence.findByGroupId(groupId);
257 }
258
259
269 @Override
270 public List<TrashEntry> getEntries(long groupId, int start, int end)
271 throws SystemException {
272
273 return trashEntryPersistence.findByGroupId(groupId, start, end);
274 }
275
276
289 @Override
290 public List<TrashEntry> getEntries(
291 long groupId, int start, int end, OrderByComparator obc)
292 throws SystemException {
293
294 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
295 }
296
297 @Override
298 public List<TrashEntry> getEntries(long groupId, String className)
299 throws SystemException {
300
301 long classNameId = PortalUtil.getClassNameId(className);
302
303 return trashEntryPersistence.findByG_C(groupId, classNameId);
304 }
305
306
313 @Override
314 public int getEntriesCount(long groupId) throws SystemException {
315 return trashEntryPersistence.countByGroupId(groupId);
316 }
317
318
327 @Override
328 public TrashEntry getEntry(long entryId)
329 throws PortalException, SystemException {
330
331 return trashEntryPersistence.findByPrimaryKey(entryId);
332 }
333
334
344 @Override
345 public TrashEntry getEntry(String className, long classPK)
346 throws PortalException, SystemException {
347
348 long classNameId = PortalUtil.getClassNameId(className);
349
350 return trashEntryPersistence.findByC_C(classNameId, classPK);
351 }
352
353 @Override
354 public Hits search(
355 long companyId, long groupId, long userId, String keywords,
356 int start, int end, Sort sort)
357 throws SystemException {
358
359 try {
360 SearchContext searchContext = new SearchContext();
361
362 searchContext.setCompanyId(companyId);
363 searchContext.setEnd(end);
364 searchContext.setKeywords(keywords);
365 searchContext.setGroupIds(new long[] {groupId});
366
367 if (sort != null) {
368 searchContext.setSorts(sort);
369 }
370
371 searchContext.setStart(start);
372 searchContext.setUserId(userId);
373
374 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
375 TrashEntry.class);
376
377 return indexer.search(searchContext);
378 }
379 catch (Exception e) {
380 throw new SystemException(e);
381 }
382 }
383
384 protected Date getMaxAge(Group group)
385 throws PortalException, SystemException {
386
387 Calendar calendar = Calendar.getInstance();
388
389 calendar.setTime(new Date());
390
391 int maxAge = TrashUtil.getMaxAge(group);
392
393 calendar.add(Calendar.MINUTE, -maxAge);
394
395 return calendar.getTime();
396 }
397
398 }