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