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