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