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.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.dao.orm.WildcardMode;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.language.LanguageUtil;
023    import com.liferay.portal.kernel.lock.Lock;
024    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
025    import com.liferay.portal.kernel.portlet.PortletProvider;
026    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
027    import com.liferay.portal.kernel.repository.LocalRepository;
028    import com.liferay.portal.kernel.repository.Repository;
029    import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
030    import com.liferay.portal.kernel.repository.capabilities.RepositoryEventTriggerCapability;
031    import com.liferay.portal.kernel.repository.event.RepositoryEventType;
032    import com.liferay.portal.kernel.repository.event.TrashRepositoryEventType;
033    import com.liferay.portal.kernel.repository.event.WorkflowRepositoryEventType;
034    import com.liferay.portal.kernel.repository.model.FileEntry;
035    import com.liferay.portal.kernel.repository.model.FileShortcut;
036    import com.liferay.portal.kernel.repository.model.FileVersion;
037    import com.liferay.portal.kernel.repository.model.Folder;
038    import com.liferay.portal.kernel.repository.model.RepositoryModel;
039    import com.liferay.portal.kernel.search.Indexer;
040    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
041    import com.liferay.portal.kernel.settings.LocalizedValuesMap;
042    import com.liferay.portal.kernel.social.SocialActivityManagerUtil;
043    import com.liferay.portal.kernel.util.Constants;
044    import com.liferay.portal.kernel.util.GetterUtil;
045    import com.liferay.portal.kernel.util.ListUtil;
046    import com.liferay.portal.kernel.util.LocalizationUtil;
047    import com.liferay.portal.kernel.util.ObjectValuePair;
048    import com.liferay.portal.kernel.util.UnicodeProperties;
049    import com.liferay.portal.kernel.util.Validator;
050    import com.liferay.portal.kernel.workflow.WorkflowConstants;
051    import com.liferay.portal.model.UserConstants;
052    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
053    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
054    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
055    import com.liferay.portal.service.ServiceContext;
056    import com.liferay.portal.util.GroupSubscriptionCheckSubscriptionSender;
057    import com.liferay.portal.util.SubscriptionSender;
058    import com.liferay.portlet.asset.model.AssetEntry;
059    import com.liferay.portlet.asset.model.AssetLink;
060    import com.liferay.portlet.asset.model.AssetLinkConstants;
061    import com.liferay.portlet.documentlibrary.DLGroupServiceSettings;
062    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
063    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
064    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
065    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
066    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
067    import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
068    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
069    import com.liferay.portlet.documentlibrary.model.DLFolder;
070    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
071    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
072    import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
073    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
074    import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
075    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
076    import com.liferay.portlet.documentlibrary.util.comparator.DLFileVersionVersionComparator;
077    import com.liferay.portlet.social.model.SocialActivityConstants;
078    import com.liferay.portlet.trash.model.TrashEntry;
079    import com.liferay.portlet.trash.model.TrashVersion;
080    import com.liferay.portlet.trash.util.TrashUtil;
081    import com.liferay.util.dao.orm.CustomSQLUtil;
082    
083    import java.io.Serializable;
084    
085    import java.util.ArrayList;
086    import java.util.Date;
087    import java.util.HashMap;
088    import java.util.List;
089    import java.util.Map;
090    
091    /**
092     * Provides the local service helper for the document library application.
093     *
094     * @author Alexander Chow
095     */
096    public class DLAppHelperLocalServiceImpl
097            extends DLAppHelperLocalServiceBaseImpl {
098    
099            @Override
100            public void addFolder(
101                            long userId, Folder folder, ServiceContext serviceContext)
102                    throws PortalException {
103    
104                    if (!DLAppHelperThreadLocal.isEnabled()) {
105                            return;
106                    }
107    
108                    updateAsset(
109                            userId, folder, serviceContext.getAssetCategoryIds(),
110                            serviceContext.getAssetTagNames(),
111                            serviceContext.getAssetLinkEntryIds());
112            }
113    
114            @Override
115            public void cancelCheckOut(
116                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
117                            FileVersion destinationFileVersion, FileVersion draftFileVersion,
118                            ServiceContext serviceContext)
119                    throws PortalException {
120    
121                    if (draftFileVersion == null) {
122                            return;
123                    }
124    
125                    AssetEntry draftAssetEntry = assetEntryLocalService.fetchEntry(
126                            DLFileEntryConstants.getClassName(),
127                            draftFileVersion.getPrimaryKey());
128    
129                    if (draftAssetEntry != null) {
130                            assetEntryLocalService.deleteEntry(draftAssetEntry);
131                    }
132            }
133    
134            @Override
135            public void checkAssetEntry(
136                            long userId, FileEntry fileEntry, FileVersion fileVersion)
137                    throws PortalException {
138    
139                    AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
140                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
141    
142                    long[] assetCategoryIds = new long[0];
143                    String[] assetTagNames = new String[0];
144    
145                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
146    
147                    if (fileEntryAssetEntry == null) {
148                            fileEntryAssetEntry = assetEntryLocalService.updateEntry(
149                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
150                                    fileEntry.getModifiedDate(),
151                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
152                                    fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
153                                    assetTagNames, false, null, null, null, fileEntry.getMimeType(),
154                                    fileEntry.getTitle(), fileEntry.getDescription(), null, null,
155                                    null, 0, 0, null);
156                    }
157    
158                    AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
159                            DLFileEntryConstants.getClassName(),
160                            fileVersion.getFileVersionId());
161    
162                    if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
163                            !fileVersion.getVersion().equals(
164                                    DLFileEntryConstants.VERSION_DEFAULT)) {
165    
166                            assetCategoryIds = assetCategoryLocalService.getCategoryIds(
167                                    DLFileEntryConstants.getClassName(),
168                                    fileEntry.getFileEntryId());
169                            assetTagNames = assetTagLocalService.getTagNames(
170                                    DLFileEntryConstants.getClassName(),
171                                    fileEntry.getFileEntryId());
172    
173                            fileVersionAssetEntry = assetEntryLocalService.updateEntry(
174                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
175                                    fileEntry.getModifiedDate(),
176                                    DLFileEntryConstants.getClassName(),
177                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
178                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
179                                    null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
180                                    fileEntry.getDescription(), null, null, null, 0, 0, null);
181    
182                            List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
183                                    fileEntryAssetEntry.getEntryId());
184    
185                            long[] assetLinkIds = ListUtil.toLongArray(
186                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
187    
188                            assetLinkLocalService.updateLinks(
189                                    userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
190                                    AssetLinkConstants.TYPE_RELATED);
191                    }
192            }
193    
194            @Override
195            public void deleteFileEntry(FileEntry fileEntry) throws PortalException {
196                    if (!DLAppHelperThreadLocal.isEnabled()) {
197                            return;
198                    }
199    
200                    // Subscriptions
201    
202                    subscriptionLocalService.deleteSubscriptions(
203                            fileEntry.getCompanyId(), DLFileEntryConstants.getClassName(),
204                            fileEntry.getFileEntryId());
205    
206                    // File ranks
207    
208                    dlFileRankLocalService.deleteFileRanksByFileEntryId(
209                            fileEntry.getFileEntryId());
210    
211                    // File shortcuts
212    
213                    dlFileShortcutLocalService.deleteFileShortcuts(
214                            fileEntry.getFileEntryId());
215    
216                    // Asset
217    
218                    assetEntryLocalService.deleteEntry(
219                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
220    
221                    // Ratings
222    
223                    ratingsStatsLocalService.deleteStats(
224                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
225            }
226    
227            @Override
228            public void deleteFolder(Folder folder) throws PortalException {
229                    if (!DLAppHelperThreadLocal.isEnabled()) {
230                            return;
231                    }
232    
233                    // Asset
234    
235                    assetEntryLocalService.deleteEntry(
236                            DLFolderConstants.getClassName(), folder.getFolderId());
237            }
238    
239            @Override
240            public void deleteRepositoryFileEntries(long repositoryId)
241                    throws PortalException {
242    
243                    LocalRepository localRepository =
244                            RepositoryProviderUtil.getLocalRepository(repositoryId);
245    
246                    List<FileEntry> fileEntries = localRepository.getRepositoryFileEntries(
247                            UserConstants.USER_ID_DEFAULT,
248                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, QueryUtil.ALL_POS,
249                            QueryUtil.ALL_POS, null);
250    
251                    for (FileEntry fileEntry : fileEntries) {
252                            deleteFileEntry(fileEntry);
253                    }
254            }
255    
256            @Override
257            public void getFileAsStream(
258                    long userId, FileEntry fileEntry, boolean incrementCounter) {
259    
260                    if (!incrementCounter) {
261                            return;
262                    }
263    
264                    // File rank
265    
266                    if (userId > 0) {
267                            dlFileRankLocalService.updateFileRank(
268                                    fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
269                                    fileEntry.getFileEntryId(), new ServiceContext());
270                    }
271    
272                    // File read count
273    
274                    assetEntryLocalService.incrementViewCounter(
275                            userId, DLFileEntryConstants.getClassName(),
276                            fileEntry.getFileEntryId(), 1);
277    
278                    List<DLFileShortcut> fileShortcuts =
279                            dlFileShortcutPersistence.findByToFileEntryId(
280                                    fileEntry.getFileEntryId());
281    
282                    for (DLFileShortcut fileShortcut : fileShortcuts) {
283                            assetEntryLocalService.incrementViewCounter(
284                                    userId, DLFileShortcutConstants.getClassName(),
285                                    fileShortcut.getFileShortcutId(), 1);
286                    }
287            }
288    
289            @Override
290            public List<DLFileShortcut> getFileShortcuts(
291                    long groupId, long folderId, boolean active, int status) {
292    
293                    return dlFileShortcutPersistence.findByG_F_A_S(
294                            groupId, folderId, active, status);
295            }
296    
297            /**
298             * @deprecated As of 6.2.0, replaced by {@link #getFileShortcuts(long, long,
299             *             boolean, int)}
300             */
301            @Deprecated
302            @Override
303            public List<DLFileShortcut> getFileShortcuts(
304                    long groupId, long folderId, int status) {
305    
306                    return getFileShortcuts(groupId, folderId, true, status);
307            }
308    
309            @Override
310            public int getFileShortcutsCount(
311                    long groupId, long folderId, boolean active, int status) {
312    
313                    return dlFileShortcutPersistence.countByG_F_A_S(
314                            groupId, folderId, active, status);
315            }
316    
317            /**
318             * @deprecated As of 6.2.0, replaced by {@link #getFileShortcutsCount(long,
319             *             long, boolean, int)}
320             */
321            @Deprecated
322            @Override
323            public int getFileShortcutsCount(long groupId, long folderId, int status) {
324                    return getFileShortcutsCount(groupId, folderId, true, status);
325            }
326    
327            @Override
328            public List<FileEntry> getNoAssetFileEntries() {
329                    return null;
330            }
331    
332            @Override
333            public void moveDependentsToTrash(DLFolder dlFolder)
334                    throws PortalException {
335    
336                    trashOrRestoreFolder(dlFolder, true);
337            }
338    
339            /**
340             * @deprecated As of 7.0.0, replaced by {@link
341             *             #moveDependentsToTrash(DLFolder)}
342             */
343            @Deprecated
344            @Override
345            public void moveDependentsToTrash(
346                            List<Object> dlFileEntriesAndDLFolders, long trashEntryId)
347                    throws PortalException {
348    
349                    if (dlFileEntriesAndDLFolders.isEmpty()) {
350                            return;
351                    }
352    
353                    Object object = dlFileEntriesAndDLFolders.get(0);
354    
355                    long folderId = 0;
356    
357                    if (object instanceof DLFileEntry) {
358                            DLFileEntry dlFileEntry = (DLFileEntry)object;
359    
360                            folderId = dlFileEntry.getFolderId();
361                    }
362                    else if (object instanceof DLFileShortcut) {
363                            DLFileShortcut dlFileShortcut = (DLFileShortcut)object;
364    
365                            folderId = dlFileShortcut.getFolderId();
366                    }
367                    else if (object instanceof DLFolder) {
368                            DLFolder dlFolder = (DLFolder)object;
369    
370                            folderId = dlFolder.getFolderId();
371                    }
372    
373                    DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
374    
375                    moveDependentsToTrash(dlFolder);
376            }
377    
378            @Override
379            public FileEntry moveFileEntryFromTrash(
380                            long userId, FileEntry fileEntry, long newFolderId,
381                            ServiceContext serviceContext)
382                    throws PortalException {
383    
384                    boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
385                            userId, fileEntry.getFileEntryId());
386    
387                    if (!hasLock) {
388                            dlFileEntryLocalService.lockFileEntry(
389                                    userId, fileEntry.getFileEntryId());
390                    }
391    
392                    try {
393                            return doMoveFileEntryFromTrash(
394                                    userId, fileEntry, newFolderId, serviceContext);
395                    }
396                    finally {
397                            if (!hasLock) {
398                                    dlFileEntryLocalService.unlockFileEntry(
399                                            fileEntry.getFileEntryId());
400                            }
401                    }
402            }
403    
404            /**
405             * Moves the file entry to the recycle bin.
406             *
407             * @param  userId the primary key of the user moving the file entry
408             * @param  fileEntry the file entry to be moved
409             * @return the moved file entry
410             * @throws PortalException if a user with the primary key could not be found
411             */
412            @Override
413            public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
414                    throws PortalException {
415    
416                    boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
417                            userId, fileEntry.getFileEntryId());
418    
419                    if (!hasLock) {
420                            dlFileEntryLocalService.lockFileEntry(
421                                    userId, fileEntry.getFileEntryId());
422                    }
423    
424                    try {
425                            if (fileEntry.isCheckedOut()) {
426                                    dlFileEntryLocalService.cancelCheckOut(
427                                            userId, fileEntry.getFileEntryId());
428                            }
429    
430                            return doMoveFileEntryToTrash(userId, fileEntry);
431                    }
432                    finally {
433                            if (!hasLock) {
434                                    dlFileEntryLocalService.unlockFileEntry(
435                                            fileEntry.getFileEntryId());
436                            }
437                    }
438            }
439    
440            @Override
441            public FileShortcut moveFileShortcutFromTrash(
442                            long userId, FileShortcut fileShortcut, long newFolderId,
443                            ServiceContext serviceContext)
444                    throws PortalException {
445    
446                    DLFileShortcut dlFileShortcut = (DLFileShortcut)fileShortcut.getModel();
447    
448                    if (dlFileShortcut.isInTrashExplicitly()) {
449                            restoreFileShortcutFromTrash(userId, fileShortcut);
450                    }
451                    else {
452    
453                            // File shortcut
454    
455                            TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
456                                    DLFileShortcutConstants.getClassName(),
457                                    fileShortcut.getFileShortcutId());
458    
459                            int status = WorkflowConstants.STATUS_APPROVED;
460    
461                            if (trashVersion != null) {
462                                    status = trashVersion.getStatus();
463                            }
464    
465                            dlFileShortcutLocalService.updateStatus(
466                                    userId, fileShortcut.getFileShortcutId(), status,
467                                    new ServiceContext());
468    
469                            // Trash
470    
471                            if (trashVersion != null) {
472                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
473                            }
474    
475                            // Social
476    
477                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
478    
479                            extraDataJSONObject.put("title", fileShortcut.getToTitle());
480    
481                            SocialActivityManagerUtil.addActivity(
482                                    userId, fileShortcut,
483                                    SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
484                                    extraDataJSONObject.toString(), 0);
485                    }
486    
487                    return dlAppService.updateFileShortcut(
488                            fileShortcut.getFileShortcutId(), newFolderId,
489                            fileShortcut.getToFileEntryId(), serviceContext);
490            }
491    
492            /**
493             * Moves the file shortcut to the recycle bin.
494             *
495             * @param  userId the primary key of the user moving the file shortcut
496             * @param  fileShortcut the file shortcut to be moved
497             * @return the moved file shortcut
498             * @throws PortalException if a user with the primary key could not be found
499             */
500            @Override
501            public FileShortcut moveFileShortcutToTrash(
502                            long userId, FileShortcut fileShortcut)
503                    throws PortalException {
504    
505                    // File shortcut
506    
507                    DLFileShortcut dlFileShortcut = (DLFileShortcut)fileShortcut.getModel();
508    
509                    int oldStatus = dlFileShortcut.getStatus();
510    
511                    dlFileShortcutLocalService.updateStatus(
512                            userId, fileShortcut.getFileShortcutId(),
513                            WorkflowConstants.STATUS_IN_TRASH, new ServiceContext());
514    
515                    // Social
516    
517                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
518    
519                    extraDataJSONObject.put(
520                            "title", TrashUtil.getOriginalTitle(fileShortcut.getToTitle()));
521    
522                    SocialActivityManagerUtil.addActivity(
523                            userId, fileShortcut, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
524                            extraDataJSONObject.toString(), 0);
525    
526                    // Trash
527    
528                    trashEntryLocalService.addTrashEntry(
529                            userId, fileShortcut.getGroupId(),
530                            DLFileShortcutConstants.getClassName(),
531                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), null,
532                            oldStatus, null, null);
533    
534                    return fileShortcut;
535            }
536    
537            @Override
538            public Folder moveFolderFromTrash(
539                            long userId, Folder folder, long parentFolderId,
540                            ServiceContext serviceContext)
541                    throws PortalException {
542    
543                    boolean hasLock = dlFolderLocalService.hasFolderLock(
544                            userId, folder.getFolderId());
545    
546                    Lock lock = null;
547    
548                    if (!hasLock) {
549                            lock = dlFolderLocalService.lockFolder(
550                                    userId, folder.getFolderId());
551                    }
552    
553                    try {
554                            return doMoveFolderFromTrash(
555                                    userId, folder, parentFolderId, serviceContext);
556                    }
557                    finally {
558                            if (!hasLock) {
559                                    dlFolderLocalService.unlockFolder(
560                                            folder.getFolderId(), lock.getUuid());
561                            }
562                    }
563            }
564    
565            /**
566             * Moves the folder to the recycle bin.
567             *
568             * @param  userId the primary key of the user moving the folder
569             * @param  folder the folder to be moved
570             * @return the moved folder
571             * @throws PortalException if a user with the primary key could not be found
572             */
573            @Override
574            public Folder moveFolderToTrash(long userId, Folder folder)
575                    throws PortalException {
576    
577                    boolean hasLock = dlFolderLocalService.hasFolderLock(
578                            userId, folder.getFolderId());
579    
580                    Lock lock = null;
581    
582                    if (!hasLock) {
583                            lock = dlFolderLocalService.lockFolder(
584                                    userId, folder.getFolderId());
585                    }
586    
587                    try {
588                            return doMoveFolderToTrash(userId, folder);
589                    }
590                    finally {
591                            if (!hasLock) {
592                                    dlFolderLocalService.unlockFolder(
593                                            folder.getFolderId(), lock.getUuid());
594                            }
595                    }
596            }
597    
598            @Override
599            public void restoreDependentsFromTrash(DLFolder dlFolder)
600                    throws PortalException {
601    
602                    trashOrRestoreFolder(dlFolder, false);
603            }
604    
605            /**
606             * @deprecated As of 7.0.0, replaced by {@link
607             *             #restoreDependentsFromTrash(DLFolder)}
608             */
609            @Deprecated
610            @Override
611            public void restoreDependentsFromTrash(
612                            List<Object> dlFileEntriesAndDLFolders)
613                    throws PortalException {
614    
615                    if (dlFileEntriesAndDLFolders.isEmpty()) {
616                            return;
617                    }
618    
619                    Object object = dlFileEntriesAndDLFolders.get(0);
620    
621                    long folderId = 0;
622    
623                    if (object instanceof DLFileEntry) {
624                            DLFileEntry dlFileEntry = (DLFileEntry)object;
625    
626                            folderId = dlFileEntry.getFolderId();
627                    }
628                    else if (object instanceof DLFileShortcut) {
629                            DLFileShortcut dlFileShortcut = (DLFileShortcut)object;
630    
631                            folderId = dlFileShortcut.getFolderId();
632                    }
633                    else if (object instanceof DLFolder) {
634                            DLFolder dlFolder = (DLFolder)object;
635    
636                            folderId = dlFolder.getFolderId();
637                    }
638    
639                    DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
640    
641                    restoreDependentsFromTrash(dlFolder);
642            }
643    
644            /**
645             * @deprecated As of 7.0.0, replaced by {@link
646             *             #restoreDependentsFromTrash(List)}
647             */
648            @Deprecated
649            @Override
650            public void restoreDependentsFromTrash(
651                            List<Object> dlFileEntriesAndDLFolders, long trashEntryId)
652                    throws PortalException {
653    
654                    restoreDependentsFromTrash(dlFileEntriesAndDLFolders);
655            }
656    
657            @Override
658            public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
659                    throws PortalException {
660    
661                    // File entry
662    
663                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
664    
665                    dlFileEntry.setFileName(
666                            TrashUtil.getOriginalTitle(dlFileEntry.getTitle(), "fileName"));
667                    dlFileEntry.setTitle(
668                            TrashUtil.getOriginalTitle(dlFileEntry.getTitle()));
669    
670                    dlFileEntryPersistence.update(dlFileEntry);
671    
672                    FileVersion fileVersion = fileEntry.getFileVersion();
673    
674                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
675                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
676    
677                    dlFileEntryLocalService.updateStatus(
678                            userId, fileVersion.getFileVersionId(), trashEntry.getStatus(),
679                            new ServiceContext(), new HashMap<String, Serializable>());
680    
681                    if (DLAppHelperThreadLocal.isEnabled()) {
682    
683                            // File rank
684    
685                            dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
686    
687                            // File shortcut
688    
689                            dlFileShortcutLocalService.enableFileShortcuts(
690                                    fileEntry.getFileEntryId());
691    
692                            // Sync
693    
694                            triggerRepositoryEvent(
695                                    fileEntry.getRepositoryId(),
696                                    TrashRepositoryEventType.EntryRestored.class, FileEntry.class,
697                                    fileEntry);
698                    }
699    
700                    // Trash
701    
702                    List<TrashVersion> trashVersions = trashVersionLocalService.getVersions(
703                            trashEntry.getEntryId());
704    
705                    for (TrashVersion trashVersion : trashVersions) {
706                            DLFileVersion trashDLFileVersion =
707                                    dlFileVersionPersistence.findByPrimaryKey(
708                                            trashVersion.getClassPK());
709    
710                            trashDLFileVersion.setStatus(trashVersion.getStatus());
711    
712                            dlFileVersionPersistence.update(trashDLFileVersion);
713                    }
714    
715                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
716    
717                    if (!DLAppHelperThreadLocal.isEnabled()) {
718                            return;
719                    }
720    
721                    // Social
722    
723                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
724    
725                    extraDataJSONObject.put("title", fileEntry.getTitle());
726    
727                    SocialActivityManagerUtil.addActivity(
728                            userId, fileEntry, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
729                            extraDataJSONObject.toString(), 0);
730            }
731    
732            @Override
733            public void restoreFileShortcutFromTrash(
734                            long userId, FileShortcut fileShortcut)
735                    throws PortalException {
736    
737                    // File shortcut
738    
739                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
740                            DLFileShortcutConstants.getClassName(),
741                            fileShortcut.getFileShortcutId());
742    
743                    dlFileShortcutLocalService.updateStatus(
744                            userId, fileShortcut.getFileShortcutId(), trashEntry.getStatus(),
745                            new ServiceContext());
746    
747                    // Social
748    
749                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
750    
751                    extraDataJSONObject.put("title", fileShortcut.getToTitle());
752    
753                    SocialActivityManagerUtil.addActivity(
754                            userId, fileShortcut,
755                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
756                            extraDataJSONObject.toString(), 0);
757    
758                    // Trash
759    
760                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
761            }
762    
763            @Override
764            public void restoreFolderFromTrash(long userId, Folder folder)
765                    throws PortalException {
766    
767                    // Folder
768    
769                    DLFolder dlFolder = (DLFolder)folder.getModel();
770    
771                    dlFolder.setName(TrashUtil.getOriginalTitle(dlFolder.getName()));
772    
773                    dlFolderPersistence.update(dlFolder);
774    
775                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
776                            DLFolder.class.getName(), dlFolder.getFolderId());
777    
778                    dlFolderLocalService.updateStatus(
779                            userId, folder.getFolderId(), trashEntry.getStatus(),
780                            new HashMap<String, Serializable>(), new ServiceContext());
781    
782                    // File rank
783    
784                    dlFileRankLocalService.enableFileRanksByFolderId(folder.getFolderId());
785    
786                    // Folders, file entries, and file shortcuts
787    
788                    restoreDependentsFromTrash(dlFolder);
789    
790                    // Sync
791    
792                    triggerRepositoryEvent(
793                            folder.getRepositoryId(),
794                            TrashRepositoryEventType.EntryRestored.class, Folder.class, folder);
795    
796                    // Trash
797    
798                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
799    
800                    // Social
801    
802                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
803    
804                    extraDataJSONObject.put("title", folder.getName());
805    
806                    SocialActivityManagerUtil.addActivity(
807                            userId, folder, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
808                            extraDataJSONObject.toString(), 0);
809            }
810    
811            @Override
812            public AssetEntry updateAsset(
813                            long userId, FileEntry fileEntry, FileVersion fileVersion,
814                            long assetClassPk)
815                    throws PortalException {
816    
817                    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
818                            DLFileEntryConstants.getClassName(), assetClassPk);
819                    String[] assetTagNames = assetTagLocalService.getTagNames(
820                            DLFileEntryConstants.getClassName(), assetClassPk);
821    
822                    AssetEntry assetEntry = assetEntryLocalService.getEntry(
823                            DLFileEntryConstants.getClassName(), assetClassPk);
824    
825                    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
826                            assetEntry.getEntryId());
827    
828                    long[] assetLinkIds = ListUtil.toLongArray(
829                            assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
830    
831                    return updateAsset(
832                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
833                            assetLinkIds);
834            }
835    
836            @Override
837            public AssetEntry updateAsset(
838                            long userId, FileEntry fileEntry, FileVersion fileVersion,
839                            long[] assetCategoryIds, String[] assetTagNames,
840                            long[] assetLinkEntryIds)
841                    throws PortalException {
842    
843                    AssetEntry assetEntry = null;
844    
845                    boolean visible = false;
846    
847                    boolean addDraftAssetEntry = false;
848    
849                    if (fileEntry instanceof LiferayFileEntry) {
850                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
851    
852                            if (dlFileVersion.isApproved()) {
853                                    visible = true;
854                            }
855                            else {
856                                    String version = dlFileVersion.getVersion();
857    
858                                    if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
859                                            addDraftAssetEntry = true;
860                                    }
861                            }
862                    }
863                    else {
864                            visible = true;
865                    }
866    
867                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
868    
869                    if (addDraftAssetEntry) {
870                            if (assetCategoryIds == null) {
871                                    assetCategoryIds = assetCategoryLocalService.getCategoryIds(
872                                            DLFileEntryConstants.getClassName(),
873                                            fileEntry.getFileEntryId());
874                            }
875    
876                            if (assetTagNames == null) {
877                                    assetTagNames = assetTagLocalService.getTagNames(
878                                            DLFileEntryConstants.getClassName(),
879                                            fileEntry.getFileEntryId());
880                            }
881    
882                            if (assetLinkEntryIds == null) {
883                                    AssetEntry previousAssetEntry = assetEntryLocalService.getEntry(
884                                            DLFileEntryConstants.getClassName(),
885                                            fileEntry.getFileEntryId());
886    
887                                    List<AssetLink> assetLinks =
888                                            assetLinkLocalService.getDirectLinks(
889                                                    previousAssetEntry.getEntryId(),
890                                                    AssetLinkConstants.TYPE_RELATED);
891    
892                                    assetLinkEntryIds = ListUtil.toLongArray(
893                                            assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
894                            }
895    
896                            assetEntry = assetEntryLocalService.updateEntry(
897                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
898                                    fileEntry.getModifiedDate(),
899                                    DLFileEntryConstants.getClassName(),
900                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
901                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
902                                    null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
903                                    fileEntry.getDescription(), null, null, null, 0, 0, null);
904                    }
905                    else {
906                            assetEntry = assetEntryLocalService.updateEntry(
907                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
908                                    fileEntry.getModifiedDate(),
909                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
910                                    fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
911                                    assetTagNames, visible, null, null, null,
912                                    fileEntry.getMimeType(), fileEntry.getTitle(),
913                                    fileEntry.getDescription(), null, null, null, 0, 0, null);
914    
915                            List<DLFileShortcut> dlFileShortcuts =
916                                    dlFileShortcutPersistence.findByToFileEntryId(
917                                            fileEntry.getFileEntryId());
918    
919                            for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
920                                    assetEntryLocalService.updateEntry(
921                                            userId, dlFileShortcut.getGroupId(),
922                                            dlFileShortcut.getCreateDate(),
923                                            dlFileShortcut.getModifiedDate(),
924                                            DLFileShortcutConstants.getClassName(),
925                                            dlFileShortcut.getFileShortcutId(),
926                                            dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
927                                            assetTagNames, true, null, null, null,
928                                            fileEntry.getMimeType(), fileEntry.getTitle(),
929                                            fileEntry.getDescription(), null, null, null, 0, 0, null);
930                            }
931                    }
932    
933                    assetLinkLocalService.updateLinks(
934                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
935                            AssetLinkConstants.TYPE_RELATED);
936    
937                    return assetEntry;
938            }
939    
940            @Override
941            public AssetEntry updateAsset(
942                            long userId, Folder folder, long[] assetCategoryIds,
943                            String[] assetTagNames, long[] assetLinkEntryIds)
944                    throws PortalException {
945    
946                    AssetEntry assetEntry = null;
947    
948                    boolean visible = false;
949    
950                    if (folder instanceof LiferayFolder) {
951                            DLFolder dlFolder = (DLFolder)folder.getModel();
952    
953                            if (dlFolder.isApproved() && !dlFolder.isHidden() &&
954                                    !dlFolder.isInHiddenFolder()) {
955    
956                                    visible = true;
957                            }
958                    }
959                    else {
960                            visible = true;
961                    }
962    
963                    assetEntry = assetEntryLocalService.updateEntry(
964                            userId, folder.getGroupId(), folder.getCreateDate(),
965                            folder.getModifiedDate(), DLFolderConstants.getClassName(),
966                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
967                            assetTagNames, visible, null, null, null, null, folder.getName(),
968                            folder.getDescription(), null, null, null, 0, 0, null);
969    
970                    assetLinkLocalService.updateLinks(
971                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
972                            AssetLinkConstants.TYPE_RELATED);
973    
974                    return assetEntry;
975            }
976    
977            @Override
978            public void updateFileEntry(
979                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
980                            FileVersion destinationFileVersion, long assetClassPk)
981                    throws PortalException {
982    
983                    if (!DLAppHelperThreadLocal.isEnabled()) {
984                            return;
985                    }
986    
987                    boolean updateAsset = true;
988    
989                    if (fileEntry instanceof LiferayFileEntry &&
990                            fileEntry.getVersion().equals(
991                                    destinationFileVersion.getVersion())) {
992    
993                            updateAsset = false;
994                    }
995    
996                    if (updateAsset) {
997                            updateAsset(
998                                    userId, fileEntry, destinationFileVersion, assetClassPk);
999                    }
1000            }
1001    
1002            @Override
1003            public void updateFileEntry(
1004                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
1005                            FileVersion destinationFileVersion, ServiceContext serviceContext)
1006                    throws PortalException {
1007    
1008                    if (!DLAppHelperThreadLocal.isEnabled()) {
1009                            return;
1010                    }
1011    
1012                    updateAsset(
1013                            userId, fileEntry, destinationFileVersion,
1014                            serviceContext.getAssetCategoryIds(),
1015                            serviceContext.getAssetTagNames(),
1016                            serviceContext.getAssetLinkEntryIds());
1017            }
1018    
1019            @Override
1020            public void updateFolder(
1021                            long userId, Folder folder, ServiceContext serviceContext)
1022                    throws PortalException {
1023    
1024                    updateAsset(
1025                            userId, folder, serviceContext.getAssetCategoryIds(),
1026                            serviceContext.getAssetTagNames(),
1027                            serviceContext.getAssetLinkEntryIds());
1028            }
1029    
1030            @Override
1031            public void updateStatus(
1032                            long userId, FileEntry fileEntry, FileVersion latestFileVersion,
1033                            int oldStatus, int newStatus, ServiceContext serviceContext,
1034                            Map<String, Serializable> workflowContext)
1035                    throws PortalException {
1036    
1037                    if (!DLAppHelperThreadLocal.isEnabled()) {
1038                            return;
1039                    }
1040    
1041                    if (newStatus == WorkflowConstants.STATUS_APPROVED) {
1042    
1043                            // Asset
1044    
1045                            String latestFileVersionVersion = latestFileVersion.getVersion();
1046    
1047                            if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
1048                                    if (!latestFileVersionVersion.equals(
1049                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
1050    
1051                                            AssetEntry draftAssetEntry =
1052                                                    assetEntryLocalService.fetchEntry(
1053                                                            DLFileEntryConstants.getClassName(),
1054                                                            latestFileVersion.getPrimaryKey());
1055    
1056                                            if (draftAssetEntry != null) {
1057                                                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
1058    
1059                                                    long[] assetCategoryIds =
1060                                                            draftAssetEntry.getCategoryIds();
1061                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
1062    
1063                                                    List<AssetLink> assetLinks =
1064                                                            assetLinkLocalService.getDirectLinks(
1065                                                                    draftAssetEntry.getEntryId(),
1066                                                                    AssetLinkConstants.TYPE_RELATED);
1067    
1068                                                    long[] assetLinkEntryIds = ListUtil.toLongArray(
1069                                                            assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
1070    
1071                                                    AssetEntry assetEntry =
1072                                                            assetEntryLocalService.updateEntry(
1073                                                                    userId, fileEntry.getGroupId(),
1074                                                                    fileEntry.getCreateDate(),
1075                                                                    fileEntry.getModifiedDate(),
1076                                                                    DLFileEntryConstants.getClassName(),
1077                                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
1078                                                                    fileEntryTypeId, assetCategoryIds,
1079                                                                    assetTagNames, true, null, null, null,
1080                                                                    draftAssetEntry.getMimeType(),
1081                                                                    fileEntry.getTitle(),
1082                                                                    fileEntry.getDescription(), null, null, null, 0,
1083                                                                    0, null);
1084    
1085                                                    assetLinkLocalService.updateLinks(
1086                                                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
1087                                                            AssetLinkConstants.TYPE_RELATED);
1088    
1089                                                    assetEntryLocalService.deleteEntry(draftAssetEntry);
1090                                            }
1091                                    }
1092    
1093                                    AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
1094                                            DLFileEntryConstants.getClassName(),
1095                                            fileEntry.getFileEntryId());
1096    
1097                                    if (assetEntry != null) {
1098                                            assetEntryLocalService.updateVisible(
1099                                                    DLFileEntryConstants.getClassName(),
1100                                                    fileEntry.getFileEntryId(), true);
1101                                    }
1102                            }
1103    
1104                            // Sync
1105    
1106                            String event = GetterUtil.getString(workflowContext.get("event"));
1107    
1108                            if (Validator.isNotNull(event)) {
1109                                    triggerRepositoryEvent(
1110                                            fileEntry.getRepositoryId(),
1111                                            getWorkflowRepositoryEventTypeClass(event), FileEntry.class,
1112                                            fileEntry);
1113                            }
1114    
1115                            if ((oldStatus != WorkflowConstants.STATUS_IN_TRASH) &&
1116                                    !fileEntry.isInTrash()) {
1117    
1118                                    // Social
1119    
1120                                    Date activityCreateDate = latestFileVersion.getModifiedDate();
1121                                    int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
1122    
1123                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
1124                                            activityCreateDate = latestFileVersion.getCreateDate();
1125                                            activityType = DLActivityKeys.ADD_FILE_ENTRY;
1126                                    }
1127    
1128                                    JSONObject extraDataJSONObject =
1129                                            JSONFactoryUtil.createJSONObject();
1130    
1131                                    extraDataJSONObject.put("title", fileEntry.getTitle());
1132    
1133                                    SocialActivityManagerUtil.addUniqueActivity(
1134                                            latestFileVersion.getStatusByUserId(), activityCreateDate,
1135                                            fileEntry, activityType, extraDataJSONObject.toString(), 0);
1136    
1137                                    // Subscriptions
1138    
1139                                    notifySubscribers(
1140                                            userId, latestFileVersion,
1141                                            (String)workflowContext.get(WorkflowConstants.CONTEXT_URL),
1142                                            serviceContext);
1143                            }
1144                    }
1145                    else {
1146    
1147                            // Asset
1148    
1149                            boolean visible = false;
1150    
1151                            if (newStatus != WorkflowConstants.STATUS_IN_TRASH) {
1152                                    List<DLFileVersion> approvedFileVersions =
1153                                            dlFileVersionPersistence.findByF_S(
1154                                                    fileEntry.getFileEntryId(),
1155                                                    WorkflowConstants.STATUS_APPROVED);
1156    
1157                                    if (!approvedFileVersions.isEmpty()) {
1158                                            visible = true;
1159                                    }
1160                            }
1161    
1162                            assetEntryLocalService.updateVisible(
1163                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1164                                    visible);
1165                    }
1166            }
1167    
1168            protected FileEntry doMoveFileEntryFromTrash(
1169                            long userId, FileEntry fileEntry, long newFolderId,
1170                            ServiceContext serviceContext)
1171                    throws PortalException {
1172    
1173                    // File entry
1174    
1175                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1176    
1177                    if (dlFileEntry.isInTrashExplicitly()) {
1178                            restoreFileEntryFromTrash(userId, fileEntry);
1179    
1180                            fileEntry = dlAppLocalService.moveFileEntry(
1181                                    userId, fileEntry.getFileEntryId(), newFolderId,
1182                                    serviceContext);
1183    
1184                            if (DLAppHelperThreadLocal.isEnabled()) {
1185                                    dlFileRankLocalService.enableFileRanks(
1186                                            fileEntry.getFileEntryId());
1187                            }
1188    
1189                            return fileEntry;
1190                    }
1191    
1192                    List<DLFileVersion> dlFileVersions =
1193                            dlFileVersionLocalService.getFileVersions(
1194                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_IN_TRASH);
1195    
1196                    dlFileVersions = ListUtil.sort(
1197                            dlFileVersions, new DLFileVersionVersionComparator());
1198    
1199                    FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
1200    
1201                    TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1202                            DLFileVersion.class.getName(), fileVersion.getFileVersionId());
1203    
1204                    int oldStatus = WorkflowConstants.STATUS_APPROVED;
1205    
1206                    if (trashVersion != null) {
1207                            oldStatus = trashVersion.getStatus();
1208                    }
1209    
1210                    dlFileEntryLocalService.updateStatus(
1211                            userId, fileVersion.getFileVersionId(), oldStatus, serviceContext,
1212                            new HashMap<String, Serializable>());
1213    
1214                    // File versions
1215    
1216                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1217    
1218                            // File version
1219    
1220                            trashVersion = trashVersionLocalService.fetchVersion(
1221                                    DLFileVersion.class.getName(),
1222                                    dlFileVersion.getFileVersionId());
1223    
1224                            oldStatus = WorkflowConstants.STATUS_APPROVED;
1225    
1226                            if (trashVersion != null) {
1227                                    oldStatus = trashVersion.getStatus();
1228                            }
1229    
1230                            dlFileVersion.setStatus(oldStatus);
1231    
1232                            dlFileVersionPersistence.update(dlFileVersion);
1233    
1234                            // Trash
1235    
1236                            if (trashVersion != null) {
1237                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
1238                            }
1239                    }
1240    
1241                    if (DLAppHelperThreadLocal.isEnabled()) {
1242    
1243                            // File rank
1244    
1245                            dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
1246    
1247                            // File shortcut
1248    
1249                            dlFileShortcutLocalService.enableFileShortcuts(
1250                                    fileEntry.getFileEntryId());
1251                    }
1252    
1253                    // App helper
1254    
1255                    fileEntry = dlAppService.moveFileEntry(
1256                            fileEntry.getFileEntryId(), newFolderId, serviceContext);
1257    
1258                    // Sync
1259    
1260                    triggerRepositoryEvent(
1261                            fileEntry.getRepositoryId(),
1262                            TrashRepositoryEventType.EntryRestored.class, FileEntry.class,
1263                            fileEntry);
1264    
1265                    // Social
1266    
1267                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1268    
1269                    extraDataJSONObject.put("title", fileEntry.getTitle());
1270    
1271                    SocialActivityManagerUtil.addActivity(
1272                            userId, fileEntry, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1273                            extraDataJSONObject.toString(), 0);
1274    
1275                    return fileEntry;
1276            }
1277    
1278            protected FileEntry doMoveFileEntryToTrash(long userId, FileEntry fileEntry)
1279                    throws PortalException {
1280    
1281                    // File versions
1282    
1283                    List<DLFileVersion> dlFileVersions =
1284                            dlFileVersionLocalService.getFileVersions(
1285                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1286    
1287                    dlFileVersions = ListUtil.sort(
1288                            dlFileVersions, new DLFileVersionVersionComparator());
1289    
1290                    List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1291                            new ArrayList<>();
1292    
1293                    if ((dlFileVersions != null) && !dlFileVersions.isEmpty()) {
1294                            dlFileVersionStatusOVPs = getDlFileVersionStatuses(dlFileVersions);
1295                    }
1296    
1297                    FileVersion fileVersion = fileEntry.getFileVersion();
1298    
1299                    int oldStatus = fileVersion.getStatus();
1300    
1301                    dlFileEntryLocalService.updateStatus(
1302                            userId, fileVersion.getFileVersionId(),
1303                            WorkflowConstants.STATUS_IN_TRASH, new ServiceContext(),
1304                            new HashMap<String, Serializable>());
1305    
1306                    if (DLAppHelperThreadLocal.isEnabled()) {
1307    
1308                            // File shortcut
1309    
1310                            dlFileShortcutLocalService.disableFileShortcuts(
1311                                    fileEntry.getFileEntryId());
1312    
1313                            // File rank
1314    
1315                            dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());
1316    
1317                            // Sync
1318    
1319                            triggerRepositoryEvent(
1320                                    fileEntry.getRepositoryId(),
1321                                    TrashRepositoryEventType.EntryTrashed.class, FileEntry.class,
1322                                    fileEntry);
1323                    }
1324    
1325                    // Trash
1326    
1327                    DLFileVersion oldDLFileVersion = (DLFileVersion)fileVersion.getModel();
1328    
1329                    int oldDLFileVersionStatus = oldDLFileVersion.getStatus();
1330    
1331                    for (DLFileVersion curDLFileVersion : dlFileVersions) {
1332                            curDLFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1333    
1334                            dlFileVersionPersistence.update(curDLFileVersion);
1335                    }
1336    
1337                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1338    
1339                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1340    
1341                    typeSettingsProperties.put("fileName", dlFileEntry.getFileName());
1342                    typeSettingsProperties.put("title", dlFileEntry.getTitle());
1343    
1344                    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1345                            userId, dlFileEntry.getGroupId(),
1346                            DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId(),
1347                            dlFileEntry.getUuid(), dlFileEntry.getClassName(),
1348                            oldDLFileVersionStatus, dlFileVersionStatusOVPs,
1349                            typeSettingsProperties);
1350    
1351                    String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
1352    
1353                    dlFileEntry.setFileName(trashTitle);
1354                    dlFileEntry.setTitle(trashTitle);
1355    
1356                    dlFileEntryPersistence.update(dlFileEntry);
1357    
1358                    if (!DLAppHelperThreadLocal.isEnabled()) {
1359                            return fileEntry;
1360                    }
1361    
1362                    // Social
1363    
1364                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1365    
1366                    extraDataJSONObject.put(
1367                            "title", TrashUtil.getOriginalTitle(fileEntry.getTitle()));
1368    
1369                    SocialActivityManagerUtil.addActivity(
1370                            userId, fileEntry, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1371                            extraDataJSONObject.toString(), 0);
1372    
1373                    // Workflow
1374    
1375                    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1376                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
1377                                    fileVersion.getCompanyId(), fileVersion.getGroupId(),
1378                                    DLFileEntryConstants.getClassName(),
1379                                    fileVersion.getFileVersionId());
1380                    }
1381    
1382                    return fileEntry;
1383            }
1384    
1385            protected Folder doMoveFolderFromTrash(
1386                            long userId, Folder folder, long parentFolderId,
1387                            ServiceContext serviceContext)
1388                    throws PortalException {
1389    
1390                    DLFolder dlFolder = (DLFolder)folder.getModel();
1391    
1392                    if (dlFolder.isInTrashExplicitly()) {
1393                            restoreFolderFromTrash(userId, folder);
1394                    }
1395                    else {
1396    
1397                            // Folder
1398    
1399                            TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1400                                    DLFolder.class.getName(), dlFolder.getFolderId());
1401    
1402                            int status = WorkflowConstants.STATUS_APPROVED;
1403    
1404                            if (trashVersion != null) {
1405                                    status = trashVersion.getStatus();
1406                            }
1407    
1408                            dlFolderLocalService.updateStatus(
1409                                    userId, folder.getFolderId(), status,
1410                                    new HashMap<String, Serializable>(), new ServiceContext());
1411    
1412                            // File rank
1413    
1414                            dlFileRankLocalService.enableFileRanksByFolderId(
1415                                    folder.getFolderId());
1416    
1417                            // Trash
1418    
1419                            if (trashVersion != null) {
1420                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
1421                            }
1422    
1423                            // Folders, file entries, and file shortcuts
1424    
1425                            restoreDependentsFromTrash(dlFolder);
1426    
1427                            // Sync
1428    
1429                            triggerRepositoryEvent(
1430                                    folder.getRepositoryId(),
1431                                    TrashRepositoryEventType.EntryRestored.class, Folder.class,
1432                                    folder);
1433    
1434                            // Social
1435    
1436                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1437    
1438                            extraDataJSONObject.put("title", folder.getName());
1439    
1440                            SocialActivityManagerUtil.addActivity(
1441                                    userId, folder, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1442                                    extraDataJSONObject.toString(), 0);
1443                    }
1444    
1445                    return dlAppLocalService.moveFolder(
1446                            userId, folder.getFolderId(), parentFolderId, serviceContext);
1447            }
1448    
1449            protected Folder doMoveFolderToTrash(long userId, Folder folder)
1450                    throws PortalException {
1451    
1452                    // Folder
1453    
1454                    DLFolder dlFolder = dlFolderLocalService.updateStatus(
1455                            userId, folder.getFolderId(), WorkflowConstants.STATUS_IN_TRASH,
1456                            new HashMap<String, Serializable>(), new ServiceContext());
1457    
1458                    // File rank
1459    
1460                    dlFileRankLocalService.disableFileRanksByFolderId(folder.getFolderId());
1461    
1462                    // Trash
1463    
1464                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1465    
1466                    typeSettingsProperties.put("title", dlFolder.getName());
1467    
1468                    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1469                            userId, dlFolder.getGroupId(), DLFolderConstants.getClassName(),
1470                            dlFolder.getFolderId(), dlFolder.getUuid(), null,
1471                            WorkflowConstants.STATUS_APPROVED, null, typeSettingsProperties);
1472    
1473                    dlFolder.setName(TrashUtil.getTrashTitle(trashEntry.getEntryId()));
1474    
1475                    dlFolderPersistence.update(dlFolder);
1476    
1477                    // Folders, file entries, and file shortcuts
1478    
1479                    moveDependentsToTrash(dlFolder);
1480    
1481                    // Sync
1482    
1483                    triggerRepositoryEvent(
1484                            folder.getRepositoryId(),
1485                            TrashRepositoryEventType.EntryTrashed.class, Folder.class, folder);
1486    
1487                    // Social
1488    
1489                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1490    
1491                    extraDataJSONObject.put("title", folder.getName());
1492    
1493                    SocialActivityManagerUtil.addActivity(
1494                            userId, folder, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1495                            extraDataJSONObject.toString(), 0);
1496    
1497                    return new LiferayFolder(dlFolder);
1498            }
1499    
1500            protected List<ObjectValuePair<Long, Integer>> getDlFileVersionStatuses(
1501                    List<DLFileVersion> dlFileVersions) {
1502    
1503                    List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1504                            new ArrayList<>(dlFileVersions.size());
1505    
1506                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1507                            int status = dlFileVersion.getStatus();
1508    
1509                            if (status == WorkflowConstants.STATUS_PENDING) {
1510                                    status = WorkflowConstants.STATUS_DRAFT;
1511                            }
1512    
1513                            ObjectValuePair<Long, Integer> dlFileVersionStatusOVP =
1514                                    new ObjectValuePair<>(dlFileVersion.getFileVersionId(), status);
1515    
1516                            dlFileVersionStatusOVPs.add(dlFileVersionStatusOVP);
1517                    }
1518    
1519                    return dlFileVersionStatusOVPs;
1520            }
1521    
1522            protected long getFileEntryTypeId(FileEntry fileEntry) {
1523                    if (fileEntry instanceof LiferayFileEntry) {
1524                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1525    
1526                            return dlFileEntry.getFileEntryTypeId();
1527                    }
1528    
1529                    return 0;
1530            }
1531    
1532            protected Class<? extends WorkflowRepositoryEventType>
1533                    getWorkflowRepositoryEventTypeClass(
1534                            String syncEvent) {
1535    
1536                    if (syncEvent.equals(DLSyncConstants.EVENT_ADD)) {
1537                            return WorkflowRepositoryEventType.Add.class;
1538                    }
1539                    else if (syncEvent.equals(DLSyncConstants.EVENT_UPDATE)) {
1540                            return WorkflowRepositoryEventType.Update.class;
1541                    }
1542                    else {
1543                            throw new IllegalArgumentException(
1544                                    String.format("Unsupported sync event %s", syncEvent));
1545                    }
1546            }
1547    
1548            protected void notifySubscribers(
1549                            long userId, FileVersion fileVersion, String entryURL,
1550                            ServiceContext serviceContext)
1551                    throws PortalException {
1552    
1553                    if (!fileVersion.isApproved() || Validator.isNull(entryURL)) {
1554                            return;
1555                    }
1556    
1557                    DLGroupServiceSettings dlGroupServiceSettings =
1558                            DLGroupServiceSettings.getInstance(fileVersion.getGroupId());
1559    
1560                    boolean commandUpdate = false;
1561    
1562                    if (serviceContext.isCommandUpdate() ||
1563                            Constants.CHECKIN.equals(serviceContext.getCommand())) {
1564    
1565                            commandUpdate = true;
1566                    }
1567    
1568                    if (serviceContext.isCommandAdd() &&
1569                            dlGroupServiceSettings.isEmailFileEntryAddedEnabled()) {
1570                    }
1571                    else if (commandUpdate &&
1572                                     dlGroupServiceSettings.isEmailFileEntryUpdatedEnabled()) {
1573                    }
1574                    else {
1575                            return;
1576                    }
1577    
1578                    String entryTitle = fileVersion.getTitle();
1579    
1580                    String fromName = dlGroupServiceSettings.getEmailFromName();
1581                    String fromAddress = dlGroupServiceSettings.getEmailFromAddress();
1582    
1583                    LocalizedValuesMap subjectLocalizedValuesMap = null;
1584                    LocalizedValuesMap bodyLocalizedValuesMap = null;
1585    
1586                    if (commandUpdate) {
1587                            subjectLocalizedValuesMap =
1588                                    dlGroupServiceSettings.getEmailFileEntryUpdatedSubject();
1589                            bodyLocalizedValuesMap =
1590                                    dlGroupServiceSettings.getEmailFileEntryUpdatedBody();
1591                    }
1592                    else {
1593                            subjectLocalizedValuesMap =
1594                                    dlGroupServiceSettings.getEmailFileEntryAddedSubject();
1595                            bodyLocalizedValuesMap =
1596                                    dlGroupServiceSettings.getEmailFileEntryAddedBody();
1597                    }
1598    
1599                    FileEntry fileEntry = fileVersion.getFileEntry();
1600    
1601                    Folder folder = null;
1602    
1603                    long folderId = fileEntry.getFolderId();
1604    
1605                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1606                            folder = dlAppLocalService.getFolder(folderId);
1607                    }
1608    
1609                    String folderName = LanguageUtil.get(
1610                            serviceContext.getLocale(), "home");
1611    
1612                    if (folder != null) {
1613                            folderName = folder.getName();
1614                    }
1615    
1616                    SubscriptionSender subscriptionSender =
1617                            new GroupSubscriptionCheckSubscriptionSender(
1618                                    DLPermission.RESOURCE_NAME);
1619    
1620                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1621    
1622                    DLFileEntryType dlFileEntryType =
1623                            dlFileEntryTypeLocalService.getDLFileEntryType(
1624                                    dlFileEntry.getFileEntryTypeId());
1625    
1626                    subscriptionSender.setClassPK(fileVersion.getFileEntryId());
1627                    subscriptionSender.setClassName(DLFileEntryConstants.getClassName());
1628                    subscriptionSender.setCompanyId(fileVersion.getCompanyId());
1629                    subscriptionSender.setContextAttributes(
1630                            "[$DOCUMENT_STATUS_BY_USER_NAME$]",
1631                            fileVersion.getStatusByUserName(), "[$DOCUMENT_TITLE$]", entryTitle,
1632                            "[$DOCUMENT_TYPE$]",
1633                            dlFileEntryType.getName(serviceContext.getLocale()),
1634                            "[$DOCUMENT_URL$]", entryURL, "[$FOLDER_NAME$]", folderName);
1635                    subscriptionSender.setContextCreatorUserPrefix("DOCUMENT");
1636                    subscriptionSender.setCreatorUserId(fileVersion.getUserId());
1637                    subscriptionSender.setCurrentUserId(userId);
1638                    subscriptionSender.setEntryTitle(entryTitle);
1639                    subscriptionSender.setEntryURL(entryURL);
1640                    subscriptionSender.setFrom(fromAddress, fromName);
1641                    subscriptionSender.setHtmlFormat(true);
1642                    subscriptionSender.setLocalizedBodyMap(
1643                            LocalizationUtil.getMap(bodyLocalizedValuesMap));
1644                    subscriptionSender.setLocalizedSubjectMap(
1645                            LocalizationUtil.getMap(subjectLocalizedValuesMap));
1646                    subscriptionSender.setMailId(
1647                            "file_entry", fileVersion.getFileEntryId());
1648    
1649                    int notificationType =
1650                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY;
1651    
1652                    if (commandUpdate) {
1653                            notificationType =
1654                                    UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY;
1655                    }
1656    
1657                    subscriptionSender.setNotificationType(notificationType);
1658    
1659                    String portletId = PortletProviderUtil.getPortletId(
1660                            FileEntry.class.getName(), PortletProvider.Action.VIEW);
1661    
1662                    subscriptionSender.setPortletId(portletId);
1663    
1664                    subscriptionSender.setReplyToAddress(fromAddress);
1665                    subscriptionSender.setScopeGroupId(fileVersion.getGroupId());
1666                    subscriptionSender.setServiceContext(serviceContext);
1667    
1668                    subscriptionSender.addPersistedSubscribers(
1669                            DLFolder.class.getName(), fileVersion.getGroupId());
1670    
1671                    if (folder != null) {
1672                            subscriptionSender.addPersistedSubscribers(
1673                                    DLFolder.class.getName(), folder.getFolderId());
1674    
1675                            for (Long ancestorFolderId : folder.getAncestorFolderIds()) {
1676                                    subscriptionSender.addPersistedSubscribers(
1677                                            DLFolder.class.getName(), ancestorFolderId);
1678                            }
1679                    }
1680    
1681                    if (dlFileEntryType.getFileEntryTypeId() ==
1682                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
1683    
1684                            subscriptionSender.addPersistedSubscribers(
1685                                    DLFileEntryType.class.getName(), fileVersion.getGroupId());
1686                    }
1687                    else {
1688                            subscriptionSender.addPersistedSubscribers(
1689                                    DLFileEntryType.class.getName(),
1690                                    dlFileEntryType.getFileEntryTypeId());
1691                    }
1692    
1693                    subscriptionSender.addPersistedSubscribers(
1694                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
1695    
1696                    subscriptionSender.flushNotificationsAsync();
1697            }
1698    
1699            protected void trashOrRestoreFolder(DLFolder dlFolder, boolean moveToTrash)
1700                    throws PortalException {
1701    
1702                    TrashEntry trashEntry = null;
1703    
1704                    if (moveToTrash) {
1705                            trashEntry = trashEntryLocalService.getEntry(
1706                                    DLFolderConstants.getClassName(), dlFolder.getFolderId());
1707                    }
1708    
1709                    long dlFileEntryClassNameId = classNameLocalService.getClassNameId(
1710                            DLFileEntry.class);
1711    
1712                    List<AssetEntry> dlFileEntryAssetEntries =
1713                            assetEntryFinder.findByDLFileEntryC_T(
1714                                    dlFileEntryClassNameId, dlFolder.getTreePath());
1715    
1716                    for (AssetEntry dlFileEntryAssetEntry : dlFileEntryAssetEntries) {
1717                            assetEntryLocalService.updateVisible(
1718                                    dlFileEntryAssetEntry, !moveToTrash);
1719                    }
1720    
1721                    long dlFolderClassNameId = classNameLocalService.getClassNameId(
1722                            DLFolder.class);
1723    
1724                    List<AssetEntry> dlFolderAssetEntries =
1725                            assetEntryFinder.findByDLFolderC_T(
1726                                    dlFolderClassNameId, dlFolder.getTreePath());
1727    
1728                    for (AssetEntry dlFolderAssetEntry : dlFolderAssetEntries) {
1729                            assetEntryLocalService.updateVisible(
1730                                    dlFolderAssetEntry, !moveToTrash);
1731                    }
1732    
1733                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_M_T_H(
1734                            dlFolder.getGroupId(), false,
1735                            CustomSQLUtil.keywords(
1736                                    dlFolder.getTreePath(), WildcardMode.TRAILING)[0],
1737                            false);
1738    
1739                    for (DLFolder childDLFolder : dlFolders) {
1740                            trashOrRestoreFolder(
1741                                    dlFolder, childDLFolder, moveToTrash, trashEntry);
1742                    }
1743            }
1744    
1745            protected void trashOrRestoreFolder(
1746                            DLFolder dlFolder, DLFolder childDLFolder, boolean moveToTrash,
1747                            TrashEntry trashEntry)
1748                    throws PortalException {
1749    
1750                    List<DLFileEntry> dlFileEntries =
1751                            dlFileEntryLocalService.getFileEntries(
1752                                    childDLFolder.getGroupId(), childDLFolder.getFolderId());
1753    
1754                    for (DLFileEntry dlFileEntry : dlFileEntries) {
1755                            if (moveToTrash) {
1756                                    if (dlFileEntry.isInTrashExplicitly()) {
1757                                            continue;
1758                                    }
1759                            }
1760                            else if (!dlFileEntry.isInTrashImplicitly()) {
1761                                    continue;
1762                            }
1763    
1764                            // File shortcut
1765    
1766                            dlFileShortcutLocalService.updateFileShortcutsActive(
1767                                    dlFileEntry.getFileEntryId(), !moveToTrash);
1768    
1769                            // File versions
1770    
1771                            List<DLFileVersion> dlFileVersions = null;
1772    
1773                            if (moveToTrash) {
1774                                    dlFileVersions = dlFileVersionLocalService.getFileVersions(
1775                                            dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1776                            }
1777                            else {
1778                                    dlFileVersions = dlFileVersionLocalService.getFileVersions(
1779                                            dlFileEntry.getFileEntryId(),
1780                                            WorkflowConstants.STATUS_IN_TRASH);
1781                            }
1782    
1783                            for (DLFileVersion dlFileVersion : dlFileVersions) {
1784    
1785                                    // File version
1786    
1787                                    if (moveToTrash) {
1788                                            int oldStatus = dlFileVersion.getStatus();
1789    
1790                                            dlFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1791    
1792                                            dlFileVersionPersistence.update(dlFileVersion);
1793    
1794                                            // Trash
1795    
1796                                            if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1797                                                    int newStatus = oldStatus;
1798    
1799                                                    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1800                                                            newStatus = WorkflowConstants.STATUS_DRAFT;
1801                                                    }
1802    
1803                                                    trashVersionLocalService.addTrashVersion(
1804                                                            trashEntry.getEntryId(),
1805                                                            DLFileVersion.class.getName(),
1806                                                            dlFileVersion.getFileVersionId(), newStatus, null);
1807                                            }
1808    
1809                                            // Workflow
1810    
1811                                            if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1812                                                    workflowInstanceLinkLocalService.
1813                                                            deleteWorkflowInstanceLink(
1814                                                                    dlFileVersion.getCompanyId(),
1815                                                                    dlFileVersion.getGroupId(),
1816                                                                    DLFileEntryConstants.getClassName(),
1817                                                                    dlFileVersion.getFileVersionId());
1818                                            }
1819                                    }
1820                                    else {
1821                                            TrashVersion trashVersion =
1822                                                    trashVersionLocalService.fetchVersion(
1823                                                            DLFileVersion.class.getName(),
1824                                                            dlFileVersion.getFileVersionId());
1825    
1826                                            int oldStatus = WorkflowConstants.STATUS_APPROVED;
1827    
1828                                            if (trashVersion != null) {
1829                                                    oldStatus = trashVersion.getStatus();
1830                                            }
1831    
1832                                            dlFileVersion.setStatus(oldStatus);
1833    
1834                                            dlFileVersionPersistence.update(dlFileVersion);
1835    
1836                                            // Trash
1837    
1838                                            if (trashVersion != null) {
1839                                                    trashVersionLocalService.deleteTrashVersion(
1840                                                            trashVersion);
1841                                            }
1842                                    }
1843                            }
1844    
1845                            // Index
1846    
1847                            Indexer<DLFileEntry> indexer =
1848                                    IndexerRegistryUtil.nullSafeGetIndexer(DLFileEntry.class);
1849    
1850                            indexer.reindex(dlFileEntry);
1851                    }
1852    
1853                    List<DLFileShortcut> dlFileShortcuts =
1854                            dlFileShortcutPersistence.findByG_F(
1855                                    childDLFolder.getGroupId(), childDLFolder.getFolderId());
1856    
1857                    for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
1858                            if (moveToTrash) {
1859                                    if (dlFileShortcut.isInTrashExplicitly()) {
1860                                            continue;
1861                                    }
1862    
1863                                    int oldStatus = dlFileShortcut.getStatus();
1864    
1865                                    dlFileShortcut.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1866    
1867                                    dlFileShortcutPersistence.update(dlFileShortcut);
1868    
1869                                    // Trash
1870    
1871                                    if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1872                                            trashVersionLocalService.addTrashVersion(
1873                                                    trashEntry.getEntryId(),
1874                                                    DLFileShortcutConstants.getClassName(),
1875                                                    dlFileShortcut.getFileShortcutId(), oldStatus, null);
1876                                    }
1877                            }
1878                            else {
1879                                    if (!dlFileShortcut.isInTrashImplicitly()) {
1880                                            continue;
1881                                    }
1882    
1883                                    TrashVersion trashVersion =
1884                                            trashVersionLocalService.fetchVersion(
1885                                                    DLFileShortcutConstants.getClassName(),
1886                                                    dlFileShortcut.getFileShortcutId());
1887    
1888                                    int oldStatus = WorkflowConstants.STATUS_APPROVED;
1889    
1890                                    if (trashVersion != null) {
1891                                            oldStatus = trashVersion.getStatus();
1892                                    }
1893    
1894                                    dlFileShortcut.setStatus(oldStatus);
1895    
1896                                    dlFileShortcutPersistence.update(dlFileShortcut);
1897    
1898                                    if (trashVersion != null) {
1899                                            trashVersionLocalService.deleteTrashVersion(trashVersion);
1900                                    }
1901                            }
1902                    }
1903    
1904                    if (childDLFolder.equals(dlFolder)) {
1905                            return;
1906                    }
1907    
1908                    if (moveToTrash) {
1909                            if (childDLFolder.isInTrashExplicitly()) {
1910                                    return;
1911                            }
1912    
1913                            int oldStatus = childDLFolder.getStatus();
1914    
1915                            childDLFolder.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1916    
1917                            dlFolderPersistence.update(childDLFolder);
1918    
1919                            // Trash
1920    
1921                            if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1922                                    trashVersionLocalService.addTrashVersion(
1923                                            trashEntry.getEntryId(), DLFolder.class.getName(),
1924                                            childDLFolder.getFolderId(), oldStatus, null);
1925                            }
1926                    }
1927                    else {
1928                            if (!childDLFolder.isInTrashImplicitly()) {
1929                                    return;
1930                            }
1931    
1932                            TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1933                                    DLFolder.class.getName(), childDLFolder.getFolderId());
1934    
1935                            int oldStatus = WorkflowConstants.STATUS_APPROVED;
1936    
1937                            if (trashVersion != null) {
1938                                    oldStatus = trashVersion.getStatus();
1939                            }
1940    
1941                            childDLFolder.setStatus(oldStatus);
1942    
1943                            dlFolderPersistence.update(childDLFolder);
1944    
1945                            // Trash
1946    
1947                            if (trashVersion != null) {
1948                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
1949                            }
1950                    }
1951    
1952                    // Index
1953    
1954                    Indexer<DLFolder> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1955                            DLFolder.class);
1956    
1957                    indexer.reindex(childDLFolder);
1958            }
1959    
1960            protected <T extends RepositoryModel<T>> void triggerRepositoryEvent(
1961                            long repositoryId,
1962                            Class<? extends RepositoryEventType> repositoryEventType,
1963                            Class<T> modelClass, T target)
1964                    throws PortalException {
1965    
1966                    Repository repository = RepositoryProviderUtil.getRepository(
1967                            repositoryId);
1968    
1969                    if (repository.isCapabilityProvided(
1970                                    RepositoryEventTriggerCapability.class)) {
1971    
1972                            RepositoryEventTriggerCapability repositoryEventTriggerCapability =
1973                                    repository.getCapability(
1974                                            RepositoryEventTriggerCapability.class);
1975    
1976                            repositoryEventTriggerCapability.trigger(
1977                                    repositoryEventType, modelClass, target);
1978                    }
1979            }
1980    
1981    }