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.service.persistence.GroupActionableDynamicQuery;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portlet.trash.model.TrashEntry;
038 import com.liferay.portlet.trash.model.TrashVersion;
039 import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
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 GroupActionableDynamicQuery() {
131
132 @Override
133 protected void performAction(Object object)
134 throws PortalException, SystemException {
135
136 Group group = (Group)object;
137
138 Date date = getMaxAge(group);
139
140 List<TrashEntry> entries = trashEntryPersistence.findByG_LtCD(
141 group.getGroupId(), date);
142
143 for (TrashEntry entry : entries) {
144 TrashHandler trashHandler =
145 TrashHandlerRegistryUtil.getTrashHandler(
146 entry.getClassName());
147
148 trashHandler.deleteTrashEntry(entry.getClassPK());
149 }
150 }
151
152 };
153
154 actionableDynamicQuery.performActions();
155 }
156
157
166 @Override
167 public TrashEntry deleteEntry(long entryId)
168 throws PortalException, SystemException {
169
170 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
171
172 return deleteEntry(entry);
173 }
174
175
185 @Override
186 public TrashEntry deleteEntry(String className, long classPK)
187 throws PortalException, SystemException {
188
189 long classNameId = PortalUtil.getClassNameId(className);
190
191 TrashEntry entry = trashEntryPersistence.fetchByC_C(
192 classNameId, classPK);
193
194 return deleteEntry(entry);
195 }
196
197 @Indexable(type = IndexableType.DELETE)
198 @Override
199 public TrashEntry deleteEntry(TrashEntry trashEntry)
200 throws SystemException {
201
202 if (trashEntry != null) {
203 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
204
205 trashEntry = trashEntryPersistence.remove(trashEntry);
206
207 systemEventLocalService.deleteSystemEvents(
208 trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
209 }
210
211 return trashEntry;
212 }
213
214
221 @Override
222 public TrashEntry fetchEntry(long entryId) throws SystemException {
223 return trashEntryPersistence.fetchByPrimaryKey(entryId);
224 }
225
226
234 @Override
235 public TrashEntry fetchEntry(String className, long classPK)
236 throws SystemException {
237
238 long classNameId = PortalUtil.getClassNameId(className);
239
240 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
241 }
242
243
250 @Override
251 public List<TrashEntry> getEntries(long groupId) throws SystemException {
252 return trashEntryPersistence.findByGroupId(groupId);
253 }
254
255
265 @Override
266 public List<TrashEntry> getEntries(long groupId, int start, int end)
267 throws SystemException {
268
269 return trashEntryPersistence.findByGroupId(groupId, start, end);
270 }
271
272
285 @Override
286 public List<TrashEntry> getEntries(
287 long groupId, int start, int end, OrderByComparator obc)
288 throws SystemException {
289
290 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
291 }
292
293 @Override
294 public List<TrashEntry> getEntries(long groupId, String className)
295 throws SystemException {
296
297 long classNameId = PortalUtil.getClassNameId(className);
298
299 return trashEntryPersistence.findByG_C(groupId, classNameId);
300 }
301
302
309 @Override
310 public int getEntriesCount(long groupId) throws SystemException {
311 return trashEntryPersistence.countByGroupId(groupId);
312 }
313
314
323 @Override
324 public TrashEntry getEntry(long entryId)
325 throws PortalException, SystemException {
326
327 return trashEntryPersistence.findByPrimaryKey(entryId);
328 }
329
330
340 @Override
341 public TrashEntry getEntry(String className, long classPK)
342 throws PortalException, SystemException {
343
344 long classNameId = PortalUtil.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,
352 int start, int end, Sort sort)
353 throws SystemException {
354
355 try {
356 SearchContext searchContext = new SearchContext();
357
358 searchContext.setCompanyId(companyId);
359 searchContext.setEnd(end);
360 searchContext.setKeywords(keywords);
361 searchContext.setGroupIds(new long[] {groupId});
362
363 if (sort != null) {
364 searchContext.setSorts(sort);
365 }
366
367 searchContext.setStart(start);
368 searchContext.setUserId(userId);
369
370 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
371 TrashEntry.class);
372
373 return indexer.search(searchContext);
374 }
375 catch (Exception e) {
376 throw new SystemException(e);
377 }
378 }
379
380 protected Date getMaxAge(Group group)
381 throws PortalException, SystemException {
382
383 Calendar calendar = Calendar.getInstance();
384
385 calendar.setTime(new Date());
386
387 int maxAge = TrashUtil.getMaxAge(group);
388
389 calendar.add(Calendar.MINUTE, -maxAge);
390
391 return calendar.getTime();
392 }
393
394 }