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