001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.service.persistence.GroupActionableDynamicQuery;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.trash.model.TrashEntry;
038    import com.liferay.portlet.trash.model.TrashVersion;
039    import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
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    /**
047     * Provides the local service for accessing, adding, checking, and deleting
048     * trash entries in the Recycle Bin.
049     *
050     * @author Zsolt Berentey
051     */
052    public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
053    
054            /**
055             * Moves an entry to trash.
056             *
057             * @param  userId the primary key of the user removing the entity
058             * @param  groupId the primary key of the entry's group
059             * @param  className the class name of the entity
060             * @param  classPK the primary key of the entity
061             * @param  status the status of the entity prior to being moved to trash
062             * @param  statusOVPs the primary keys and statuses of any of the entry's
063             *         versions (e.g., {@link
064             *         com.liferay.portlet.documentlibrary.model.DLFileVersion})
065             * @param  typeSettingsProperties the type settings properties
066             * @return the trashEntry
067             * @throws PortalException if a user with the primary key could not be found
068             * @throws SystemException if a system exception occurred
069             */
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 GroupActionableDynamicQuery() {
131    
132                            @Override
133                            protected void performAction(Object object)
134                                    throws PortalException, SystemException {
135    
136                                    Group group = (Group)object;
137    
138                                    Date date = getMaxAge(group);
139    
140                                    List<TrashEntry> entries = trashEntryPersistence.findByG_LtCD(
141                                            group.getGroupId(), date);
142    
143                                    for (TrashEntry entry : entries) {
144                                            TrashHandler trashHandler =
145                                                    TrashHandlerRegistryUtil.getTrashHandler(
146                                                            entry.getClassName());
147    
148                                            trashHandler.deleteTrashEntry(entry.getClassPK());
149                                    }
150                            }
151    
152                    };
153    
154                    actionableDynamicQuery.performActions();
155            }
156    
157            /**
158             * Deletes the trash entry with the primary key.
159             *
160             * @param  entryId the primary key of the trash entry
161             * @return the trash entry with the primary key
162             * @throws PortalException if a trash entry with the primary key could not
163             *         be found
164             * @throws SystemException if a system exception occurred
165             */
166            @Override
167            public TrashEntry deleteEntry(long entryId)
168                    throws PortalException, SystemException {
169    
170                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
171    
172                    return deleteEntry(entry);
173            }
174    
175            /**
176             * Deletes the trash entry with the entity class name and primary key.
177             *
178             * @param  className the class name of entity
179             * @param  classPK the primary key of the entry
180             * @return the trash entry with the entity class name and primary key
181             * @throws PortalException if a trash entry with the primary key could not
182             *         be found
183             * @throws SystemException if a system exception occurred
184             */
185            @Override
186            public TrashEntry deleteEntry(String className, long classPK)
187                    throws PortalException, SystemException {
188    
189                    long classNameId = PortalUtil.getClassNameId(className);
190    
191                    TrashEntry entry = trashEntryPersistence.fetchByC_C(
192                            classNameId, classPK);
193    
194                    return deleteEntry(entry);
195            }
196    
197            @Indexable(type = IndexableType.DELETE)
198            @Override
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                            systemEventLocalService.deleteSystemEvents(
208                                    trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
209                    }
210    
211                    return trashEntry;
212            }
213    
214            /**
215             * Returns the trash entry with the primary key.
216             *
217             * @param  entryId the primary key of the entry
218             * @return the trash entry with the primary key
219             * @throws SystemException if a system exception occurred
220             */
221            @Override
222            public TrashEntry fetchEntry(long entryId) throws SystemException {
223                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
224            }
225    
226            /**
227             * Returns the trash entry with the entity class name and primary key.
228             *
229             * @param  className the class name of the entity
230             * @param  classPK the primary key of the entity
231             * @return the trash entry with the entity class name and primary key
232             * @throws SystemException if a system exception occurred
233             */
234            @Override
235            public TrashEntry fetchEntry(String className, long classPK)
236                    throws SystemException {
237    
238                    long classNameId = PortalUtil.getClassNameId(className);
239    
240                    return trashEntryPersistence.fetchByC_C(classNameId, classPK);
241            }
242    
243            /**
244             * Returns the trash entries with the matching group ID.
245             *
246             * @param  groupId the primary key of the group
247             * @return the trash entries with the group ID
248             * @throws SystemException if a system exception occurred
249             */
250            @Override
251            public List<TrashEntry> getEntries(long groupId) throws SystemException {
252                    return trashEntryPersistence.findByGroupId(groupId);
253            }
254    
255            /**
256             * Returns a range of all the trash entries matching the group ID.
257             *
258             * @param  groupId the primary key of the group
259             * @param  start the lower bound of the range of trash entries to return
260             * @param  end the upper bound of the range of trash entries to return (not
261             *         inclusive)
262             * @return the range of matching trash entries
263             * @throws SystemException if a system exception occurred
264             */
265            @Override
266            public List<TrashEntry> getEntries(long groupId, int start, int end)
267                    throws SystemException {
268    
269                    return trashEntryPersistence.findByGroupId(groupId, start, end);
270            }
271    
272            /**
273             * Returns a range of all the trash entries matching the group ID.
274             *
275             * @param  groupId the primary key of the group
276             * @param  start the lower bound of the range of trash entries to return
277             * @param  end the upper bound of the range of trash entries to return (not
278             *         inclusive)
279             * @param  obc the comparator to order the trash entries (optionally
280             *         <code>null</code>)
281             * @return the range of matching trash entries ordered by comparator
282             *         <code>obc</code>
283             * @throws SystemException if a system exception occurred
284             */
285            @Override
286            public List<TrashEntry> getEntries(
287                            long groupId, int start, int end, OrderByComparator obc)
288                    throws SystemException {
289    
290                    return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
291            }
292    
293            @Override
294            public List<TrashEntry> getEntries(long groupId, String className)
295                    throws SystemException {
296    
297                    long classNameId = PortalUtil.getClassNameId(className);
298    
299                    return trashEntryPersistence.findByG_C(groupId, classNameId);
300            }
301    
302            /**
303             * Returns the number of trash entries with the group ID.
304             *
305             * @param  groupId the primary key of the group
306             * @return the number of matching trash entries
307             * @throws SystemException if a system exception occurred
308             */
309            @Override
310            public int getEntriesCount(long groupId) throws SystemException {
311                    return trashEntryPersistence.countByGroupId(groupId);
312            }
313    
314            /**
315             * Returns the trash entry with the primary key.
316             *
317             * @param  entryId the primary key of the trash entry
318             * @return the trash entry with the primary key
319             * @throws PortalException if a trash entry with the primary key could not
320             *         be found
321             * @throws SystemException if a system exception occurred
322             */
323            @Override
324            public TrashEntry getEntry(long entryId)
325                    throws PortalException, SystemException {
326    
327                    return trashEntryPersistence.findByPrimaryKey(entryId);
328            }
329    
330            /**
331             * Returns the entry with the entity class name and primary key.
332             *
333             * @param  className the class name of the entity
334             * @param  classPK the primary key of the entity
335             * @return the trash entry with the entity class name and primary key
336             * @throws PortalException if a trash entry with the primary key could not
337             *         be found
338             * @throws SystemException if a system exception occurred
339             */
340            @Override
341            public TrashEntry getEntry(String className, long classPK)
342                    throws PortalException, SystemException {
343    
344                    long classNameId = PortalUtil.getClassNameId(className);
345    
346                    return trashEntryPersistence.findByC_C(classNameId, classPK);
347            }
348    
349            @Override
350            public Hits search(
351                            long companyId, long groupId, long userId, String keywords,
352                            int start, int end, Sort sort)
353                    throws SystemException {
354    
355                    try {
356                            SearchContext searchContext = new SearchContext();
357    
358                            searchContext.setCompanyId(companyId);
359                            searchContext.setEnd(end);
360                            searchContext.setKeywords(keywords);
361                            searchContext.setGroupIds(new long[] {groupId});
362    
363                            if (sort != null) {
364                                    searchContext.setSorts(sort);
365                            }
366    
367                            searchContext.setStart(start);
368                            searchContext.setUserId(userId);
369    
370                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
371                                    TrashEntry.class);
372    
373                            return indexer.search(searchContext);
374                    }
375                    catch (Exception e) {
376                            throw new SystemException(e);
377                    }
378            }
379    
380            protected Date getMaxAge(Group group)
381                    throws PortalException, SystemException {
382    
383                    Calendar calendar = Calendar.getInstance();
384    
385                    calendar.setTime(new Date());
386    
387                    int maxAge = TrashUtil.getMaxAge(group);
388    
389                    calendar.add(Calendar.MINUTE, -maxAge);
390    
391                    return calendar.getTime();
392            }
393    
394    }