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.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    /**
046     * Provides the local service for accessing, adding, checking, and deleting
047     * trash entries in the Recycle Bin.
048     *
049     * @author Zsolt Berentey
050     */
051    public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
052    
053            /**
054             * Moves an entry to trash.
055             *
056             * @param  userId the primary key of the user removing the entity
057             * @param  groupId the primary key of the entry's group
058             * @param  className the class name of the entity
059             * @param  classPK the primary key of the entity
060             * @param  status the status of the entity prior to being moved to trash
061             * @param  statusOVPs the primary keys and statuses of any of the entry's
062             *         versions (e.g., {@link
063             *         com.liferay.portlet.documentlibrary.model.DLFileVersion})
064             * @param  typeSettingsProperties the type settings properties
065             * @return the trashEntry
066             * @throws PortalException if a user with the primary key could not be found
067             * @throws SystemException if a system exception occurred
068             */
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            /**
149             * Deletes the trash entry with the primary key.
150             *
151             * @param  entryId the primary key of the trash entry
152             * @return the trash entry with the primary key
153             * @throws PortalException if a trash entry with the primary key could not
154             *         be found
155             * @throws SystemException if a system exception occurred
156             */
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            /**
167             * Deletes the trash entry with the entity class name and primary key.
168             *
169             * @param  className the class name of entity
170             * @param  classPK the primary key of the entry
171             * @return the trash entry with the entity class name and primary key
172             * @throws PortalException if a trash entry with the primary key could not
173             *         be found
174             * @throws SystemException if a system exception occurred
175             */
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            /**
203             * Returns the trash entry with the primary key.
204             *
205             * @param  entryId the primary key of the entry
206             * @return the trash entry with the primary key
207             * @throws SystemException if a system exception occurred
208             */
209            @Override
210            public TrashEntry fetchEntry(long entryId) throws SystemException {
211                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
212            }
213    
214            /**
215             * Returns the trash entry with the entity class name and primary key.
216             *
217             * @param  className the class name of the entity
218             * @param  classPK the primary key of the entity
219             * @return the trash entry with the entity class name and primary key
220             * @throws SystemException if a system exception occurred
221             */
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            /**
232             * Returns the trash entries with the matching group ID.
233             *
234             * @param  groupId the primary key of the group
235             * @return the trash entries with the group ID
236             * @throws SystemException if a system exception occurred
237             */
238            @Override
239            public List<TrashEntry> getEntries(long groupId) throws SystemException {
240                    return trashEntryPersistence.findByGroupId(groupId);
241            }
242    
243            /**
244             * Returns a range of all the trash entries matching the group ID.
245             *
246             * @param  groupId the primary key of the group
247             * @param  start the lower bound of the range of trash entries to return
248             * @param  end the upper bound of the range of trash entries to return (not
249             *         inclusive)
250             * @return the range of matching trash entries
251             * @throws SystemException if a system exception occurred
252             */
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            /**
261             * Returns a range of all the trash entries matching the group ID.
262             *
263             * @param  groupId the primary key of the group
264             * @param  start the lower bound of the range of trash entries to return
265             * @param  end the upper bound of the range of trash entries to return (not
266             *         inclusive)
267             * @param  obc the comparator to order the trash entries (optionally
268             *         <code>null</code>)
269             * @return the range of matching trash entries ordered by comparator
270             *         <code>obc</code>
271             * @throws SystemException if a system exception occurred
272             */
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            /**
291             * Returns the number of trash entries with the group ID.
292             *
293             * @param  groupId the primary key of the group
294             * @return the number of matching trash entries
295             * @throws SystemException if a system exception occurred
296             */
297            @Override
298            public int getEntriesCount(long groupId) throws SystemException {
299                    return trashEntryPersistence.countByGroupId(groupId);
300            }
301    
302            /**
303             * Returns the trash entry with the primary key.
304             *
305             * @param  entryId the primary key of the trash entry
306             * @return the trash entry with the primary key
307             * @throws PortalException if a trash entry with the primary key could not
308             *         be found
309             * @throws SystemException if a system exception occurred
310             */
311            @Override
312            public TrashEntry getEntry(long entryId)
313                    throws PortalException, SystemException {
314    
315                    return trashEntryPersistence.findByPrimaryKey(entryId);
316            }
317    
318            /**
319             * Returns the entry with the entity class name and primary key.
320             *
321             * @param  className the class name of the entity
322             * @param  classPK the primary key of the entity
323             * @return the trash entry with the entity class name and primary key
324             * @throws PortalException if a trash entry with the primary key could not
325             *         be found
326             * @throws SystemException if a system exception occurred
327             */
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    }