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            /**
282             * Returns the number of trash entries with the group ID.
283             *
284             * @param  groupId the primary key of the group
285             * @return the number of matching trash entries
286             * @throws SystemException if a system exception occurred
287             */
288            @Override
289            public int getEntriesCount(long groupId) throws SystemException {
290                    return trashEntryPersistence.countByGroupId(groupId);
291            }
292    
293            /**
294             * Returns the trash entry with the primary key.
295             *
296             * @param  entryId the primary key of the trash entry
297             * @return the trash entry with the primary key
298             * @throws PortalException if a trash entry with the primary key could not
299             *         be found
300             * @throws SystemException if a system exception occurred
301             */
302            @Override
303            public TrashEntry getEntry(long entryId)
304                    throws PortalException, SystemException {
305    
306                    return trashEntryPersistence.findByPrimaryKey(entryId);
307            }
308    
309            /**
310             * Returns the entry with the entity class name and primary key.
311             *
312             * @param  className the class name of the entity
313             * @param  classPK the primary key of the entity
314             * @return the trash entry with the entity class name and primary key
315             * @throws PortalException if a trash entry with the primary key could not
316             *         be found
317             * @throws SystemException if a system exception occurred
318             */
319            @Override
320            public TrashEntry getEntry(String className, long classPK)
321                    throws PortalException, SystemException {
322    
323                    long classNameId = PortalUtil.getClassNameId(className);
324    
325                    return trashEntryPersistence.findByC_C(classNameId, classPK);
326            }
327    
328            /**
329             * Returns all the trash versions associated with the trash entry.
330             *
331             * @param  entryId the primary key of the trash entry
332             * @return all the trash versions associated with the trash entry
333             * @throws SystemException if a system exception occurred
334             */
335            @Override
336            public List<TrashVersion> getVersions(long entryId) throws SystemException {
337                    return trashVersionPersistence.findByEntryId(entryId);
338            }
339    
340            /**
341             * Returns all the trash versions associated with the trash entry.
342             *
343             * @param  className the class name of the trash entity
344             * @param  classPK the primary key of the trash entity
345             * @return all the trash versions associated with the trash entry
346             * @throws SystemException if a system exception occurred
347             */
348            @Override
349            public List<TrashVersion> getVersions(String className, long classPK)
350                    throws SystemException {
351    
352                    long classNameId = PortalUtil.getClassNameId(className);
353    
354                    return trashVersionPersistence.findByC_C(classNameId, classPK);
355            }
356    
357            @Override
358            public Hits search(
359                            long companyId, long groupId, long userId, String keywords,
360                            int start, int end, Sort sort)
361                    throws SystemException {
362    
363                    try {
364                            SearchContext searchContext = new SearchContext();
365    
366                            searchContext.setCompanyId(companyId);
367                            searchContext.setEnd(end);
368                            searchContext.setKeywords(keywords);
369                            searchContext.setGroupIds(new long[] {groupId});
370    
371                            if (sort != null) {
372                                    searchContext.setSorts(sort);
373                            }
374    
375                            searchContext.setStart(start);
376                            searchContext.setUserId(userId);
377    
378                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
379                                    TrashEntry.class);
380    
381                            return indexer.search(searchContext);
382                    }
383                    catch (Exception e) {
384                            throw new SystemException(e);
385                    }
386            }
387    
388            protected Date getMaxAge(Group group)
389                    throws PortalException, SystemException {
390    
391                    Calendar calendar = Calendar.getInstance();
392    
393                    calendar.setTime(new Date());
394    
395                    int maxAge = TrashUtil.getMaxAge(group);
396    
397                    calendar.add(Calendar.MINUTE, -maxAge);
398    
399                    return calendar.getTime();
400            }
401    
402    }