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.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     * The trash local service is responsible for accessing, creating, modifying and
047     * deleting 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            public TrashEntry addTrashEntry(
070                            long userId, long groupId, String className, long classPK,
071                            int status, List<ObjectValuePair<Long, Integer>> statusOVPs,
072                            UnicodeProperties typeSettingsProperties)
073                    throws PortalException, SystemException {
074    
075                    User user = userPersistence.findByPrimaryKey(userId);
076                    long classNameId = PortalUtil.getClassNameId(className);
077    
078                    long entryId = counterLocalService.increment();
079    
080                    TrashEntry trashEntry = trashEntryPersistence.create(entryId);
081    
082                    trashEntry.setGroupId(groupId);
083                    trashEntry.setCompanyId(user.getCompanyId());
084                    trashEntry.setUserId(user.getUserId());
085                    trashEntry.setUserName(user.getFullName());
086                    trashEntry.setCreateDate(new Date());
087                    trashEntry.setClassNameId(classNameId);
088                    trashEntry.setClassPK(classPK);
089    
090                    if (typeSettingsProperties != null) {
091                            trashEntry.setTypeSettingsProperties(typeSettingsProperties);
092                    }
093    
094                    trashEntry.setStatus(status);
095    
096                    trashEntryPersistence.update(trashEntry);
097    
098                    if (statusOVPs != null) {
099                            for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
100                                    long versionId = counterLocalService.increment();
101    
102                                    TrashVersion trashVersion = trashVersionPersistence.create(
103                                            versionId);
104    
105                                    trashVersion.setEntryId(entryId);
106                                    trashVersion.setClassNameId(classNameId);
107                                    trashVersion.setClassPK(statusOVP.getKey());
108                                    trashVersion.setStatus(statusOVP.getValue());
109    
110                                    trashVersionPersistence.update(trashVersion);
111                            }
112                    }
113    
114                    return trashEntry;
115            }
116    
117            public void checkEntries() throws PortalException, SystemException {
118                    ActionableDynamicQuery actionableDynamicQuery =
119                            new GroupActionableDynamicQuery() {
120    
121                            @Override
122                            protected void performAction(Object object)
123                                    throws PortalException, SystemException {
124    
125                                    Group group = (Group)object;
126    
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                    actionableDynamicQuery.performActions();
144            }
145    
146            /**
147             * Deletes the trash entry with the primary key.
148             *
149             * @param  entryId the primary key of the trash entry
150             * @throws PortalException if a trash entry with the primary key could not
151             *         be found
152             * @throws SystemException if a system exception occurred
153             */
154            public TrashEntry deleteEntry(long entryId)
155                    throws PortalException, SystemException {
156    
157                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
158    
159                    return deleteEntry(entry);
160            }
161    
162            /**
163             * Deletes the trash entry with the entity class name and primary key.
164             *
165             * @param  className the class name of entity
166             * @param  classPK the primary key of the entry
167             * @throws PortalException if a trash entry with the primary key could not
168             *         be found
169             * @throws SystemException if a system exception occurred
170             */
171            public TrashEntry deleteEntry(String className, long classPK)
172                    throws PortalException, SystemException {
173    
174                    long classNameId = PortalUtil.getClassNameId(className);
175    
176                    TrashEntry entry = trashEntryPersistence.fetchByC_C(
177                            classNameId, classPK);
178    
179                    return deleteEntry(entry);
180            }
181    
182            @Indexable(type = IndexableType.DELETE)
183            public TrashEntry deleteEntry(TrashEntry trashEntry)
184                    throws SystemException {
185    
186                    if (trashEntry != null) {
187                            trashVersionPersistence.removeByEntryId(trashEntry.getEntryId());
188    
189                            trashEntry = trashEntryPersistence.remove(trashEntry);
190                    }
191    
192                    return trashEntry;
193            }
194    
195            /**
196             * Returns the trash entry with the primary key.
197             *
198             * @param  entryId the primary key of the entry
199             * @return the trash entry with the primary key
200             * @throws SystemException if a system exception occurred
201             */
202            public TrashEntry fetchEntry(long entryId) throws SystemException {
203                    return trashEntryPersistence.fetchByPrimaryKey(entryId);
204            }
205    
206            /**
207             * Returns the trash entry with the entity class name and primary key.
208             *
209             * @param  className the class name of the entity
210             * @param  classPK the primary key of the entity
211             * @return the trash entry with the entity class name and primary key
212             * @throws SystemException if a system exception occurred
213             */
214            public TrashEntry fetchEntry(String className, long classPK)
215                    throws SystemException {
216    
217                    long classNameId = PortalUtil.getClassNameId(className);
218    
219                    return trashEntryPersistence.fetchByC_C(classNameId, classPK);
220            }
221    
222            /**
223             * Returns the trash entries with the matching group ID.
224             *
225             * @param  groupId the primary key of the group
226             * @return the trash entries with the group ID
227             * @throws SystemException if a system exception occurred
228             */
229            public List<TrashEntry> getEntries(long groupId) throws SystemException {
230                    return trashEntryPersistence.findByGroupId(groupId);
231            }
232    
233            /**
234             * Returns a range of all the trash entries matching the group ID.
235             *
236             * @param  groupId the primary key of the group
237             * @param  start the lower bound of the range of trash entries to return
238             * @param  end the upper bound of the range of trash entries to return (not
239             *         inclusive)
240             * @return the range of matching trash entries
241             * @throws SystemException if a system exception occurred
242             */
243            public List<TrashEntry> getEntries(long groupId, int start, int end)
244                    throws SystemException {
245    
246                    return trashEntryPersistence.findByGroupId(groupId, start, end);
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             * @param  obc the comparator to order the trash entries (optionally
257             *         <code>null</code>)
258             * @return the range of matching trash entries ordered by comparator
259             *         <code>obc</code>
260             * @throws SystemException if a system exception occurred
261             */
262            public List<TrashEntry> getEntries(
263                            long groupId, int start, int end, OrderByComparator obc)
264                    throws SystemException {
265    
266                    return trashEntryPersistence.findByGroupId(groupId, start, end, obc);
267            }
268    
269            /**
270             * Returns the number of trash entries with the group ID.
271             *
272             * @param  groupId the primary key of the group
273             * @return the number of matching trash entries
274             * @throws SystemException if a system exception occurred
275             */
276            public int getEntriesCount(long groupId) throws SystemException {
277                    return trashEntryPersistence.countByGroupId(groupId);
278            }
279    
280            /**
281             * Returns the trash entry with the primary key.
282             *
283             * @param  entryId the primary key of the trash entry
284             * @return the trash entry with the primary key
285             * @throws PortalException if a trash entry with the primary key could not
286             *         be found
287             * @throws SystemException if a system exception occurred
288             */
289            public TrashEntry getEntry(long entryId)
290                    throws PortalException, SystemException {
291    
292                    return trashEntryPersistence.findByPrimaryKey(entryId);
293            }
294    
295            /**
296             * Returns the entry with the entity class name and primary key.
297             *
298             * @param  className the class name of the entity
299             * @param  classPK the primary key of the entity
300             * @return the trash entry with the entity class name and 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(String className, long classPK)
306                    throws PortalException, SystemException {
307    
308                    long classNameId = PortalUtil.getClassNameId(className);
309    
310                    return trashEntryPersistence.findByC_C(classNameId, classPK);
311            }
312    
313            /**
314             * Returns all the trash versions associated with the trash entry.
315             *
316             * @param  entryId the primary key of the trash entry
317             * @return all the trash versions associated with the trash entry
318             * @throws SystemException if a system exception occurred
319             */
320            public List<TrashVersion> getVersions(long entryId) throws SystemException {
321                    return trashVersionPersistence.findByEntryId(entryId);
322            }
323    
324            /**
325             * Returns all the trash versions associated with the trash entry.
326             *
327             * @param  className the class name of the trash entity
328             * @param  classPK the primary key of the trash entity
329             * @return all the trash versions associated with the trash entry
330             * @throws SystemException if a system exception occurred
331             */
332            public List<TrashVersion> getVersions(String className, long classPK)
333                    throws SystemException {
334    
335                    long classNameId = PortalUtil.getClassNameId(className);
336    
337                    return trashVersionPersistence.findByC_C(classNameId, classPK);
338            }
339    
340            public Hits search(
341                            long companyId, long groupId, long userId, String keywords,
342                            int start, int end, Sort sort)
343                    throws SystemException {
344    
345                    try {
346                            SearchContext searchContext = new SearchContext();
347    
348                            searchContext.setCompanyId(companyId);
349                            searchContext.setEnd(end);
350                            searchContext.setKeywords(keywords);
351                            searchContext.setGroupIds(new long[] {groupId});
352    
353                            if (sort != null) {
354                                    searchContext.setSorts(new Sort[] {sort});
355                            }
356    
357                            searchContext.setStart(start);
358                            searchContext.setUserId(userId);
359    
360                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
361                                    TrashEntry.class);
362    
363                            return indexer.search(searchContext);
364                    }
365                    catch (Exception e) {
366                            throw new SystemException(e);
367                    }
368            }
369    
370            protected Date getMaxAge(Group group)
371                    throws PortalException, SystemException {
372    
373                    Calendar calendar = Calendar.getInstance();
374    
375                    calendar.setTime(new Date());
376    
377                    int maxAge = TrashUtil.getMaxAge(group);
378    
379                    calendar.add(Calendar.DATE, -maxAge);
380    
381                    return calendar.getTime();
382            }
383    
384    }