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 @Override
282 public List<TrashEntry> getEntries(long groupId, String className)
283 throws SystemException {
284
285 long classNameId = PortalUtil.getClassNameId(className);
286
287 return trashEntryPersistence.findByG_C(groupId, classNameId);
288 }
289
290
297 @Override
298 public int getEntriesCount(long groupId) throws SystemException {
299 return trashEntryPersistence.countByGroupId(groupId);
300 }
301
302
311 @Override
312 public TrashEntry getEntry(long entryId)
313 throws PortalException, SystemException {
314
315 return trashEntryPersistence.findByPrimaryKey(entryId);
316 }
317
318
328 @Override
329 public TrashEntry getEntry(String className, long classPK)
330 throws PortalException, SystemException {
331
332 long classNameId = PortalUtil.getClassNameId(className);
333
334 return trashEntryPersistence.findByC_C(classNameId, classPK);
335 }
336
337 @Override
338 public Hits search(
339 long companyId, long groupId, long userId, String keywords,
340 int start, int end, Sort sort)
341 throws SystemException {
342
343 try {
344 SearchContext searchContext = new SearchContext();
345
346 searchContext.setCompanyId(companyId);
347 searchContext.setEnd(end);
348 searchContext.setKeywords(keywords);
349 searchContext.setGroupIds(new long[] {groupId});
350
351 if (sort != null) {
352 searchContext.setSorts(sort);
353 }
354
355 searchContext.setStart(start);
356 searchContext.setUserId(userId);
357
358 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
359 TrashEntry.class);
360
361 return indexer.search(searchContext);
362 }
363 catch (Exception e) {
364 throw new SystemException(e);
365 }
366 }
367
368 protected Date getMaxAge(Group group)
369 throws PortalException, SystemException {
370
371 Calendar calendar = Calendar.getInstance();
372
373 calendar.setTime(new Date());
374
375 int maxAge = TrashUtil.getMaxAge(group);
376
377 calendar.add(Calendar.MINUTE, -maxAge);
378
379 return calendar.getTime();
380 }
381
382 }