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());
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                    dlFileEntry.setTitle(
1019                            TrashUtil.getOriginalTitle(dlFileEntry.getTitle()));
1020    
1021                    dlFileEntryPersistence.update(dlFileEntry);
1022    
1023                    FileVersion fileVersion = fileEntry.getFileVersion();
1024    
1025                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
1026                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
1027    
1028                    dlFileEntryLocalService.updateStatus(
1029                            userId, fileVersion.getFileVersionId(), trashEntry.getStatus(),
1030                            new HashMap<String, Serializable>(), new ServiceContext());
1031    
1032                    if (DLAppHelperThreadLocal.isEnabled()) {
1033    
1034                            // File rank
1035    
1036                            dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
1037    
1038                            // File shortcut
1039    
1040                            dlFileShortcutLocalService.enableFileShortcuts(
1041                                    fileEntry.getFileEntryId());
1042    
1043                            // Sync
1044    
1045                            registerDLSyncEventCallback(
1046                                    DLSyncConstants.EVENT_RESTORE_FROM_TRASH, fileEntry);
1047                    }
1048    
1049                    // Trash
1050    
1051                    List<TrashVersion> trashVersions = trashVersionLocalService.getVersions(
1052                            trashEntry.getEntryId());
1053    
1054                    for (TrashVersion trashVersion : trashVersions) {
1055                            DLFileVersion trashDLFileVersion =
1056                                    dlFileVersionPersistence.findByPrimaryKey(
1057                                            trashVersion.getClassPK());
1058    
1059                            trashDLFileVersion.setStatus(trashVersion.getStatus());
1060    
1061                            dlFileVersionPersistence.update(trashDLFileVersion);
1062                    }
1063    
1064                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
1065    
1066                    if (!DLAppHelperThreadLocal.isEnabled()) {
1067                            return;
1068                    }
1069    
1070                    // Social
1071    
1072                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1073    
1074                    extraDataJSONObject.put("title", fileEntry.getTitle());
1075    
1076                    socialActivityLocalService.addActivity(
1077                            userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
1078                            fileEntry.getFileEntryId(),
1079                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1080                            extraDataJSONObject.toString(), 0);
1081            }
1082    
1083            @Override
1084            public void restoreFileShortcutFromTrash(
1085                            long userId, DLFileShortcut dlFileShortcut)
1086                    throws PortalException, SystemException {
1087    
1088                    // File shortcut
1089    
1090                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
1091                            DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId());
1092    
1093                    dlFileShortcutLocalService.updateStatus(
1094                            userId, dlFileShortcut.getFileShortcutId(), trashEntry.getStatus(),
1095                            new ServiceContext());
1096    
1097                    // Social
1098    
1099                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1100    
1101                    extraDataJSONObject.put("title", dlFileShortcut.getToTitle());
1102    
1103                    socialActivityLocalService.addActivity(
1104                            userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
1105                            dlFileShortcut.getFileShortcutId(),
1106                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1107                            extraDataJSONObject.toString(), 0);
1108    
1109                    // Trash
1110    
1111                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
1112            }
1113    
1114            @Override
1115            public void restoreFolderFromTrash(long userId, Folder folder)
1116                    throws PortalException, SystemException {
1117    
1118                    // Folder
1119    
1120                    DLFolder dlFolder = (DLFolder)folder.getModel();
1121    
1122                    dlFolder.setName(TrashUtil.getOriginalTitle(dlFolder.getName()));
1123    
1124                    dlFolderPersistence.update(dlFolder);
1125    
1126                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
1127                            DLFolder.class.getName(), dlFolder.getFolderId());
1128    
1129                    dlFolderLocalService.updateStatus(
1130                            userId, folder.getFolderId(), trashEntry.getStatus(),
1131                            new HashMap<String, Serializable>(), new ServiceContext());
1132    
1133                    // File rank
1134    
1135                    dlFileRankLocalService.enableFileRanksByFolderId(folder.getFolderId());
1136    
1137                    // Folders, file entries, and file shortcuts
1138    
1139                    QueryDefinition queryDefinition = new QueryDefinition(
1140                            WorkflowConstants.STATUS_IN_TRASH);
1141    
1142                    List<Object> foldersAndFileEntriesAndFileShortcuts =
1143                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
1144                                    dlFolder.getGroupId(), dlFolder.getFolderId(), null, false,
1145                                    queryDefinition);
1146    
1147                    dlAppHelperLocalService.restoreDependentsFromTrash(
1148                            foldersAndFileEntriesAndFileShortcuts, trashEntry.getEntryId());
1149    
1150                    // Sync
1151    
1152                    registerDLSyncEventCallback(
1153                            DLSyncConstants.EVENT_RESTORE_FROM_TRASH, folder);
1154    
1155                    // Trash
1156    
1157                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
1158    
1159                    // Social
1160    
1161                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1162    
1163                    extraDataJSONObject.put("title", folder.getName());
1164    
1165                    socialActivityLocalService.addActivity(
1166                            userId, folder.getGroupId(), DLFolderConstants.getClassName(),
1167                            folder.getFolderId(),
1168                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1169                            extraDataJSONObject.toString(), 0);
1170            }
1171    
1172            @Override
1173            public AssetEntry updateAsset(
1174                            long userId, FileEntry fileEntry, FileVersion fileVersion,
1175                            long assetClassPk)
1176                    throws PortalException, SystemException {
1177    
1178                    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
1179                            DLFileEntryConstants.getClassName(), assetClassPk);
1180                    String[] assetTagNames = assetTagLocalService.getTagNames(
1181                            DLFileEntryConstants.getClassName(), assetClassPk);
1182    
1183                    AssetEntry assetEntry = assetEntryLocalService.getEntry(
1184                            DLFileEntryConstants.getClassName(), assetClassPk);
1185    
1186                    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
1187                            assetEntry.getEntryId());
1188    
1189                    long[] assetLinkIds = StringUtil.split(
1190                            ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
1191    
1192                    return updateAsset(
1193                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
1194                            assetLinkIds);
1195            }
1196    
1197            @Override
1198            public AssetEntry updateAsset(
1199                            long userId, FileEntry fileEntry, FileVersion fileVersion,
1200                            long[] assetCategoryIds, String[] assetTagNames,
1201                            long[] assetLinkEntryIds)
1202                    throws PortalException, SystemException {
1203    
1204                    AssetEntry assetEntry = null;
1205    
1206                    boolean visible = false;
1207    
1208                    boolean addDraftAssetEntry = false;
1209    
1210                    if (fileEntry instanceof LiferayFileEntry) {
1211                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
1212    
1213                            if (dlFileVersion.isApproved()) {
1214                                    visible = true;
1215                            }
1216                            else {
1217                                    String version = dlFileVersion.getVersion();
1218    
1219                                    if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
1220                                            addDraftAssetEntry = true;
1221                                    }
1222                            }
1223                    }
1224                    else {
1225                            visible = true;
1226                    }
1227    
1228                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
1229    
1230                    if (addDraftAssetEntry) {
1231                            if (assetCategoryIds == null) {
1232                                    assetCategoryIds = assetCategoryLocalService.getCategoryIds(
1233                                            DLFileEntryConstants.getClassName(),
1234                                            fileEntry.getFileEntryId());
1235                            }
1236    
1237                            if (assetTagNames == null) {
1238                                    assetTagNames = assetTagLocalService.getTagNames(
1239                                            DLFileEntryConstants.getClassName(),
1240                                            fileEntry.getFileEntryId());
1241                            }
1242    
1243                            if (assetLinkEntryIds == null) {
1244                                    AssetEntry previousAssetEntry = assetEntryLocalService.getEntry(
1245                                            DLFileEntryConstants.getClassName(),
1246                                            fileEntry.getFileEntryId());
1247    
1248                                    List<AssetLink> assetLinks =
1249                                            assetLinkLocalService.getDirectLinks(
1250                                                    previousAssetEntry.getEntryId(),
1251                                                    AssetLinkConstants.TYPE_RELATED);
1252    
1253                                    assetLinkEntryIds = StringUtil.split(
1254                                            ListUtil.toString(
1255                                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
1256                            }
1257    
1258                            assetEntry = assetEntryLocalService.updateEntry(
1259                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
1260                                    fileEntry.getModifiedDate(),
1261                                    DLFileEntryConstants.getClassName(),
1262                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
1263                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
1264                                    null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
1265                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
1266                                    false);
1267                    }
1268                    else {
1269                            assetEntry = assetEntryLocalService.updateEntry(
1270                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
1271                                    fileEntry.getModifiedDate(),
1272                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1273                                    fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
1274                                    assetTagNames, visible, null, null, null,
1275                                    fileEntry.getMimeType(), fileEntry.getTitle(),
1276                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
1277                                    false);
1278    
1279                            List<DLFileShortcut> dlFileShortcuts =
1280                                    dlFileShortcutPersistence.findByToFileEntryId(
1281                                            fileEntry.getFileEntryId());
1282    
1283                            for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
1284                                    assetEntryLocalService.updateEntry(
1285                                            userId, dlFileShortcut.getGroupId(),
1286                                            dlFileShortcut.getCreateDate(),
1287                                            dlFileShortcut.getModifiedDate(),
1288                                            DLFileShortcut.class.getName(),
1289                                            dlFileShortcut.getFileShortcutId(),
1290                                            dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
1291                                            assetTagNames, true, null, null, null,
1292                                            fileEntry.getMimeType(), fileEntry.getTitle(),
1293                                            fileEntry.getDescription(), null, null, null, 0, 0, null,
1294                                            false);
1295                            }
1296                    }
1297    
1298                    assetLinkLocalService.updateLinks(
1299                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
1300                            AssetLinkConstants.TYPE_RELATED);
1301    
1302                    return assetEntry;
1303            }
1304    
1305            @Override
1306            public AssetEntry updateAsset(
1307                            long userId, Folder folder, long[] assetCategoryIds,
1308                            String[] assetTagNames, long[] assetLinkEntryIds)
1309                    throws PortalException, SystemException {
1310    
1311                    AssetEntry assetEntry = null;
1312    
1313                    boolean visible = false;
1314    
1315                    if (folder instanceof LiferayFolder) {
1316                            DLFolder dlFolder = (DLFolder)folder.getModel();
1317    
1318                            if (dlFolder.isApproved() && !dlFolder.isHidden() &&
1319                                    !dlFolder.isInHiddenFolder()) {
1320    
1321                                    visible = true;
1322                            }
1323                    }
1324                    else {
1325                            visible = true;
1326                    }
1327    
1328                    assetEntry = assetEntryLocalService.updateEntry(
1329                            userId, folder.getGroupId(), folder.getCreateDate(),
1330                            folder.getModifiedDate(), DLFolderConstants.getClassName(),
1331                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
1332                            assetTagNames, visible, null, null, null, null, folder.getName(),
1333                            folder.getDescription(), null, null, null, 0, 0, null, false);
1334    
1335                    assetLinkLocalService.updateLinks(
1336                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
1337                            AssetLinkConstants.TYPE_RELATED);
1338    
1339                    return assetEntry;
1340            }
1341    
1342            @Override
1343            public void updateFileEntry(
1344                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
1345                            FileVersion destinationFileVersion, long assetClassPk)
1346                    throws PortalException, SystemException {
1347    
1348                    if (!DLAppHelperThreadLocal.isEnabled()) {
1349                            return;
1350                    }
1351    
1352                    boolean updateAsset = true;
1353    
1354                    if (fileEntry instanceof LiferayFileEntry &&
1355                            fileEntry.getVersion().equals(
1356                                    destinationFileVersion.getVersion())) {
1357    
1358                            updateAsset = false;
1359                    }
1360    
1361                    if (updateAsset) {
1362                            updateAsset(
1363                                    userId, fileEntry, destinationFileVersion, assetClassPk);
1364                    }
1365    
1366                    registerDLProcessorCallback(fileEntry, sourceFileVersion);
1367    
1368                    registerDLSyncEventCallback(DLSyncConstants.EVENT_UPDATE, fileEntry);
1369            }
1370    
1371            @Override
1372            public void updateFileEntry(
1373                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
1374                            FileVersion destinationFileVersion, ServiceContext serviceContext)
1375                    throws PortalException, SystemException {
1376    
1377                    if (!DLAppHelperThreadLocal.isEnabled()) {
1378                            return;
1379                    }
1380    
1381                    updateAsset(
1382                            userId, fileEntry, destinationFileVersion,
1383                            serviceContext.getAssetCategoryIds(),
1384                            serviceContext.getAssetTagNames(),
1385                            serviceContext.getAssetLinkEntryIds());
1386    
1387                    registerDLProcessorCallback(fileEntry, sourceFileVersion);
1388    
1389                    registerDLSyncEventCallback(DLSyncConstants.EVENT_UPDATE, fileEntry);
1390            }
1391    
1392            @Override
1393            public void updateFolder(
1394                            long userId, Folder folder, ServiceContext serviceContext)
1395                    throws PortalException, SystemException {
1396    
1397                    updateAsset(
1398                            userId, folder, serviceContext.getAssetCategoryIds(),
1399                            serviceContext.getAssetTagNames(),
1400                            serviceContext.getAssetLinkEntryIds());
1401    
1402                    registerDLSyncEventCallback(DLSyncConstants.EVENT_UPDATE, folder);
1403            }
1404    
1405            @Override
1406            public void updateStatus(
1407                            long userId, FileEntry fileEntry, FileVersion latestFileVersion,
1408                            int oldStatus, int newStatus,
1409                            Map<String, Serializable> workflowContext,
1410                            ServiceContext serviceContext)
1411                    throws PortalException, SystemException {
1412    
1413                    if (!DLAppHelperThreadLocal.isEnabled()) {
1414                            return;
1415                    }
1416    
1417                    if (newStatus == WorkflowConstants.STATUS_APPROVED) {
1418    
1419                            // Asset
1420    
1421                            String latestFileVersionVersion = latestFileVersion.getVersion();
1422    
1423                            if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
1424                                    if (!latestFileVersionVersion.equals(
1425                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
1426    
1427                                            AssetEntry draftAssetEntry =
1428                                                    assetEntryLocalService.fetchEntry(
1429                                                            DLFileEntryConstants.getClassName(),
1430                                                            latestFileVersion.getPrimaryKey());
1431    
1432                                            if (draftAssetEntry != null) {
1433                                                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
1434    
1435                                                    long[] assetCategoryIds =
1436                                                            draftAssetEntry.getCategoryIds();
1437                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
1438    
1439                                                    List<AssetLink> assetLinks =
1440                                                            assetLinkLocalService.getDirectLinks(
1441                                                                    draftAssetEntry.getEntryId(),
1442                                                                    AssetLinkConstants.TYPE_RELATED);
1443    
1444                                                    long[] assetLinkEntryIds = StringUtil.split(
1445                                                            ListUtil.toString(
1446                                                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
1447    
1448                                                    AssetEntry assetEntry =
1449                                                            assetEntryLocalService.updateEntry(
1450                                                                    userId, fileEntry.getGroupId(),
1451                                                                    fileEntry.getCreateDate(),
1452                                                                    fileEntry.getModifiedDate(),
1453                                                                    DLFileEntryConstants.getClassName(),
1454                                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
1455                                                                    fileEntryTypeId, assetCategoryIds,
1456                                                                    assetTagNames, true, null, null, null,
1457                                                                    draftAssetEntry.getMimeType(),
1458                                                                    fileEntry.getTitle(),
1459                                                                    fileEntry.getDescription(), null, null, null, 0,
1460                                                                    0, null, false);
1461    
1462                                                    assetLinkLocalService.updateLinks(
1463                                                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
1464                                                            AssetLinkConstants.TYPE_RELATED);
1465    
1466                                                    SystemEventHierarchyEntryThreadLocal.push(
1467                                                            DLFileEntryConstants.getClassName());
1468    
1469                                                    try {
1470                                                            assetEntryLocalService.deleteEntry(draftAssetEntry);
1471                                                    }
1472                                                    finally {
1473                                                            SystemEventHierarchyEntryThreadLocal.pop(
1474                                                                    DLFileEntryConstants.getClassName());
1475                                                    }
1476                                            }
1477                                    }
1478    
1479                                    AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
1480                                            DLFileEntryConstants.getClassName(),
1481                                            fileEntry.getFileEntryId());
1482    
1483                                    if (assetEntry != null) {
1484                                            assetEntryLocalService.updateVisible(
1485                                                    DLFileEntryConstants.getClassName(),
1486                                                    fileEntry.getFileEntryId(), true);
1487                                    }
1488                            }
1489    
1490                            // Sync
1491    
1492                            String event = (String)workflowContext.get("event");
1493    
1494                            if (Validator.isNotNull(event)) {
1495                                    registerDLSyncEventCallback(event, fileEntry);
1496                            }
1497    
1498                            if ((oldStatus != WorkflowConstants.STATUS_IN_TRASH) &&
1499                                    !fileEntry.isInTrash()) {
1500    
1501                                    // Social
1502    
1503                                    Date activityCreateDate = latestFileVersion.getModifiedDate();
1504                                    int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
1505    
1506                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
1507                                            activityCreateDate = latestFileVersion.getCreateDate();
1508                                            activityType = DLActivityKeys.ADD_FILE_ENTRY;
1509                                    }
1510    
1511                                    JSONObject extraDataJSONObject =
1512                                            JSONFactoryUtil.createJSONObject();
1513    
1514                                    extraDataJSONObject.put("title", fileEntry.getTitle());
1515    
1516                                    socialActivityLocalService.addUniqueActivity(
1517                                            latestFileVersion.getStatusByUserId(),
1518                                            fileEntry.getGroupId(), activityCreateDate,
1519                                            DLFileEntryConstants.getClassName(),
1520                                            fileEntry.getFileEntryId(), activityType,
1521                                            extraDataJSONObject.toString(), 0);
1522    
1523                                    // Subscriptions
1524    
1525                                    notifySubscribers(latestFileVersion, serviceContext);
1526                            }
1527                    }
1528                    else {
1529    
1530                            // Asset
1531    
1532                            boolean visible = false;
1533    
1534                            if (newStatus != WorkflowConstants.STATUS_IN_TRASH) {
1535                                    List<DLFileVersion> approvedFileVersions =
1536                                            dlFileVersionPersistence.findByF_S(
1537                                                    fileEntry.getFileEntryId(),
1538                                                    WorkflowConstants.STATUS_APPROVED);
1539    
1540                                    if (!approvedFileVersions.isEmpty()) {
1541                                            visible = true;
1542                                    }
1543                            }
1544    
1545                            AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
1546                                    DLFileEntryConstants.getClassName(),
1547                                    fileEntry.getFileEntryId());
1548    
1549                            if (assetEntry != null) {
1550                                    assetEntryLocalService.updateVisible(
1551                                            DLFileEntryConstants.getClassName(),
1552                                            fileEntry.getFileEntryId(), visible);
1553                            }
1554                    }
1555            }
1556    
1557            protected FileEntry doMoveFileEntryFromTrash(
1558                            long userId, FileEntry fileEntry, long newFolderId,
1559                            ServiceContext serviceContext)
1560                    throws PortalException, SystemException {
1561    
1562                    // File entry
1563    
1564                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1565    
1566                    if (dlFileEntry.isInTrashExplicitly()) {
1567                            restoreFileEntryFromTrash(userId, fileEntry);
1568    
1569                            fileEntry = dlAppLocalService.moveFileEntry(
1570                                    userId, fileEntry.getFileEntryId(), newFolderId,
1571                                    serviceContext);
1572    
1573                            if (DLAppHelperThreadLocal.isEnabled()) {
1574                                    dlFileRankLocalService.enableFileRanks(
1575                                            fileEntry.getFileEntryId());
1576                            }
1577    
1578                            // Indexer
1579    
1580                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1581                                    DLFileEntry.class);
1582    
1583                            indexer.reindex((DLFileEntry)fileEntry.getModel());
1584    
1585                            return fileEntry;
1586                    }
1587    
1588                    TrashEntry trashEntry = dlFileEntry.getTrashEntry();
1589    
1590                    List<DLFileVersion> dlFileVersions =
1591                            dlFileVersionLocalService.getFileVersions(
1592                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_IN_TRASH);
1593    
1594                    dlFileVersions = ListUtil.sort(
1595                            dlFileVersions, new FileVersionVersionComparator());
1596    
1597                    FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
1598    
1599                    TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1600                            trashEntry.getEntryId(), DLFileVersion.class.getName(),
1601                            fileVersion.getFileVersionId());
1602    
1603                    int oldStatus = WorkflowConstants.STATUS_APPROVED;
1604    
1605                    if (trashVersion != null) {
1606                            oldStatus = trashVersion.getStatus();
1607                    }
1608    
1609                    dlFileEntryLocalService.updateStatus(
1610                            userId, fileVersion.getFileVersionId(), oldStatus,
1611                            new HashMap<String, Serializable>(), serviceContext);
1612    
1613                    // File versions
1614    
1615                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1616    
1617                            // File version
1618    
1619                            trashVersion = trashVersionLocalService.fetchVersion(
1620                                    trashEntry.getEntryId(), DLFileVersion.class.getName(),
1621                                    dlFileVersion.getFileVersionId());
1622    
1623                            oldStatus = WorkflowConstants.STATUS_APPROVED;
1624    
1625                            if (trashVersion != null) {
1626                                    oldStatus = trashVersion.getStatus();
1627                            }
1628    
1629                            dlFileVersion.setStatus(oldStatus);
1630    
1631                            dlFileVersionPersistence.update(dlFileVersion);
1632    
1633                            // Trash
1634    
1635                            if (trashVersion != null) {
1636                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
1637                            }
1638                    }
1639    
1640                    if (DLAppHelperThreadLocal.isEnabled()) {
1641    
1642                            // File rank
1643    
1644                            dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
1645    
1646                            // File shortcut
1647    
1648                            dlFileShortcutLocalService.enableFileShortcuts(
1649                                    fileEntry.getFileEntryId());
1650                    }
1651    
1652                    // App helper
1653    
1654                    fileEntry = dlAppService.moveFileEntry(
1655                            fileEntry.getFileEntryId(), newFolderId, serviceContext);
1656    
1657                    // Sync
1658    
1659                    registerDLSyncEventCallback(
1660                            DLSyncConstants.EVENT_RESTORE_FROM_TRASH, fileEntry);
1661    
1662                    // Social
1663    
1664                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1665    
1666                    extraDataJSONObject.put("title", fileEntry.getTitle());
1667    
1668                    socialActivityLocalService.addActivity(
1669                            userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
1670                            fileEntry.getFileEntryId(),
1671                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1672                            extraDataJSONObject.toString(), 0);
1673    
1674                    // Indexer
1675    
1676                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1677                            DLFileEntry.class);
1678    
1679                    indexer.reindex((DLFileEntry)fileEntry.getModel());
1680    
1681                    return fileEntry;
1682            }
1683    
1684            protected FileEntry doMoveFileEntryToTrash(long userId, FileEntry fileEntry)
1685                    throws PortalException, SystemException {
1686    
1687                    // File versions
1688    
1689                    List<DLFileVersion> dlFileVersions =
1690                            dlFileVersionLocalService.getFileVersions(
1691                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1692    
1693                    dlFileVersions = ListUtil.sort(
1694                            dlFileVersions, new FileVersionVersionComparator());
1695    
1696                    FileVersion fileVersion = fileEntry.getFileVersion();
1697    
1698                    int oldStatus = fileVersion.getStatus();
1699    
1700                    dlFileEntryLocalService.updateStatus(
1701                            userId, fileVersion.getFileVersionId(),
1702                            WorkflowConstants.STATUS_IN_TRASH,
1703                            new HashMap<String, Serializable>(), new ServiceContext());
1704    
1705                    if (DLAppHelperThreadLocal.isEnabled()) {
1706    
1707                            // File shortcut
1708    
1709                            dlFileShortcutLocalService.disableFileShortcuts(
1710                                    fileEntry.getFileEntryId());
1711    
1712                            // File rank
1713    
1714                            dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());
1715    
1716                            // Sync
1717    
1718                            registerDLSyncEventCallback(
1719                                    DLSyncConstants.EVENT_MOVE_TO_TRASH, fileEntry);
1720                    }
1721    
1722                    // Trash
1723    
1724                    int oldDLFileVersionStatus = WorkflowConstants.STATUS_ANY;
1725    
1726                    List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1727                            new ArrayList<ObjectValuePair<Long, Integer>>();
1728    
1729                    DLFileVersion oldDLFileVersion = (DLFileVersion)fileVersion.getModel();
1730    
1731                    oldDLFileVersionStatus = oldDLFileVersion.getStatus();
1732    
1733                    if ((dlFileVersions != null) && !dlFileVersions.isEmpty()) {
1734                            dlFileVersionStatusOVPs = getDlFileVersionStatuses(dlFileVersions);
1735                    }
1736    
1737                    for (DLFileVersion curDLFileVersion : dlFileVersions) {
1738                            curDLFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1739    
1740                            dlFileVersionPersistence.update(curDLFileVersion);
1741                    }
1742    
1743                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1744    
1745                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1746    
1747                    typeSettingsProperties.put("title", dlFileEntry.getTitle());
1748    
1749                    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1750                            userId, dlFileEntry.getGroupId(),
1751                            DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId(),
1752                            dlFileEntry.getUuid(), dlFileEntry.getClassName(),
1753                            oldDLFileVersionStatus, dlFileVersionStatusOVPs,
1754                            typeSettingsProperties);
1755    
1756                    String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
1757    
1758                    dlFileEntry.setTitle(trashTitle);
1759    
1760                    dlFileEntryPersistence.update(dlFileEntry);
1761    
1762                    // Indexer
1763    
1764                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1765                            DLFileEntry.class);
1766    
1767                    indexer.reindex(dlFileEntry);
1768    
1769                    if (!DLAppHelperThreadLocal.isEnabled()) {
1770                            return fileEntry;
1771                    }
1772    
1773                    // Social
1774    
1775                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1776    
1777                    extraDataJSONObject.put(
1778                            "title", TrashUtil.getOriginalTitle(fileEntry.getTitle()));
1779    
1780                    socialActivityLocalService.addActivity(
1781                            userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
1782                            fileEntry.getFileEntryId(),
1783                            SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1784                            extraDataJSONObject.toString(), 0);
1785    
1786                    // Workflow
1787    
1788                    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1789                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
1790                                    fileVersion.getCompanyId(), fileVersion.getGroupId(),
1791                                    DLFileEntryConstants.getClassName(),
1792                                    fileVersion.getFileVersionId());
1793                    }
1794    
1795                    return fileEntry;
1796            }
1797    
1798            protected Folder doMoveFolderFromTrash(
1799                            long userId, Folder folder, long parentFolderId,
1800                            ServiceContext serviceContext)
1801                    throws PortalException, SystemException {
1802    
1803                    DLFolder dlFolder = (DLFolder)folder.getModel();
1804    
1805                    if (dlFolder.isInTrashExplicitly()) {
1806                            restoreFolderFromTrash(userId, folder);
1807                    }
1808                    else {
1809    
1810                            // Folder
1811    
1812                            TrashEntry trashEntry = dlFolder.getTrashEntry();
1813    
1814                            TrashVersion trashVersion =
1815                                    trashVersionLocalService.fetchVersion(
1816                                            trashEntry.getEntryId(), DLFolder.class.getName(),
1817                                            dlFolder.getFolderId());
1818    
1819                            int status = WorkflowConstants.STATUS_APPROVED;
1820    
1821                            if (trashVersion != null) {
1822                                    status = trashVersion.getStatus();
1823                            }
1824    
1825                            dlFolderLocalService.updateStatus(
1826                                    userId, folder.getFolderId(), status,
1827                                    new HashMap<String, Serializable>(), new ServiceContext());
1828    
1829                            // File rank
1830    
1831                            dlFileRankLocalService.enableFileRanksByFolderId(
1832                                    folder.getFolderId());
1833    
1834                            // Trash
1835    
1836                            if (trashVersion != null) {
1837                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
1838                            }
1839    
1840                            // Folders, file entries, and file shortcuts
1841    
1842                            QueryDefinition queryDefinition = new QueryDefinition(
1843                                    WorkflowConstants.STATUS_IN_TRASH);
1844    
1845                            List<Object> foldersAndFileEntriesAndFileShortcuts =
1846                                    dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
1847                                            dlFolder.getGroupId(), dlFolder.getFolderId(), null, false,
1848                                            queryDefinition);
1849    
1850                            dlAppHelperLocalService.restoreDependentsFromTrash(
1851                                    foldersAndFileEntriesAndFileShortcuts, trashEntry.getEntryId());
1852    
1853                            // Sync
1854    
1855                            registerDLSyncEventCallback(
1856                                    DLSyncConstants.EVENT_RESTORE_FROM_TRASH, folder);
1857    
1858                            // Social
1859    
1860                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1861    
1862                            extraDataJSONObject.put("title", folder.getName());
1863    
1864                            socialActivityLocalService.addActivity(
1865                                    userId, folder.getGroupId(), DLFolderConstants.class.getName(),
1866                                    folder.getFolderId(),
1867                                    SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1868                                    extraDataJSONObject.toString(), 0);
1869                    }
1870    
1871                    return dlAppLocalService.moveFolder(
1872                            userId, folder.getFolderId(), parentFolderId, serviceContext);
1873            }
1874    
1875            protected Folder doMoveFolderToTrash(long userId, Folder folder)
1876                    throws PortalException, SystemException {
1877    
1878                    // Folder
1879    
1880                    DLFolder dlFolder = dlFolderLocalService.updateStatus(
1881                            userId, folder.getFolderId(), WorkflowConstants.STATUS_IN_TRASH,
1882                            new HashMap<String, Serializable>(), new ServiceContext());
1883    
1884                    // File rank
1885    
1886                    dlFileRankLocalService.disableFileRanksByFolderId(folder.getFolderId());
1887    
1888                    // Trash
1889    
1890                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1891    
1892                    typeSettingsProperties.put("title", dlFolder.getName());
1893    
1894                    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1895                            userId, dlFolder.getGroupId(), DLFolderConstants.getClassName(),
1896                            dlFolder.getFolderId(), dlFolder.getUuid(), null,
1897                            WorkflowConstants.STATUS_APPROVED, null, typeSettingsProperties);
1898    
1899                    dlFolder.setName(TrashUtil.getTrashTitle(trashEntry.getEntryId()));
1900    
1901                    dlFolderPersistence.update(dlFolder);
1902    
1903                    // Folders, file entries, and file shortcuts
1904    
1905                    QueryDefinition queryDefinition = new QueryDefinition(
1906                            WorkflowConstants.STATUS_ANY);
1907    
1908                    List<Object> foldersAndFileEntriesAndFileShortcuts =
1909                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
1910                                    dlFolder.getGroupId(), dlFolder.getFolderId(), null, false,
1911                                    queryDefinition);
1912    
1913                    dlAppHelperLocalService.moveDependentsToTrash(
1914                            foldersAndFileEntriesAndFileShortcuts, trashEntry.getEntryId());
1915    
1916                    // Sync
1917    
1918                    registerDLSyncEventCallback(
1919                            DLSyncConstants.EVENT_MOVE_TO_TRASH, folder);
1920    
1921                    // Social
1922    
1923                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1924    
1925                    extraDataJSONObject.put("title", folder.getName());
1926    
1927                    socialActivityLocalService.addActivity(
1928                            userId, folder.getGroupId(), DLFolderConstants.getClassName(),
1929                            folder.getFolderId(), SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1930                            extraDataJSONObject.toString(), 0);
1931    
1932                    // Indexer
1933    
1934                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1935                            DLFolder.class);
1936    
1937                    indexer.reindex(dlFolder);
1938    
1939                    return new LiferayFolder(dlFolder);
1940            }
1941    
1942            protected List<ObjectValuePair<Long, Integer>> getDlFileVersionStatuses(
1943                    List<DLFileVersion> dlFileVersions) {
1944    
1945                    List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1946                            new ArrayList<ObjectValuePair<Long, Integer>>(
1947                                    dlFileVersions.size());
1948    
1949                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1950                            int status = dlFileVersion.getStatus();
1951    
1952                            if (status == WorkflowConstants.STATUS_PENDING) {
1953                                    status = WorkflowConstants.STATUS_DRAFT;
1954                            }
1955    
1956                            ObjectValuePair<Long, Integer> dlFileVersionStatusOVP =
1957                                    new ObjectValuePair<Long, Integer>(
1958                                            dlFileVersion.getFileVersionId(), status);
1959    
1960                            dlFileVersionStatusOVPs.add(dlFileVersionStatusOVP);
1961                    }
1962    
1963                    return dlFileVersionStatusOVPs;
1964            }
1965    
1966            protected long getFileEntryTypeId(FileEntry fileEntry) {
1967                    if (fileEntry instanceof LiferayFileEntry) {
1968                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1969    
1970                            return dlFileEntry.getFileEntryTypeId();
1971                    }
1972                    else {
1973                            return 0;
1974                    }
1975            }
1976    
1977            protected boolean isStagingGroup(long groupId) {
1978                    try {
1979                            Group group = groupLocalService.getGroup(groupId);
1980    
1981                            return group.isStagingGroup();
1982                    }
1983                    catch (Exception e) {
1984                            return false;
1985                    }
1986            }
1987    
1988            protected void notify(final SubscriptionSender subscriptionSender) {
1989                    TransactionCommitCallbackRegistryUtil.registerCallback(
1990                            new Callable<Void>() {
1991    
1992                                    @Override
1993                                    public Void call() throws Exception {
1994                                            subscriptionSender.flushNotificationsAsync();
1995    
1996                                            return null;
1997                                    }
1998    
1999                            }
2000                    );
2001            }
2002    
2003            protected void notifySubscribers(
2004                            FileVersion fileVersion, ServiceContext serviceContext)
2005                    throws PortalException, SystemException {
2006    
2007                    if (!fileVersion.isApproved()) {
2008                            return;
2009                    }
2010    
2011                    PortletPreferences preferences =
2012                            ServiceContextUtil.getPortletPreferences(serviceContext);
2013    
2014                    if (preferences == null) {
2015                            long ownerId = fileVersion.getGroupId();
2016                            int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
2017                            long plid = PortletKeys.PREFS_PLID_SHARED;
2018                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
2019                            String defaultPreferences = null;
2020    
2021                            preferences = portletPreferencesLocalService.getPreferences(
2022                                    fileVersion.getCompanyId(), ownerId, ownerType, plid, portletId,
2023                                    defaultPreferences);
2024                    }
2025    
2026                    if (serviceContext.isCommandAdd() &&
2027                            DLUtil.getEmailFileEntryAddedEnabled(preferences)) {
2028                    }
2029                    else if (serviceContext.isCommandUpdate() &&
2030                                     DLUtil.getEmailFileEntryUpdatedEnabled(preferences)) {
2031                    }
2032                    else {
2033                            return;
2034                    }
2035    
2036                    String fromName = DLUtil.getEmailFromName(
2037                            preferences, fileVersion.getCompanyId());
2038                    String fromAddress = DLUtil.getEmailFromAddress(
2039                            preferences, fileVersion.getCompanyId());
2040    
2041                    Map<Locale, String> localizedSubjectMap = null;
2042                    Map<Locale, String> localizedBodyMap = null;
2043    
2044                    if (serviceContext.isCommandUpdate()) {
2045                            localizedSubjectMap = DLUtil.getEmailFileEntryUpdatedSubjectMap(
2046                                    preferences);
2047                            localizedBodyMap = DLUtil.getEmailFileEntryUpdatedBodyMap(
2048                                    preferences);
2049                    }
2050                    else {
2051                            localizedSubjectMap = DLUtil.getEmailFileEntryAddedSubjectMap(
2052                                    preferences);
2053                            localizedBodyMap = DLUtil.getEmailFileEntryAddedBodyMap(
2054                                    preferences);
2055                    }
2056    
2057                    FileEntry fileEntry = fileVersion.getFileEntry();
2058    
2059                    Folder folder = null;
2060    
2061                    long folderId = fileEntry.getFolderId();
2062    
2063                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
2064                            folder = dlAppLocalService.getFolder(folderId);
2065                    }
2066    
2067                    String folderName = LanguageUtil.get(
2068                            serviceContext.getLocale(), "home");
2069    
2070                    if (folder != null) {
2071                            folderName = folder.getName();
2072                    }
2073    
2074                    SubscriptionSender subscriptionSender = new SubscriptionSender();
2075    
2076                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
2077    
2078                    DLFileEntryType dlFileEntryType =
2079                            dlFileEntryTypeLocalService.getDLFileEntryType(
2080                                    dlFileEntry.getFileEntryTypeId());
2081    
2082                    subscriptionSender.setCompanyId(fileVersion.getCompanyId());
2083                    subscriptionSender.setContextAttributes(
2084                            "[$DOCUMENT_STATUS_BY_USER_NAME$]",
2085                            fileVersion.getStatusByUserName(), "[$DOCUMENT_TITLE$]",
2086                            fileVersion.getTitle(), "[$DOCUMENT_TYPE$]",
2087                            dlFileEntryType.getName(serviceContext.getLocale()),
2088                            "[$FOLDER_NAME$]", folderName);
2089                    subscriptionSender.setContextUserPrefix("DOCUMENT");
2090                    subscriptionSender.setFrom(fromAddress, fromName);
2091                    subscriptionSender.setHtmlFormat(true);
2092                    subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
2093                    subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
2094                    subscriptionSender.setMailId(
2095                            "file_entry", fileVersion.getFileEntryId());
2096                    subscriptionSender.setPortletId(PortletKeys.DOCUMENT_LIBRARY);
2097                    subscriptionSender.setReplyToAddress(fromAddress);
2098                    subscriptionSender.setScopeGroupId(fileVersion.getGroupId());
2099                    subscriptionSender.setServiceContext(serviceContext);
2100                    subscriptionSender.setUserId(fileVersion.getUserId());
2101    
2102                    subscriptionSender.addPersistedSubscribers(
2103                            Folder.class.getName(), fileVersion.getGroupId());
2104    
2105                    List<Long> folderIds = new ArrayList<Long>();
2106    
2107                    if (folder != null) {
2108                            folderIds.add(folder.getFolderId());
2109    
2110                            folderIds.addAll(folder.getAncestorFolderIds());
2111                    }
2112    
2113                    for (long curFolderId : folderIds) {
2114                            subscriptionSender.addPersistedSubscribers(
2115                                    Folder.class.getName(), curFolderId);
2116                    }
2117    
2118                    if (dlFileEntryType.getFileEntryTypeId() ==
2119                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
2120    
2121                            subscriptionSender.addPersistedSubscribers(
2122                                    DLFileEntryType.class.getName(), fileVersion.getGroupId());
2123                    }
2124                    else {
2125                            subscriptionSender.addPersistedSubscribers(
2126                                    DLFileEntryType.class.getName(),
2127                                    dlFileEntryType.getFileEntryTypeId());
2128                    }
2129    
2130                    subscriptionSender.addPersistedSubscribers(
2131                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
2132    
2133                    notify(subscriptionSender);
2134            }
2135    
2136            protected void registerDLProcessorCallback(
2137                    final FileEntry fileEntry, final FileVersion fileVersion) {
2138    
2139                    TransactionCommitCallbackRegistryUtil.registerCallback(
2140                            new Callable<Void>() {
2141    
2142                                    @Override
2143                                    public Void call() throws Exception {
2144                                            DLProcessorRegistryUtil.trigger(
2145                                                    fileEntry, fileVersion, true);
2146    
2147                                            return null;
2148                                    }
2149    
2150                            });
2151            }
2152    
2153            protected void registerDLSyncEventCallback(
2154                            final String event, final String type, final long typePK)
2155                    throws SystemException {
2156    
2157                    DLSyncEvent dlSyncEvent = dlSyncEventLocalService.addDLSyncEvent(
2158                            event, type, typePK);
2159    
2160                    final long modifiedTime = dlSyncEvent.getModifiedTime();
2161    
2162                    TransactionCommitCallbackRegistryUtil.registerCallback(
2163                            new Callable<Void>() {
2164    
2165                                    @Override
2166                                    public Void call() throws Exception {
2167                                            Message message = new Message();
2168    
2169                                            Map<String, Object> values = new HashMap<String, Object>(4);
2170    
2171                                            values.put("event", event);
2172                                            values.put("modifiedTime", modifiedTime);
2173                                            values.put("type", type);
2174                                            values.put("typePK", typePK);
2175    
2176                                            message.setValues(values);
2177    
2178                                            MessageBusUtil.sendMessage(
2179                                                    DestinationNames.DOCUMENT_LIBRARY_SYNC_EVENT_PROCESSOR,
2180                                                    message);
2181    
2182                                            return null;
2183                                    }
2184    
2185                            }
2186                    );
2187            }
2188    
2189    }