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