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 trashHandler.deleteTrashEntry(trashEntry.getClassPK());
157 }
158 }
159
160 };
161
162 actionableDynamicQuery.performActions();
163 }
164
165 @Override
166 public void deleteEntries(long groupId) throws SystemException {
167 List<TrashEntry> entries = getEntries(groupId);
168
169 for (TrashEntry entry : entries) {
170 deleteEntry(entry);
171 }
172 }
173
174
183 @Override
184 public TrashEntry deleteEntry(long entryId)
185 throws PortalException, SystemException {
186
187 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
188
189 return deleteEntry(entry);
190 }
191
192
202 @Override
203 public TrashEntry deleteEntry(String className, long classPK)
204 throws PortalException, SystemException {
205
206 long classNameId = PortalUtil.getClassNameId(className);
207
208 TrashEntry entry = trashEntryPersistence.fetchByC_C(
209 classNameId, classPK);
210
211 return deleteEntry(entry);
212 }
213
214 @Indexable(type = IndexableType.DELETE)
215 @Override
216 public TrashEntry deleteEntry(TrashEntry trashEntry)
217 throws SystemException {
218
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
238 @Override
239 public TrashEntry fetchEntry(long entryId) throws SystemException {
240 return trashEntryPersistence.fetchByPrimaryKey(entryId);
241 }
242
243
251 @Override
252 public TrashEntry fetchEntry(String className, long classPK)
253 throws SystemException {
254
255 long classNameId = PortalUtil.getClassNameId(className);
256
257 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
258 }
259
260
267 @Override
268 public List<TrashEntry> getEntries(long groupId) throws SystemException {
269 return trashEntryPersistence.findByGroupId(groupId);
270 }
271
272
282 @Override
283 public List<TrashEntry> getEntries(long groupId, int start, int end)
284 throws SystemException {
285
286 return trashEntryPersistence.findByGroupId(groupId, start, end);
287 }
288
289
302 @Override
303 public List<TrashEntry> getEntries(
304 long groupId, int start, int end, OrderByComparator obc)
305 throws SystemException {
306
307 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
308 }
309
310 @Override
311 public List<TrashEntry> getEntries(long groupId, String className)
312 throws SystemException {
313
314 long classNameId = PortalUtil.getClassNameId(className);
315
316 return trashEntryPersistence.findByG_C(groupId, classNameId);
317 }
318
319
326 @Override
327 public int getEntriesCount(long groupId) throws SystemException {
328 return trashEntryPersistence.countByGroupId(groupId);
329 }
330
331
340 @Override
341 public TrashEntry getEntry(long entryId)
342 throws PortalException, SystemException {
343
344 return trashEntryPersistence.findByPrimaryKey(entryId);
345 }
346
347
357 @Override
358 public TrashEntry getEntry(String className, long classPK)
359 throws PortalException, SystemException {
360
361 long classNameId = PortalUtil.getClassNameId(className);
362
363 return trashEntryPersistence.findByC_C(classNameId, classPK);
364 }
365
366 @Override
367 public Hits search(
368 long companyId, long groupId, long userId, String keywords,
369 int start, int end, Sort sort)
370 throws SystemException {
371
372 try {
373 SearchContext searchContext = new SearchContext();
374
375 searchContext.setCompanyId(companyId);
376 searchContext.setEnd(end);
377 searchContext.setKeywords(keywords);
378 searchContext.setGroupIds(new long[] {groupId});
379
380 if (sort != null) {
381 searchContext.setSorts(sort);
382 }
383
384 searchContext.setStart(start);
385 searchContext.setUserId(userId);
386
387 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
388 TrashEntry.class);
389
390 return indexer.search(searchContext);
391 }
392 catch (Exception e) {
393 throw new SystemException(e);
394 }
395 }
396
397 protected Date getMaxAge(Group group)
398 throws PortalException, SystemException {
399
400 Calendar calendar = Calendar.getInstance();
401
402 calendar.setTime(new Date());
403
404 int maxAge = TrashUtil.getMaxAge(group);
405
406 calendar.add(Calendar.MINUTE, -maxAge);
407
408 return calendar.getTime();
409 }
410
411 }