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