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.BaseActionableDynamicQuery;
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             * @throws PortalException if a user with the primary key could not be found
072             */
073            @Override
074            public TrashEntry addTrashEntry(
075                            long userId, long groupId, String className, long classPK,
076                            String classUuid, String referrerClassName, int status,
077                            List<ObjectValuePair<Long, Integer>> statusOVPs,
078                            UnicodeProperties typeSettingsProperties)
079                    throws PortalException {
080    
081                    User user = userPersistence.findByPrimaryKey(userId);
082                    long classNameId = classNameLocalService.getClassNameId(className);
083    
084                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
085                            className);
086    
087                    SystemEvent systemEvent = trashHandler.addDeletionSystemEvent(
088                            userId, groupId, classPK, classUuid, referrerClassName);
089    
090                    long entryId = counterLocalService.increment();
091    
092                    TrashEntry trashEntry = trashEntryPersistence.create(entryId);
093    
094                    trashEntry.setGroupId(groupId);
095                    trashEntry.setCompanyId(user.getCompanyId());
096                    trashEntry.setUserId(user.getUserId());
097                    trashEntry.setUserName(user.getFullName());
098                    trashEntry.setCreateDate(new Date());
099                    trashEntry.setClassNameId(classNameId);
100                    trashEntry.setClassPK(classPK);
101                    trashEntry.setSystemEventSetKey(systemEvent.getSystemEventSetKey());
102    
103                    if (typeSettingsProperties != null) {
104                            trashEntry.setTypeSettingsProperties(typeSettingsProperties);
105                    }
106    
107                    trashEntry.setStatus(status);
108    
109                    trashEntryPersistence.update(trashEntry);
110    
111                    if (statusOVPs != null) {
112                            for (ObjectValuePair<Long, Integer> statusOVP : statusOVPs) {
113                                    long versionId = counterLocalService.increment();
114    
115                                    TrashVersion trashVersion = trashVersionPersistence.create(
116                                            versionId);
117    
118                                    trashVersion.setEntryId(entryId);
119                                    trashVersion.setClassNameId(classNameId);
120                                    trashVersion.setClassPK(statusOVP.getKey());
121                                    trashVersion.setStatus(statusOVP.getValue());
122    
123                                    trashVersionPersistence.update(trashVersion);
124                            }
125                    }
126    
127                    return trashEntry;
128            }
129    
130            @Override
131            public void checkEntries() throws PortalException {
132                    ActionableDynamicQuery actionableDynamicQuery =
133                            trashEntryLocalService.getActionableDynamicQuery();
134    
135                    actionableDynamicQuery.setPerformActionMethod(
136                            new ActionableDynamicQuery.PerformActionMethod<TrashEntry>() {
137    
138                                    @Override
139                                    public void performAction(TrashEntry trashEntry)
140                                            throws PortalException {
141    
142                                            Date createDate = trashEntry.getCreateDate();
143    
144                                            Group group = groupPersistence.fetchByPrimaryKey(
145                                                    trashEntry.getGroupId());
146    
147                                            if (group == null) {
148                                                    return;
149                                            }
150    
151                                            Date date = getMaxAge(group);
152    
153                                            if (createDate.before(date) ||
154                                                    !TrashUtil.isTrashEnabled(group)) {
155    
156                                                    TrashHandler trashHandler =
157                                                            TrashHandlerRegistryUtil.getTrashHandler(
158                                                                    trashEntry.getClassName());
159    
160                                                    trashHandler.deleteTrashEntry(trashEntry.getClassPK());
161                                            }
162                                    }
163    
164                            });
165                    actionableDynamicQuery.setTransactionAttribute(
166                            BaseActionableDynamicQuery.REQUIRES_NEW_TRANSACTION_ATTRIBUTE);
167    
168                    actionableDynamicQuery.performActions();
169            }
170    
171            @Override
172            public void deleteEntries(long groupId) {
173                    List<TrashEntry> entries = getEntries(groupId);
174    
175                    for (TrashEntry entry : entries) {
176                            deleteEntry(entry);
177                    }
178            }
179    
180            /**
181             * Deletes the trash entry with the primary key.
182             *
183             * @param  entryId the primary key of the trash entry
184             * @return the trash entry with the primary key
185             * @throws PortalException if a trash entry with the primary key could not
186             *         be found
187             */
188            @Override
189            public TrashEntry deleteEntry(long entryId) throws PortalException {
190                    TrashEntry entry = trashEntryPersistence.fetchByPrimaryKey(entryId);
191    
192                    return deleteEntry(entry);
193            }
194    
195            /**
196             * Deletes the trash entry with the entity class name and primary key.
197             *
198             * @param  className the class name of entity
199             * @param  classPK the primary key of the entry
200             * @return the trash entry with the entity class name and primary key
201             * @throws PortalException if a trash entry with the primary key could not
202             *         be found
203             */
204            @Override
205            public TrashEntry deleteEntry(String className, long classPK)
206                    throws PortalException {
207    
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             * @throws PortalException if a trash entry with the primary key could not
324             *         be found
325             */
326            @Override
327            public TrashEntry getEntry(long entryId) throws PortalException {
328                    return trashEntryPersistence.findByPrimaryKey(entryId);
329            }
330    
331            /**
332             * Returns the entry with the entity class name and primary key.
333             *
334             * @param  className the class name of the entity
335             * @param  classPK the primary key of the entity
336             * @return the trash entry with the entity class name and primary key
337             * @throws PortalException if a trash entry with the primary key could not
338             *         be found
339             */
340            @Override
341            public TrashEntry getEntry(String className, long classPK)
342                    throws PortalException {
343    
344                    long classNameId = classNameLocalService.getClassNameId(className);
345    
346                    return trashEntryPersistence.findByC_C(classNameId, classPK);
347            }
348    
349            @Override
350            public Hits search(
351                    long companyId, long groupId, long userId, String keywords, int start,
352                    int end, Sort sort) {
353    
354                    try {
355                            Indexer<TrashEntry> indexer =
356                                    IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
357    
358                            SearchContext searchContext = buildSearchContext(
359                                    companyId, groupId, userId, keywords, start, end, sort);
360    
361                            return indexer.search(searchContext);
362                    }
363                    catch (Exception e) {
364                            throw new SystemException(e);
365                    }
366            }
367    
368            @Override
369            public BaseModelSearchResult<TrashEntry> searchTrashEntries(
370                    long companyId, long groupId, long userId, String keywords, int start,
371                    int end, Sort sort) {
372    
373                    try {
374                            Indexer<TrashEntry> indexer =
375                                    IndexerRegistryUtil.nullSafeGetIndexer(TrashEntry.class);
376    
377                            SearchContext searchContext = buildSearchContext(
378                                    companyId, groupId, userId, keywords, start, end, sort);
379    
380                            Hits hits = indexer.search(searchContext);
381    
382                            List<TrashEntry> trashEntries = TrashUtil.getEntries(hits);
383    
384                            return new BaseModelSearchResult<>(trashEntries, hits.getLength());
385                    }
386                    catch (Exception e) {
387                            throw new SystemException(e);
388                    }
389            }
390    
391            protected SearchContext buildSearchContext(
392                    long companyId, long groupId, long userId, String keywords, int start,
393                    int end, Sort sort) {
394    
395                    SearchContext searchContext = new SearchContext();
396    
397                    searchContext.setCompanyId(companyId);
398                    searchContext.setEnd(end);
399                    searchContext.setKeywords(keywords);
400                    searchContext.setGroupIds(new long[] {groupId});
401    
402                    if (sort != null) {
403                            searchContext.setSorts(sort);
404                    }
405    
406                    searchContext.setStart(start);
407                    searchContext.setUserId(userId);
408    
409                    QueryConfig queryConfig = searchContext.getQueryConfig();
410    
411                    queryConfig.setHighlightEnabled(false);
412                    queryConfig.setScoreEnabled(false);
413    
414                    return searchContext;
415            }
416    
417            protected Date getMaxAge(Group group) throws PortalException {
418                    Calendar calendar = Calendar.getInstance();
419    
420                    calendar.setTime(new Date());
421    
422                    int maxAge = TrashUtil.getMaxAge(group);
423    
424                    calendar.add(Calendar.MINUTE, -maxAge);
425    
426                    return calendar.getTime();
427            }
428    
429    }