001    /**
002     * Copyright (c) 2000-2012 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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.search.Hits;
020    import com.liferay.portal.kernel.search.Indexable;
021    import com.liferay.portal.kernel.search.IndexableType;
022    import com.liferay.portal.kernel.search.Indexer;
023    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024    import com.liferay.portal.kernel.search.SearchContext;
025    import com.liferay.portal.kernel.search.Sort;
026    import com.liferay.portal.kernel.trash.TrashHandler;
027    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
028    import com.liferay.portal.kernel.util.ObjectValuePair;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.UnicodeProperties;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.trash.model.TrashEntry;
035    import com.liferay.portlet.trash.model.TrashVersion;
036    import com.liferay.portlet.trash.service.base.TrashEntryLocalServiceBaseImpl;
037    import com.liferay.portlet.trash.util.TrashUtil;
038    
039    import java.util.Calendar;
040    import java.util.Date;
041    import java.util.List;
042    
043    /**
044     * The trash local service is responsible for accessing, creating, modifying and
045     * deleting trash entries in the Recycle Bin.
046     *
047     * @author Zsolt Berentey
048     */
049    public class TrashEntryLocalServiceImpl extends TrashEntryLocalServiceBaseImpl {
050    
051            /**
052             * Moves an entry to trash.
053             *
054             * @param  userId the primary key of the user removing the entity
055             * @param  groupId the primary key of the entry's group
056             * @param  className the class name of the entity
057             * @param  classPK the primary key of the entity
058             * @param  status the status of the entity prior to being moved to trash
059             * @param  statusOVPs the primary keys and statuses of any of the entry's
060             *         versions (e.g., {@link
061             *         com.liferay.portlet.documentlibrary.model.DLFileVersion})
062             * @param  typeSettingsProperties the type settings properties
063             * @return the trashEntry
064             * @throws PortalException if a user with the primary key could not be found
065             * @throws SystemException if a system exception occurred
066             */
067            public TrashEntry addTrashEntry(
068                            long userId, long groupId, String className, long classPK,
069                            int status, List<ObjectValuePair<Long, Integer>> statusOVPs,
070                            UnicodeProperties typeSettingsProperties)
071                    throws PortalException, SystemException {
072    
073                    User user = userPersistence.findByPrimaryKey(userId);
074                    long classNameId = PortalUtil.getClassNameId(className);
075    
076                    long entryId = counterLocalService.increment();
077    
078                    TrashEntry trashEntry = trashEntryPersistence.create(entryId);
079    
080                    trashEntry.setGroupId(groupId);
081                    trashEntry.setCompanyId(user.getCompanyId());
082                    trashEntry.setUserId(user.getUserId());
083                    trashEntry.setUserName(user.getFullName());
084                    trashEntry.setCreateDate(new Date());
085                    trashEntry.setClassNameId(classNameId);
086                    trashEntry.setClassPK(classPK);
087    
088                    if (typeSettingsProperties != null) {
089                            trashEntry.setTypeSettingsProperties(typeSettingsProperties);
090                    }
091    
092                    trashEntry.setStatus(status);
093    
094                    trashEntryPersistence.update(trashEntry);
095    
096                    if (statusOVPs != null) {
097                            for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
098                                    long versionId = counterLocalService.increment();
099    
100                                    TrashVersion trashVersion = trashVersionPersistence.create(
101                                            versionId);
102    
103                                    trashVersion.setEntryId(entryId);
104                                    trashVersion.setClassNameId(classNameId);
105                                    trashVersion.setClassPK(statusOVP.getKey());
106                                    trashVersion.setStatus(statusOVP.getValue());
107    
108                                    trashVersionPersistence.update(trashVersion);
109                            }
110                    }
111    
112                    return trashEntry;
113            }
114    
115            public void checkEntries() throws PortalException, SystemException {
116                    int count = groupPersistence.countAll();
117    
118                    int pages = count / Indexer.DEFAULT_INTERVAL;
119    
120                    for (int i = 0; i <= pages; i++) {
121                            int start = (i * Indexer.DEFAULT_INTERVAL);
122                            int end = start + Indexer.DEFAULT_INTERVAL;
123    
124                            List<Group> groups = groupPersistence.findAll(start, end);
125    
126                            for (Group group : groups) {
127                                    Date date = getMaxAge(group);
128    
129                                    List<TrashEntry> entries = trashEntryPersistence.findByG_LtCD(
130                                            group.getGroupId(), date);
131    
132                                    for (TrashEntry entry : entries) {
133                                            TrashHandler trashHandler =
134                                                    TrashHandlerRegistryUtil.getTrashHandler(
135                                                            entry.getClassName());
136    
137                                            trashHandler.deleteTrashEntry(entry.getClassPK(), false);
138                                    }
139                            }
140                    }
141            }
142    
143            public void checkEntriesAttachments()
144                    throws PortalException, SystemException {
145    
146                    int count = groupPersistence.countAll();
147    
148                    int pages = count / Indexer.DEFAULT_INTERVAL;
149    
150                    for (int i = 0; i <= pages; i++) {
151                            int start = (i * Indexer.DEFAULT_INTERVAL);
152                            int end = start + Indexer.DEFAULT_INTERVAL;
153    
154                            List<Group> groups = groupPersistence.findAll(start, end);
155    
156                            for (Group group : groups) {
157                                    checkEntriesAttachments(group);
158                            }
159                    }
160            }
161    
162            /**
163             * Deletes the trash entry with the primary key.
164             *
165             * @param  entryId the primary key of the trash entry
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            public TrashEntry deleteEntry(long entryId)
171                    throws PortalException, SystemException {
172    
173                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
174    
175                    return deleteEntry(entry);
176            }
177    
178            /**
179             * Deletes the trash entry with the entity class name and primary key.
180             *
181             * @param  className the class name of entity
182             * @param  classPK the primary key of the entry
183             * @throws PortalException if a trash entry with the primary key could not
184             *         be found
185             * @throws SystemException if a system exception occurred
186             */
187            public TrashEntry deleteEntry(String className, long classPK)
188                    throws PortalException, SystemException {
189    
190                    long classNameId = PortalUtil.getClassNameId(className);
191    
192                    TrashEntry entry = trashEntryPersistence.fetchByC_C(
193                            classNameId, classPK);
194    
195                    return deleteEntry(entry);
196            }
197    
198            @Indexable(type = IndexableType.DELETE)
199            public TrashEntry deleteEntry(TrashEntry trashEntry)
200                    throws SystemException {
201    
202                    if (trashEntry != null) {
203                            trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
204    
205                            trashEntry = trashEntryPersistence.remove(trashEntry);
206                    }
207    
208                    return trashEntry;
209            }
210    
211            /**
212             * Returns the trash entry with the primary key.
213             *
214             * @param  entryId the primary key of the entry
215             * @return the trash entry with the primary key
216             * @throws SystemException if a system exception occurred
217             */
218            public TrashEntry fetchEntry(long entryId) throws SystemException {
219                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
220            }
221    
222            /**
223             * Returns the trash entry with the entity class name and primary key.
224             *
225             * @param  className the class name of the entity
226             * @param  classPK the primary key of the entity
227             * @return the trash entry with the entity class name and primary key
228             * @throws SystemException if a system exception occurred
229             */
230            public TrashEntry fetchEntry(String className, long classPK)
231                    throws SystemException {
232    
233                    long classNameId = PortalUtil.getClassNameId(className);
234    
235                    return trashEntryPersistence.fetchByC_C(classNameId, classPK);
236            }
237    
238            /**
239             * Returns the trash entries with the matching group ID.
240             *
241             * @param  groupId the primary key of the group
242             * @return the trash entries with the group ID
243             * @throws SystemException if a system exception occurred
244             */
245            public List<TrashEntry> getEntries(long groupId) throws SystemException {
246                    return trashEntryPersistence.findByGroupId(groupId);
247            }
248    
249            /**
250             * Returns a range of all the trash entries matching the group ID.
251             *
252             * @param  groupId the primary key of the group
253             * @param  start the lower bound of the range of trash entries to return
254             * @param  end the upper bound of the range of trash entries to return (not
255             *         inclusive)
256             * @return the range of matching trash entries
257             * @throws SystemException if a system exception occurred
258             */
259            public List<TrashEntry> getEntries(long groupId, int start, int end)
260                    throws SystemException {
261    
262                    return trashEntryPersistence.findByGroupId(groupId, start, end);
263            }
264    
265            /**
266             * Returns a range of all the trash entries matching the group ID.
267             *
268             * @param  groupId the primary key of the group
269             * @param  start the lower bound of the range of trash entries to return
270             * @param  end the upper bound of the range of trash entries to return (not
271             *         inclusive)
272             * @param  obc the comparator to order the trash entries (optionally
273             *         <code>null</code>)
274             * @return the range of matching trash entries ordered by comparator
275             *         <code>obc</code>
276             * @throws SystemException if a system exception occurred
277             */
278            public List<TrashEntry> getEntries(
279                            long groupId, int start, int end, OrderByComparator obc)
280                    throws SystemException {
281    
282                    return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
283            }
284    
285            /**
286             * Returns the number of trash entries with the group ID.
287             *
288             * @param  groupId the primary key of the group
289             * @return the number of matching trash entries
290             * @throws SystemException if a system exception occurred
291             */
292            public int getEntriesCount(long groupId) throws SystemException {
293                    return trashEntryPersistence.countByGroupId(groupId);
294            }
295    
296            /**
297             * Returns the trash entry with the primary key.
298             *
299             * @param  entryId the primary key of the trash entry
300             * @return the trash entry with the primary key
301             * @throws PortalException if a trash entry with the primary key could not
302             *         be found
303             * @throws SystemException if a system exception occurred
304             */
305            public TrashEntry getEntry(long entryId)
306                    throws PortalException, SystemException {
307    
308                    return trashEntryPersistence.findByPrimaryKey(entryId);
309            }
310    
311            /**
312             * Returns the entry with the entity class name and primary key.
313             *
314             * @param  className the class name of the entity
315             * @param  classPK the primary key of the entity
316             * @return the trash entry with the entity class name and primary key
317             * @throws PortalException if a trash entry with the primary key could not
318             *         be found
319             * @throws SystemException if a system exception occurred
320             */
321            public TrashEntry getEntry(String className, long classPK)
322                    throws PortalException, SystemException {
323    
324                    long classNameId = PortalUtil.getClassNameId(className);
325    
326                    return trashEntryPersistence.findByC_C(classNameId, classPK);
327            }
328    
329            /**
330             * Returns all the trash versions associated with the trash entry.
331             *
332             * @param  entryId the primary key of the trash entry
333             * @return all the trash versions associated with the trash entry
334             * @throws SystemException if a system exception occurred
335             */
336            public List<TrashVersion> getVersions(long entryId) throws SystemException {
337                    return trashEntryPersistence.getTrashVersions(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            public List<TrashVersion> getVersions(String className, long classPK)
349                    throws SystemException {
350    
351                    long classNameId = PortalUtil.getClassNameId(className);
352    
353                    return trashVersionPersistence.findByC_C(classNameId, classPK);
354            }
355    
356            public Hits search(
357                            long companyId, long groupId, long userId, String keywords,
358                            int start, int end, Sort sort)
359                    throws SystemException {
360    
361                    try {
362                            SearchContext searchContext = new SearchContext();
363    
364                            searchContext.setCompanyId(companyId);
365                            searchContext.setEnd(end);
366                            searchContext.setKeywords(keywords);
367                            searchContext.setGroupIds(new long[] {groupId});
368    
369                            if (sort != null) {
370                                    searchContext.setSorts(new Sort[] {sort});
371                            }
372    
373                            searchContext.setStart(start);
374                            searchContext.setUserId(userId);
375    
376                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
377                                    TrashEntry.class);
378    
379                            return indexer.search(searchContext);
380                    }
381                    catch (Exception e) {
382                            throw new SystemException(e);
383                    }
384            }
385    
386            protected void checkEntriesAttachments(Group group)
387                    throws PortalException, SystemException {
388    
389                    Date date = getMaxAge(group);
390    
391                    for (TrashHandler trashHandler :
392                                    TrashHandlerRegistryUtil.getTrashHandlers()) {
393    
394                            trashHandler.deleteTrashAttachments(group, date);
395                    }
396            }
397    
398            protected Date getMaxAge(Group group)
399                    throws PortalException, SystemException {
400    
401                    Calendar calendar = Calendar.getInstance();
402    
403                    calendar.setTime(new Date());
404    
405                    int maxAge = TrashUtil.getMaxAge(group);
406    
407                    calendar.add(Calendar.DATE, -maxAge);
408    
409                    return calendar.getTime();
410            }
411    
412    }