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