001
014
015 package com.liferay.portlet.trash.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.search.Hits;
020 import com.liferay.portal.kernel.search.Indexable;
021 import com.liferay.portal.kernel.search.IndexableType;
022 import com.liferay.portal.kernel.search.Indexer;
023 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024 import com.liferay.portal.kernel.search.SearchContext;
025 import com.liferay.portal.kernel.search.Sort;
026 import com.liferay.portal.kernel.trash.TrashHandler;
027 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
028 import com.liferay.portal.kernel.util.ObjectValuePair;
029 import com.liferay.portal.kernel.util.OrderByComparator;
030 import com.liferay.portal.kernel.util.UnicodeProperties;
031 import com.liferay.portal.model.Group;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portlet.trash.model.TrashEntry;
035 import com.liferay.portlet.trash.model.TrashVersion;
036 import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
037 import com.liferay.portlet.trash.util.TrashUtil;
038
039 import java.util.Calendar;
040 import java.util.Date;
041 import java.util.List;
042
043
049 public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
050
051
067 public TrashEntry addTrashEntry(
068 long userId, long groupId, String className, long classPK,
069 int status, List<ObjectValuePair<Long, Integer>> statusOVPs,
070 UnicodeProperties typeSettingsProperties)
071 throws PortalException, SystemException {
072
073 User user = userPersistence.findByPrimaryKey(userId);
074 long classNameId = PortalUtil.getClassNameId(className);
075
076 long entryId = counterLocalService.increment();
077
078 TrashEntry trashEntry = trashEntryPersistence.create(entryId);
079
080 trashEntry.setGroupId(groupId);
081 trashEntry.setCompanyId(user.getCompanyId());
082 trashEntry.setUserId(user.getUserId());
083 trashEntry.setUserName(user.getFullName());
084 trashEntry.setCreateDate(new Date());
085 trashEntry.setClassNameId(classNameId);
086 trashEntry.setClassPK(classPK);
087
088 if (typeSettingsProperties != null) {
089 trashEntry.setTypeSettingsProperties(typeSettingsProperties);
090 }
091
092 trashEntry.setStatus(status);
093
094 trashEntryPersistence.update(trashEntry);
095
096 if (statusOVPs != null) {
097 for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
098 long versionId = counterLocalService.increment();
099
100 TrashVersion trashVersion = trashVersionPersistence.create(
101 versionId);
102
103 trashVersion.setEntryId(entryId);
104 trashVersion.setClassNameId(classNameId);
105 trashVersion.setClassPK(statusOVP.getKey());
106 trashVersion.setStatus(statusOVP.getValue());
107
108 trashVersionPersistence.update(trashVersion);
109 }
110 }
111
112 return trashEntry;
113 }
114
115 public void checkEntries() throws PortalException, SystemException {
116 int count = groupPersistence.countAll();
117
118 int pages = count / Indexer.DEFAULT_INTERVAL;
119
120 for (int i = 0; i <= pages; i++) {
121 int start = (i * Indexer.DEFAULT_INTERVAL);
122 int end = start + Indexer.DEFAULT_INTERVAL;
123
124 List<Group> groups = groupPersistence.findAll(start, end);
125
126 for (Group group : groups) {
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 public void checkEntriesAttachments()
144 throws PortalException, SystemException {
145
146 int count = groupPersistence.countAll();
147
148 int pages = count / Indexer.DEFAULT_INTERVAL;
149
150 for (int i = 0; i <= pages; i++) {
151 int start = (i * Indexer.DEFAULT_INTERVAL);
152 int end = start + Indexer.DEFAULT_INTERVAL;
153
154 List<Group> groups = groupPersistence.findAll(start, end);
155
156 for (Group group : groups) {
157 checkEntriesAttachments(group);
158 }
159 }
160 }
161
162
170 public TrashEntry deleteEntry(long entryId)
171 throws PortalException, SystemException {
172
173 TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
174
175 return deleteEntry(entry);
176 }
177
178
187 public TrashEntry deleteEntry(String className, long classPK)
188 throws PortalException, SystemException {
189
190 long classNameId = PortalUtil.getClassNameId(className);
191
192 TrashEntry entry = trashEntryPersistence.fetchByC_C(
193 classNameId, classPK);
194
195 return deleteEntry(entry);
196 }
197
198 @Indexable(type = IndexableType.DELETE)
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
208 return trashEntry;
209 }
210
211
218 public TrashEntry fetchEntry(long entryId) throws SystemException {
219 return trashEntryPersistence.fetchByPrimaryKey(entryId);
220 }
221
222
230 public TrashEntry fetchEntry(String className, long classPK)
231 throws SystemException {
232
233 long classNameId = PortalUtil.getClassNameId(className);
234
235 return trashEntryPersistence.fetchByC_C(classNameId, classPK);
236 }
237
238
245 public List<TrashEntry> getEntries(long groupId) throws SystemException {
246 return trashEntryPersistence.findByGroupId(groupId);
247 }
248
249
259 public List<TrashEntry> getEntries(long groupId, int start, int end)
260 throws SystemException {
261
262 return trashEntryPersistence.findByGroupId(groupId, start, end);
263 }
264
265
278 public List<TrashEntry> getEntries(
279 long groupId, int start, int end, OrderByComparator obc)
280 throws SystemException {
281
282 return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
283 }
284
285
292 public int getEntriesCount(long groupId) throws SystemException {
293 return trashEntryPersistence.countByGroupId(groupId);
294 }
295
296
305 public TrashEntry getEntry(long entryId)
306 throws PortalException, SystemException {
307
308 return trashEntryPersistence.findByPrimaryKey(entryId);
309 }
310
311
321 public TrashEntry getEntry(String className, long classPK)
322 throws PortalException, SystemException {
323
324 long classNameId = PortalUtil.getClassNameId(className);
325
326 return trashEntryPersistence.findByC_C(classNameId, classPK);
327 }
328
329
336 public List<TrashVersion> getVersions(long entryId) throws SystemException {
337 return trashEntryPersistence.getTrashVersions(entryId);
338 }
339
340
348 public List<TrashVersion> getVersions(String className, long classPK)
349 throws SystemException {
350
351 long classNameId = PortalUtil.getClassNameId(className);
352
353 return trashVersionPersistence.findByC_C(classNameId, classPK);
354 }
355
356 public Hits search(
357 long companyId, long groupId, long userId, String keywords,
358 int start, int end, Sort sort)
359 throws SystemException {
360
361 try {
362 SearchContext searchContext = new SearchContext();
363
364 searchContext.setCompanyId(companyId);
365 searchContext.setEnd(end);
366 searchContext.setKeywords(keywords);
367 searchContext.setGroupIds(new long[] {groupId});
368
369 if (sort != null) {
370 searchContext.setSorts(new Sort[] {sort});
371 }
372
373 searchContext.setStart(start);
374 searchContext.setUserId(userId);
375
376 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
377 TrashEntry.class);
378
379 return indexer.search(searchContext);
380 }
381 catch (Exception e) {
382 throw new SystemException(e);
383 }
384 }
385
386 protected void checkEntriesAttachments(Group group)
387 throws PortalException, SystemException {
388
389 Date date = getMaxAge(group);
390
391 for (TrashHandler trashHandler :
392 TrashHandlerRegistryUtil.getTrashHandlers()) {
393
394 trashHandler.deleteTrashAttachments(group, date);
395 }
396 }
397
398 protected Date getMaxAge(Group group)
399 throws PortalException, SystemException {
400
401 Calendar calendar = Calendar.getInstance();
402
403 calendar.setTime(new Date());
404
405 int maxAge = TrashUtil.getMaxAge(group);
406
407 calendar.add(Calendar.DATE, -maxAge);
408
409 return calendar.getTime();
410 }
411
412 }