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.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.service.persistence.TrashEntryActionableDynamicQuery;
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 TrashEntryActionableDynamicQuery() {
131    
132                            @Override
133                            protected void performAction(Object object)
134                                    throws PortalException, SystemException {
135    
136                                    TrashEntry trashEntry = (TrashEntry)object;
137    
138                                    Date createDate = trashEntry.getCreateDate();
139    
140                                    Group group = groupPersistence.fetchByPrimaryKey(
141                                            trashEntry.getGroupId());
142    
143                                    if (group == null) {
144                                            return;
145                                    }
146    
147                                    Date date = getMaxAge(group);
148    
149                                    if (createDate.before(date) ||
150                                            !TrashUtil.isTrashEnabled(group.getGroupId())) {
151    
152                                            TrashHandler trashHandler =
153                                                    TrashHandlerRegistryUtil.getTrashHandler(
154                                                            trashEntry.getClassName());
155    
156                                            trashHandler.deleteTrashEntry(trashEntry.getClassPK());
157                                    }
158                            }
159    
160                    };
161    
162                    actionableDynamicQuery.performActions();
163            }
164    
165            @Override
166            public void deleteEntries(long groupId) throws SystemException {
167                    List<TrashEntry> entries = getEntries(groupId);
168    
169                    for (TrashEntry entry : entries) {
170                            deleteEntry(entry);
171                    }
172            }
173    
174            /**
175             * Deletes the trash entry with the primary key.
176             *
177             * @param  entryId the primary key of the trash entry
178             * @return the trash entry with the primary key
179             * @throws PortalException if a trash entry with the primary key could not
180             *         be found
181             * @throws SystemException if a system exception occurred
182             */
183            @Override
184            public TrashEntry deleteEntry(long entryId)
185                    throws PortalException, SystemException {
186    
187                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
188    
189                    return deleteEntry(entry);
190            }
191    
192            /**
193             * Deletes the trash entry with the entity class name and primary key.
194             *
195             * @param  className the class name of entity
196             * @param  classPK the primary key of the entry
197             * @return the trash entry with the entity class name and primary key
198             * @throws PortalException if a trash entry with the primary key could not
199             *         be found
200             * @throws SystemException if a system exception occurred
201             */
202            @Override
203            public TrashEntry deleteEntry(String className, long classPK)
204                    throws PortalException, SystemException {
205    
206                    long classNameId = PortalUtil.getClassNameId(className);
207    
208                    TrashEntry entry = trashEntryPersistence.fetchByC_C(
209                            classNameId, classPK);
210    
211                    return deleteEntry(entry);
212            }
213    
214            @Indexable(type = IndexableType.DELETE)
215            @Override
216            public TrashEntry deleteEntry(TrashEntry trashEntry)
217                    throws SystemException {
218    
219                    if (trashEntry != null) {
220                            trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
221    
222                            trashEntry = trashEntryPersistence.remove(trashEntry);
223    
224                            systemEventLocalService.deleteSystemEvents(
225                                    trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
226                    }
227    
228                    return trashEntry;
229            }
230    
231            /**
232             * Returns the trash entry with the primary key.
233             *
234             * @param  entryId the primary key of the entry
235             * @return the trash entry with the primary key
236             * @throws SystemException if a system exception occurred
237             */
238            @Override
239            public TrashEntry fetchEntry(long entryId) throws SystemException {
240                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
241            }
242    
243            /**
244             * Returns the trash entry with the entity class name and primary key.
245             *
246             * @param  className the class name of the entity
247             * @param  classPK the primary key of the entity
248             * @return the trash entry with the entity class name and primary key
249             * @throws SystemException if a system exception occurred
250             */
251            @Override
252            public TrashEntry fetchEntry(String className, long classPK)
253                    throws SystemException {
254    
255                    long classNameId = PortalUtil.getClassNameId(className);
256    
257                    return trashEntryPersistence.fetchByC_C(classNameId, classPK);
258            }
259    
260            /**
261             * Returns the trash entries with the matching group ID.
262             *
263             * @param  groupId the primary key of the group
264             * @return the trash entries with the group ID
265             * @throws SystemException if a system exception occurred
266             */
267            @Override
268            public List<TrashEntry> getEntries(long groupId) throws SystemException {
269                    return trashEntryPersistence.findByGroupId(groupId);
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             * @return the range of matching trash entries
280             * @throws SystemException if a system exception occurred
281             */
282            @Override
283            public List<TrashEntry> getEntries(long groupId, int start, int end)
284                    throws SystemException {
285    
286                    return trashEntryPersistence.findByGroupId(groupId, start, end);
287            }
288    
289            /**
290             * Returns a range of all the trash entries matching the group ID.
291             *
292             * @param  groupId the primary key of the group
293             * @param  start the lower bound of the range of trash entries to return
294             * @param  end the upper bound of the range of trash entries to return (not
295             *         inclusive)
296             * @param  obc the comparator to order the trash entries (optionally
297             *         <code>null</code>)
298             * @return the range of matching trash entries ordered by comparator
299             *         <code>obc</code>
300             * @throws SystemException if a system exception occurred
301             */
302            @Override
303            public List<TrashEntry> getEntries(
304                            long groupId, int start, int end, OrderByComparator obc)
305                    throws SystemException {
306    
307                    return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
308            }
309    
310            @Override
311            public List<TrashEntry> getEntries(long groupId, String className)
312                    throws SystemException {
313    
314                    long classNameId = PortalUtil.getClassNameId(className);
315    
316                    return trashEntryPersistence.findByG_C(groupId, classNameId);
317            }
318    
319            /**
320             * Returns the number of trash entries with the group ID.
321             *
322             * @param  groupId the primary key of the group
323             * @return the number of matching trash entries
324             * @throws SystemException if a system exception occurred
325             */
326            @Override
327            public int getEntriesCount(long groupId) throws SystemException {
328                    return trashEntryPersistence.countByGroupId(groupId);
329            }
330    
331            /**
332             * Returns the trash entry with the primary key.
333             *
334             * @param  entryId the primary key of the trash entry
335             * @return the trash entry with the 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(long entryId)
342                    throws PortalException, SystemException {
343    
344                    return trashEntryPersistence.findByPrimaryKey(entryId);
345            }
346    
347            /**
348             * Returns the entry with the entity class name and primary key.
349             *
350             * @param  className the class name of the entity
351             * @param  classPK the primary key of the entity
352             * @return the trash entry with the entity class name and primary key
353             * @throws PortalException if a trash entry with the primary key could not
354             *         be found
355             * @throws SystemException if a system exception occurred
356             */
357            @Override
358            public TrashEntry getEntry(String className, long classPK)
359                    throws PortalException, SystemException {
360    
361                    long classNameId = PortalUtil.getClassNameId(className);
362    
363                    return trashEntryPersistence.findByC_C(classNameId, classPK);
364            }
365    
366            @Override
367            public Hits search(
368                            long companyId, long groupId, long userId, String keywords,
369                            int start, int end, Sort sort)
370                    throws SystemException {
371    
372                    try {
373                            SearchContext searchContext = new SearchContext();
374    
375                            searchContext.setCompanyId(companyId);
376                            searchContext.setEnd(end);
377                            searchContext.setKeywords(keywords);
378                            searchContext.setGroupIds(new long[] {groupId});
379    
380                            if (sort != null) {
381                                    searchContext.setSorts(sort);
382                            }
383    
384                            searchContext.setStart(start);
385                            searchContext.setUserId(userId);
386    
387                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
388                                    TrashEntry.class);
389    
390                            return indexer.search(searchContext);
391                    }
392                    catch (Exception e) {
393                            throw new SystemException(e);
394                    }
395            }
396    
397            protected Date getMaxAge(Group group)
398                    throws PortalException, SystemException {
399    
400                    Calendar calendar = Calendar.getInstance();
401    
402                    calendar.setTime(new Date());
403    
404                    int maxAge = TrashUtil.getMaxAge(group);
405    
406                    calendar.add(Calendar.MINUTE, -maxAge);
407    
408                    return calendar.getTime();
409            }
410    
411    }