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