001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                                            if (trashHandler != null) {
157                                                    trashHandler.deleteTrashEntry(trashEntry.getClassPK());
158                                            }
159                                    }
160                            }
161    
162                    };
163    
164                    actionableDynamicQuery.performActions();
165            }
166    
167            @Override
168            public void deleteEntries(long groupId) throws SystemException {
169                    List<TrashEntry> entries = getEntries(groupId);
170    
171                    for (TrashEntry entry : entries) {
172                            deleteEntry(entry);
173                    }
174            }
175    
176            /**
177             * Deletes the trash entry with the primary key.
178             *
179             * @param  entryId the primary key of the trash entry
180             * @return the trash entry with the 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(long entryId)
187                    throws PortalException, SystemException {
188    
189                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
190    
191                    return deleteEntry(entry);
192            }
193    
194            /**
195             * Deletes the trash entry with the entity class name and primary key.
196             *
197             * @param  className the class name of entity
198             * @param  classPK the primary key of the entry
199             * @return the trash entry with the entity class name and primary key
200             * @throws PortalException if a trash entry with the primary key could not
201             *         be found
202             * @throws SystemException if a system exception occurred
203             */
204            @Override
205            public TrashEntry deleteEntry(String className, long classPK)
206                    throws PortalException, SystemException {
207    
208                    long classNameId = PortalUtil.getClassNameId(className);
209    
210                    TrashEntry entry = trashEntryPersistence.fetchByC_C(
211                            classNameId, classPK);
212    
213                    return deleteEntry(entry);
214            }
215    
216            @Indexable(type = IndexableType.DELETE)
217            @Override
218            public TrashEntry deleteEntry(TrashEntry trashEntry)
219                    throws SystemException {
220    
221                    if (trashEntry != null) {
222                            trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
223    
224                            trashEntry = trashEntryPersistence.remove(trashEntry);
225    
226                            systemEventLocalService.deleteSystemEvents(
227                                    trashEntry.getGroupId(), trashEntry.getSystemEventSetKey());
228                    }
229    
230                    return trashEntry;
231            }
232    
233            /**
234             * Returns the trash entry with the primary key.
235             *
236             * @param  entryId the primary key of the entry
237             * @return the trash entry with the primary key
238             * @throws SystemException if a system exception occurred
239             */
240            @Override
241            public TrashEntry fetchEntry(long entryId) throws SystemException {
242                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
243            }
244    
245            /**
246             * Returns the trash entry with the entity class name and primary key.
247             *
248             * @param  className the class name of the entity
249             * @param  classPK the primary key of the entity
250             * @return the trash entry with the entity class name and primary key
251             * @throws SystemException if a system exception occurred
252             */
253            @Override
254            public TrashEntry fetchEntry(String className, long classPK)
255                    throws SystemException {
256    
257                    long classNameId = PortalUtil.getClassNameId(className);
258    
259                    return trashEntryPersistence.fetchByC_C(classNameId, classPK);
260            }
261    
262            /**
263             * Returns the trash entries with the matching group ID.
264             *
265             * @param  groupId the primary key of the group
266             * @return the trash entries with the group ID
267             * @throws SystemException if a system exception occurred
268             */
269            @Override
270            public List<TrashEntry> getEntries(long groupId) throws SystemException {
271                    return trashEntryPersistence.findByGroupId(groupId);
272            }
273    
274            /**
275             * Returns a range of all the trash entries matching the group ID.
276             *
277             * @param  groupId the primary key of the group
278             * @param  start the lower bound of the range of trash entries to return
279             * @param  end the upper bound of the range of trash entries to return (not
280             *         inclusive)
281             * @return the range of matching trash entries
282             * @throws SystemException if a system exception occurred
283             */
284            @Override
285            public List<TrashEntry> getEntries(long groupId, int start, int end)
286                    throws SystemException {
287    
288                    return trashEntryPersistence.findByGroupId(groupId, start, end);
289            }
290    
291            /**
292             * Returns a range of all the trash entries matching the group ID.
293             *
294             * @param  groupId the primary key of the group
295             * @param  start the lower bound of the range of trash entries to return
296             * @param  end the upper bound of the range of trash entries to return (not
297             *         inclusive)
298             * @param  obc the comparator to order the trash entries (optionally
299             *         <code>null</code>)
300             * @return the range of matching trash entries ordered by comparator
301             *         <code>obc</code>
302             * @throws SystemException if a system exception occurred
303             */
304            @Override
305            public List<TrashEntry> getEntries(
306                            long groupId, int start, int end, OrderByComparator obc)
307                    throws SystemException {
308    
309                    return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
310            }
311    
312            @Override
313            public List<TrashEntry> getEntries(long groupId, String className)
314                    throws SystemException {
315    
316                    long classNameId = PortalUtil.getClassNameId(className);
317    
318                    return trashEntryPersistence.findByG_C(groupId, classNameId);
319            }
320    
321            /**
322             * Returns the number of trash entries with the group ID.
323             *
324             * @param  groupId the primary key of the group
325             * @return the number of matching trash entries
326             * @throws SystemException if a system exception occurred
327             */
328            @Override
329            public int getEntriesCount(long groupId) throws SystemException {
330                    return trashEntryPersistence.countByGroupId(groupId);
331            }
332    
333            /**
334             * Returns the trash entry with the primary key.
335             *
336             * @param  entryId the primary key of the trash entry
337             * @return the trash entry with the primary key
338             * @throws PortalException if a trash entry with the primary key could not
339             *         be found
340             * @throws SystemException if a system exception occurred
341             */
342            @Override
343            public TrashEntry getEntry(long entryId)
344                    throws PortalException, SystemException {
345    
346                    return trashEntryPersistence.findByPrimaryKey(entryId);
347            }
348    
349            /**
350             * Returns the entry with the entity class name and primary key.
351             *
352             * @param  className the class name of the entity
353             * @param  classPK the primary key of the entity
354             * @return the trash entry with the entity class name and primary key
355             * @throws PortalException if a trash entry with the primary key could not
356             *         be found
357             * @throws SystemException if a system exception occurred
358             */
359            @Override
360            public TrashEntry getEntry(String className, long classPK)
361                    throws PortalException, SystemException {
362    
363                    long classNameId = PortalUtil.getClassNameId(className);
364    
365                    return trashEntryPersistence.findByC_C(classNameId, classPK);
366            }
367    
368            @Override
369            public Hits search(
370                            long companyId, long groupId, long userId, String keywords,
371                            int start, int end, Sort sort)
372                    throws SystemException {
373    
374                    try {
375                            SearchContext searchContext = new SearchContext();
376    
377                            searchContext.setCompanyId(companyId);
378                            searchContext.setEnd(end);
379                            searchContext.setKeywords(keywords);
380                            searchContext.setGroupIds(new long[] {groupId});
381    
382                            if (sort != null) {
383                                    searchContext.setSorts(sort);
384                            }
385    
386                            searchContext.setStart(start);
387                            searchContext.setUserId(userId);
388    
389                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
390                                    TrashEntry.class);
391    
392                            return indexer.search(searchContext);
393                    }
394                    catch (Exception e) {
395                            throw new SystemException(e);
396                    }
397            }
398    
399            protected Date getMaxAge(Group group)
400                    throws PortalException, SystemException {
401    
402                    Calendar calendar = Calendar.getInstance();
403    
404                    calendar.setTime(new Date());
405    
406                    int maxAge = TrashUtil.getMaxAge(group);
407    
408                    calendar.add(Calendar.MINUTE, -maxAge);
409    
410                    return calendar.getTime();
411            }
412    
413    }