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 @Override
070 public TrashEntry addTrashEntry(
071 long userId, long groupId, String className, long classPK,
072 int status, List<ObjectValuePair<Long, Integer>> statusOVPs,
073 UnicodeProperties typeSettingsProperties)
074 throws PortalException, SystemException {
075
076 User user = userPersistence.findByPrimaryKey(userId);
077 long classNameId = PortalUtil.getClassNameId(className);
078
079 long entryId = counterLocalService.increment();
080
081 TrashEntry trashEntry = trashEntryPersistence.create(entryId);
082
083 trashEntry.setGroupId(groupId);
084 trashEntry.setCompanyId(user.getCompanyId());
085 trashEntry.setUserId(user.getUserId());
086 trashEntry.setUserName(user.getFullName());
087 trashEntry.setCreateDate(new Date());
088 trashEntry.setClassNameId(classNameId);
089 trashEntry.setClassPK(classPK);
090
091 if (typeSettingsProperties != null) {
092 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
093 }
094
095 trashEntry.setStatus(status);
096
097 trashEntryPersistence.update(trashEntry);
098
099 if (statusOVPs != null) {
100 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
101 long versionId = counterLocalService.increment();
102
103 TrashVersion trashVersion = trashVersionPersistence.create(
104 versionId);
105
106 trashVersion.setEntryId(entryId);
107 trashVersion.setClassNameId(classNameId);
108 trashVersion.setClassPK(statusOVP.getKey());
109 trashVersion.setStatus(statusOVP.getValue());
110
111 trashVersionPersistence.update(trashVersion);
112 }
113 }
114
115 return trashEntry;
116 }
117
118 @Override
119 public void checkEntries() throws PortalException, SystemException {
120 ActionableDynamicQuery actionableDynamicQuery =
121 new GroupActionableDynamicQuery() {
122
123 @Override
124 protected void performAction(Object object)
125 throws PortalException, SystemException {
126
127 Group group = (Group)object;
128
129 Date date = getMaxAge(group);
130
131 List<TrashEntry> entries = trashEntryPersistence.findByG_LtCD(
132 group.getGroupId(), date);
133
134 for (TrashEntry entry : entries) {
135 TrashHandler trashHandler =
136 TrashHandlerRegistryUtil.getTrashHandler(
137 entry.getClassName());
138
139 trashHandler.deleteTrashEntry(entry.getClassPK());
140 }
141 }
142
143 };
144
145 actionableDynamicQuery.performActions();
146 }
147
148
157 @Override
158 public TrashEntry deleteEntry(long entryId)
159 throws PortalException, SystemException {
160
161 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
162
163 return deleteEntry(entry);
164 }
165
166
176 @Override
177 public TrashEntry deleteEntry(String className, long classPK)
178 throws PortalException, SystemException {
179
180 long classNameId = PortalUtil.getClassNameId(className);
181
182 TrashEntry entry = trashEntryPersistence.fetchByC_C(
183 classNameId, classPK);
184
185 return deleteEntry(entry);
186 }
187
188 @Indexable(type = IndexableType.DELETE)
189 @Override
190 public TrashEntry deleteEntry(TrashEntry trashEntry)
191 throws SystemException {
192
193 if (trashEntry != null) {
194 trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
195
196 trashEntry = trashEntryPersistence.remove(trashEntry);
197 }
198
199 return trashEntry;
200 }
201
202
209 @Override
210 public TrashEntry fetchEntry(long entryId) throws SystemException {
211 return trashEntryPersistence.fetchByPrimaryKey(entryId);
212 }
213
214
222 @Override
223 public TrashEntry fetchEntry(String className, long classPK)
224 throws SystemException {
225
226 long classNameId = PortalUtil.getClassNameId(className);
227
228 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
229 }
230
231
238 @Override
239 public List<TrashEntry> getEntries(long groupId) throws SystemException {
240 return trashEntryPersistence.findByGroupId(groupId);
241 }
242
243
253 @Override
254 public List<TrashEntry> getEntries(long groupId, int start, int end)
255 throws SystemException {
256
257 return trashEntryPersistence.findByGroupId(groupId, start, end);
258 }
259
260
273 @Override
274 public List<TrashEntry> getEntries(
275 long groupId, int start, int end, OrderByComparator obc)
276 throws SystemException {
277
278 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
279 }
280
281
288 @Override
289 public int getEntriesCount(long groupId) throws SystemException {
290 return trashEntryPersistence.countByGroupId(groupId);
291 }
292
293
302 @Override
303 public TrashEntry getEntry(long entryId)
304 throws PortalException, SystemException {
305
306 return trashEntryPersistence.findByPrimaryKey(entryId);
307 }
308
309
319 @Override
320 public TrashEntry getEntry(String className, long classPK)
321 throws PortalException, SystemException {
322
323 long classNameId = PortalUtil.getClassNameId(className);
324
325 return trashEntryPersistence.findByC_C(classNameId, classPK);
326 }
327
328
335 @Override
336 public List<TrashVersion> getVersions(long entryId) throws SystemException {
337 return trashVersionPersistence.findByEntryId(entryId);
338 }
339
340
348 @Override
349 public List<TrashVersion> getVersions(String className, long classPK)
350 throws SystemException {
351
352 long classNameId = PortalUtil.getClassNameId(className);
353
354 return trashVersionPersistence.findByC_C(classNameId, classPK);
355 }
356
357 @Override
358 public Hits search(
359 long companyId, long groupId, long userId, String keywords,
360 int start, int end, Sort sort)
361 throws SystemException {
362
363 try {
364 SearchContext searchContext = new SearchContext();
365
366 searchContext.setCompanyId(companyId);
367 searchContext.setEnd(end);
368 searchContext.setKeywords(keywords);
369 searchContext.setGroupIds(new long[] {groupId});
370
371 if (sort != null) {
372 searchContext.setSorts(sort);
373 }
374
375 searchContext.setStart(start);
376 searchContext.setUserId(userId);
377
378 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
379 TrashEntry.class);
380
381 return indexer.search(searchContext);
382 }
383 catch (Exception e) {
384 throw new SystemException(e);
385 }
386 }
387
388 protected Date getMaxAge(Group group)
389 throws PortalException, SystemException {
390
391 Calendar calendar = Calendar.getInstance();
392
393 calendar.setTime(new Date());
394
395 int maxAge = TrashUtil.getMaxAge(group);
396
397 calendar.add(Calendar.MINUTE, -maxAge);
398
399 return calendar.getTime();
400 }
401
402 }