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.User;
034 import com.liferay.portal.service.persistence.GroupActionableDynamicQuery;
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.util.TrashUtil;
040
041 import java.util.Calendar;
042 import java.util.Date;
043 import java.util.List;
044
045
051 public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
052
053
069 public TrashEntry addTrashEntry(
070 long userId, long groupId, String className, long classPK,
071 int status, List<ObjectValuePair<Long, Integer>> statusOVPs,
072 UnicodeProperties typeSettingsProperties)
073 throws PortalException, SystemException {
074
075 User user = userPersistence.findByPrimaryKey(userId);
076 long classNameId = PortalUtil.getClassNameId(className);
077
078 long entryId = counterLocalService.increment();
079
080 TrashEntry trashEntry = trashEntryPersistence.create(entryId);
081
082 trashEntry.setGroupId(groupId);
083 trashEntry.setCompanyId(user.getCompanyId());
084 trashEntry.setUserId(user.getUserId());
085 trashEntry.setUserName(user.getFullName());
086 trashEntry.setCreateDate(new Date());
087 trashEntry.setClassNameId(classNameId);
088 trashEntry.setClassPK(classPK);
089
090 if (typeSettingsProperties != null) {
091 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
092 }
093
094 trashEntry.setStatus(status);
095
096 trashEntryPersistence.update(trashEntry);
097
098 if (statusOVPs != null) {
099 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
100 long versionId = counterLocalService.increment();
101
102 TrashVersion trashVersion = trashVersionPersistence.create(
103 versionId);
104
105 trashVersion.setEntryId(entryId);
106 trashVersion.setClassNameId(classNameId);
107 trashVersion.setClassPK(statusOVP.getKey());
108 trashVersion.setStatus(statusOVP.getValue());
109
110 trashVersionPersistence.update(trashVersion);
111 }
112 }
113
114 return trashEntry;
115 }
116
117 public void checkEntries() throws PortalException, SystemException {
118 ActionableDynamicQuery actionableDynamicQuery =
119 new GroupActionableDynamicQuery() {
120
121 @Override
122 protected void performAction(Object object)
123 throws PortalException, SystemException {
124
125 Group group = (Group)object;
126
127 Date date = getMaxAge(group);
128
129 List<TrashEntry> entries = trashEntryPersistence.findByG_LtCD(
130 group.getGroupId(), date);
131
132 for (TrashEntry entry : entries) {
133 TrashHandler trashHandler =
134 TrashHandlerRegistryUtil.getTrashHandler(
135 entry.getClassName());
136
137 trashHandler.deleteTrashEntry(entry.getClassPK(), false);
138 }
139 }
140
141 };
142
143 actionableDynamicQuery.performActions();
144 }
145
146
154 public TrashEntry deleteEntry(long entryId)
155 throws PortalException, SystemException {
156
157 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
158
159 return deleteEntry(entry);
160 }
161
162
171 public TrashEntry deleteEntry(String className, long classPK)
172 throws PortalException, SystemException {
173
174 long classNameId = PortalUtil.getClassNameId(className);
175
176 TrashEntry entry = trashEntryPersistence.fetchByC_C(
177 classNameId, classPK);
178
179 return deleteEntry(entry);
180 }
181
182 @Indexable(type = IndexableType.DELETE)
183 public TrashEntry deleteEntry(TrashEntry trashEntry)
184 throws SystemException {
185
186 if (trashEntry != null) {
187 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
188
189 trashEntry = trashEntryPersistence.remove(trashEntry);
190 }
191
192 return trashEntry;
193 }
194
195
202 public TrashEntry fetchEntry(long entryId) throws SystemException {
203 return trashEntryPersistence.fetchByPrimaryKey(entryId);
204 }
205
206
214 public TrashEntry fetchEntry(String className, long classPK)
215 throws SystemException {
216
217 long classNameId = PortalUtil.getClassNameId(className);
218
219 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
220 }
221
222
229 public List<TrashEntry> getEntries(long groupId) throws SystemException {
230 return trashEntryPersistence.findByGroupId(groupId);
231 }
232
233
243 public List<TrashEntry> getEntries(long groupId, int start, int end)
244 throws SystemException {
245
246 return trashEntryPersistence.findByGroupId(groupId, start, end);
247 }
248
249
262 public List<TrashEntry> getEntries(
263 long groupId, int start, int end, OrderByComparator obc)
264 throws SystemException {
265
266 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
267 }
268
269
276 public int getEntriesCount(long groupId) throws SystemException {
277 return trashEntryPersistence.countByGroupId(groupId);
278 }
279
280
289 public TrashEntry getEntry(long entryId)
290 throws PortalException, SystemException {
291
292 return trashEntryPersistence.findByPrimaryKey(entryId);
293 }
294
295
305 public TrashEntry getEntry(String className, long classPK)
306 throws PortalException, SystemException {
307
308 long classNameId = PortalUtil.getClassNameId(className);
309
310 return trashEntryPersistence.findByC_C(classNameId, classPK);
311 }
312
313
320 public List<TrashVersion> getVersions(long entryId) throws SystemException {
321 return trashVersionPersistence.findByEntryId(entryId);
322 }
323
324
332 public List<TrashVersion> getVersions(String className, long classPK)
333 throws SystemException {
334
335 long classNameId = PortalUtil.getClassNameId(className);
336
337 return trashVersionPersistence.findByC_C(classNameId, classPK);
338 }
339
340 public Hits search(
341 long companyId, long groupId, long userId, String keywords,
342 int start, int end, Sort sort)
343 throws SystemException {
344
345 try {
346 SearchContext searchContext = new SearchContext();
347
348 searchContext.setCompanyId(companyId);
349 searchContext.setEnd(end);
350 searchContext.setKeywords(keywords);
351 searchContext.setGroupIds(new long[] {groupId});
352
353 if (sort != null) {
354 searchContext.setSorts(new Sort[] {sort});
355 }
356
357 searchContext.setStart(start);
358 searchContext.setUserId(userId);
359
360 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
361 TrashEntry.class);
362
363 return indexer.search(searchContext);
364 }
365 catch (Exception e) {
366 throw new SystemException(e);
367 }
368 }
369
370 protected Date getMaxAge(Group group)
371 throws PortalException, SystemException {
372
373 Calendar calendar = Calendar.getInstance();
374
375 calendar.setTime(new Date());
376
377 int maxAge = TrashUtil.getMaxAge(group);
378
379 calendar.add(Calendar.DATE, -maxAge);
380
381 return calendar.getTime();
382 }
383
384 }