001    /**
002     * Copyright (c) 2000-2012 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.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.FileVersion;
025    import com.liferay.portal.kernel.repository.model.Folder;
026    import com.liferay.portal.kernel.search.Indexer;
027    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028    import com.liferay.portal.kernel.transaction.TransactionCommitCallbackRegistryUtil;
029    import com.liferay.portal.kernel.util.ListUtil;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
035    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
036    import com.liferay.portal.model.Group;
037    import com.liferay.portal.model.Lock;
038    import com.liferay.portal.model.User;
039    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
040    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
041    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
042    import com.liferay.portal.service.ServiceContext;
043    import com.liferay.portal.service.ServiceContextUtil;
044    import com.liferay.portal.util.PortletKeys;
045    import com.liferay.portal.util.PropsValues;
046    import com.liferay.portal.util.SubscriptionSender;
047    import com.liferay.portlet.asset.NoSuchEntryException;
048    import com.liferay.portlet.asset.model.AssetEntry;
049    import com.liferay.portlet.asset.model.AssetLink;
050    import com.liferay.portlet.asset.model.AssetLinkConstants;
051    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
052    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
053    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
054    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
055    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
056    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
057    import com.liferay.portlet.documentlibrary.model.DLFolder;
058    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
059    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
060    import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
061    import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
062    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
063    import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
064    import com.liferay.portlet.documentlibrary.util.DLUtil;
065    import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
066    import com.liferay.portlet.social.model.SocialActivityConstants;
067    import com.liferay.portlet.trash.model.TrashEntry;
068    import com.liferay.portlet.trash.model.TrashVersion;
069    import com.liferay.portlet.trash.util.TrashUtil;
070    
071    import java.io.Serializable;
072    
073    import java.util.ArrayList;
074    import java.util.Collections;
075    import java.util.Date;
076    import java.util.HashMap;
077    import java.util.List;
078    import java.util.Locale;
079    import java.util.Map;
080    import java.util.concurrent.Callable;
081    
082    import javax.portlet.PortletPreferences;
083    
084    /**
085     * The implementation the document library application helper local service.
086     *
087     * @author Alexander Chow
088     */
089    public class DLAppHelperLocalServiceImpl
090            extends DLAppHelperLocalServiceBaseImpl {
091    
092            public void addFileEntry(
093                            long userId, FileEntry fileEntry, FileVersion fileVersion,
094                            ServiceContext serviceContext)
095                    throws PortalException, SystemException {
096    
097                    if (DLAppHelperThreadLocal.isEnabled()) {
098                            updateAsset(
099                                    userId, fileEntry, fileVersion,
100                                    serviceContext.getAssetCategoryIds(),
101                                    serviceContext.getAssetTagNames(),
102                                    serviceContext.getAssetLinkEntryIds());
103    
104                            if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
105                                    mbMessageLocalService.addDiscussionMessage(
106                                            fileEntry.getUserId(), fileEntry.getUserName(),
107                                            fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
108                                            fileEntry.getFileEntryId(),
109                                            WorkflowConstants.ACTION_PUBLISH);
110                            }
111                    }
112    
113                    boolean previousEnabled = WorkflowThreadLocal.isEnabled();
114    
115                    if (!DLAppHelperThreadLocal.isEnabled()) {
116                            WorkflowThreadLocal.setEnabled(false);
117                    }
118    
119                    try {
120                            if (fileVersion instanceof LiferayFileVersion) {
121                                    DLFileVersion dlFileVersion =
122                                            (DLFileVersion)fileVersion.getModel();
123    
124                                    Map<String, Serializable> workflowContext =
125                                            new HashMap<String, Serializable>();
126    
127                                    workflowContext.put("event", DLSyncConstants.EVENT_ADD);
128    
129                                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
130                                            dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(),
131                                            userId, DLFileEntryConstants.getClassName(),
132                                            dlFileVersion.getFileVersionId(), dlFileVersion,
133                                            serviceContext, workflowContext);
134                            }
135                    }
136                    finally {
137                            if (!DLAppHelperThreadLocal.isEnabled()) {
138                                    WorkflowThreadLocal.setEnabled(previousEnabled);
139                            }
140                    }
141    
142                    if (DLAppHelperThreadLocal.isEnabled()) {
143                            registerDLProcessorCallback(fileEntry, null);
144                    }
145            }
146    
147            public void addFolder(
148                            long userId, Folder folder, ServiceContext serviceContext)
149                    throws PortalException, SystemException {
150    
151                    updateAsset(
152                            userId, folder, serviceContext.getAssetCategoryIds(),
153                            serviceContext.getAssetTagNames(),
154                            serviceContext.getAssetLinkEntryIds());
155    
156                    if (!isStagingGroup(folder.getGroupId())) {
157                            dlSyncLocalService.addSync(
158                                    folder.getFolderId(), folder.getUuid(), folder.getCompanyId(),
159                                    folder.getRepositoryId(), folder.getParentFolderId(),
160                                    folder.getName(), folder.getDescription(),
161                                    DLSyncConstants.TYPE_FOLDER, "-1");
162                    }
163            }
164    
165            public void cancelCheckOut(
166                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
167                            FileVersion destinationFileVersion, FileVersion draftFileVersion,
168                            ServiceContext serviceContext)
169                    throws PortalException, SystemException {
170    
171                    updateFileEntry(
172                            userId, fileEntry, sourceFileVersion, destinationFileVersion,
173                            serviceContext);
174    
175                    if (draftFileVersion == null) {
176                            return;
177                    }
178    
179                    AssetEntry draftAssetEntry = null;
180    
181                    try {
182                            draftAssetEntry = assetEntryLocalService.getEntry(
183                                    DLFileEntryConstants.getClassName(),
184                                    draftFileVersion.getPrimaryKey());
185    
186                            assetEntryLocalService.deleteEntry(draftAssetEntry.getEntryId());
187                    }
188                    catch (NoSuchEntryException nsee) {
189                    }
190            }
191    
192            public void checkAssetEntry(
193                            long userId, FileEntry fileEntry, FileVersion fileVersion)
194                    throws PortalException, SystemException {
195    
196                    AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
197                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
198    
199                    long[] assetCategoryIds = new long[0];
200                    String[] assetTagNames = new String[0];
201    
202                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
203    
204                    if (fileEntryAssetEntry == null) {
205                            fileEntryAssetEntry = assetEntryLocalService.updateEntry(
206                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
207                                    fileEntry.getModifiedDate(),
208                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
209                                    fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
210                                    assetTagNames, false, null, null, null, fileEntry.getMimeType(),
211                                    fileEntry.getTitle(), fileEntry.getDescription(), null, null,
212                                    null, 0, 0, null, false);
213                    }
214    
215                    AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
216                            DLFileEntryConstants.getClassName(),
217                            fileVersion.getFileVersionId());
218    
219                    if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
220                            !fileVersion.getVersion().equals(
221                                    DLFileEntryConstants.VERSION_DEFAULT)) {
222    
223                            assetCategoryIds = assetCategoryLocalService.getCategoryIds(
224                                    DLFileEntryConstants.getClassName(),
225                                    fileEntry.getFileEntryId());
226                            assetTagNames = assetTagLocalService.getTagNames(
227                                    DLFileEntryConstants.getClassName(),
228                                    fileEntry.getFileEntryId());
229    
230                            fileVersionAssetEntry = assetEntryLocalService.updateEntry(
231                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
232                                    fileEntry.getModifiedDate(),
233                                    DLFileEntryConstants.getClassName(),
234                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
235                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
236                                    null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
237                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
238                                    false);
239    
240                            List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
241                                    fileEntryAssetEntry.getEntryId());
242    
243                            long[] assetLinkIds = StringUtil.split(
244                                    ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR),
245                                    0L);
246    
247                            assetLinkLocalService.updateLinks(
248                                    userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
249                                    AssetLinkConstants.TYPE_RELATED);
250                    }
251            }
252    
253            public void deleteFileEntry(FileEntry fileEntry)
254                    throws PortalException, SystemException {
255    
256                    if (DLAppHelperThreadLocal.isEnabled()) {
257    
258                            // Subscriptions
259    
260                            subscriptionLocalService.deleteSubscriptions(
261                                    fileEntry.getCompanyId(), DLFileEntryConstants.getClassName(),
262                                    fileEntry.getFileEntryId());
263    
264                            // File previews
265    
266                            DLProcessorRegistryUtil.cleanUp(fileEntry);
267    
268                            // File ranks
269    
270                            dlFileRankLocalService.deleteFileRanksByFileEntryId(
271                                    fileEntry.getFileEntryId());
272    
273                            // File shortcuts
274    
275                            dlFileShortcutLocalService.deleteFileShortcuts(
276                                    fileEntry.getFileEntryId());
277    
278                            // Sync
279    
280                            if (!isStagingGroup(fileEntry.getGroupId())) {
281                                    dlSyncLocalService.updateSync(
282                                            fileEntry.getFileEntryId(), fileEntry.getFolderId(),
283                                            fileEntry.getTitle(), fileEntry.getDescription(),
284                                            DLSyncConstants.EVENT_DELETE, fileEntry.getVersion());
285                            }
286    
287                            // Asset
288    
289                            assetEntryLocalService.deleteEntry(
290                                    DLFileEntryConstants.getClassName(),
291                                    fileEntry.getFileEntryId());
292    
293                            // Message boards
294    
295                            mbMessageLocalService.deleteDiscussionMessages(
296                                    DLFileEntryConstants.getClassName(),
297                                    fileEntry.getFileEntryId());
298    
299                            // Ratings
300    
301                            ratingsStatsLocalService.deleteStats(
302                                    DLFileEntryConstants.getClassName(),
303                                    fileEntry.getFileEntryId());
304                    }
305    
306                    // Trash
307    
308                    if (fileEntry.getModel() instanceof DLFileEntry) {
309                            trashEntryLocalService.deleteEntry(
310                                    DLFileEntryConstants.getClassName(),
311                                    fileEntry.getFileEntryId());
312                    }
313            }
314    
315            public void deleteFolder(Folder folder)
316                    throws PortalException, SystemException {
317    
318                    // Sync
319    
320                    if (!isStagingGroup(folder.getGroupId())) {
321                            dlSyncLocalService.updateSync(
322                                    folder.getFolderId(), folder.getParentFolderId(),
323                                    folder.getName(), folder.getDescription(),
324                                    DLSyncConstants.EVENT_DELETE, "-1");
325                    }
326    
327                    // Asset
328    
329                    assetEntryLocalService.deleteEntry(
330                            DLFolderConstants.getClassName(), folder.getFolderId());
331    
332                    // Trash
333    
334                    if (folder.getModel() instanceof DLFolder) {
335                            trashEntryLocalService.deleteEntry(
336                                    DLFolderConstants.getClassName(), folder.getFolderId());
337                    }
338            }
339    
340            public void getFileAsStream(
341                            long userId, FileEntry fileEntry, boolean incrementCounter)
342                    throws PortalException, SystemException {
343    
344                    if (!incrementCounter) {
345                            return;
346                    }
347    
348                    // File rank
349    
350                    if (userId > 0) {
351                            dlFileRankLocalService.updateFileRank(
352                                    fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
353                                    fileEntry.getFileEntryId(), new ServiceContext());
354                    }
355    
356                    // File read count
357    
358                    assetEntryLocalService.incrementViewCounter(
359                            userId, DLFileEntryConstants.getClassName(),
360                            fileEntry.getFileEntryId(), 1);
361    
362                    List<DLFileShortcut> fileShortcuts =
363                            dlFileShortcutPersistence.findByToFileEntryId(
364                                    fileEntry.getFileEntryId());
365    
366                    for (DLFileShortcut fileShortcut : fileShortcuts) {
367                            assetEntryLocalService.incrementViewCounter(
368                                    userId, DLFileShortcut.class.getName(),
369                                    fileShortcut.getFileShortcutId(), 1);
370                    }
371            }
372    
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 {@link #getFileShortcuts(long, long, boolean, int)}
383             */
384            public List<DLFileShortcut> getFileShortcuts(
385                            long groupId, long folderId, int status)
386                    throws SystemException {
387    
388                    return getFileShortcuts(groupId, folderId, true, status);
389            }
390    
391            public int getFileShortcutsCount(
392                            long groupId, long folderId, boolean active, int status)
393                    throws SystemException {
394    
395                    return dlFileShortcutPersistence.countByG_F_A_S(
396                            groupId, folderId, active, status);
397            }
398    
399            /**
400             * @deprecated {@link #getFileShortcutsCount(long, long, boolean, int)}
401             */
402            public int getFileShortcutsCount(long groupId, long folderId, int status)
403                    throws SystemException {
404    
405                    return getFileShortcutsCount(groupId, folderId, true, status);
406            }
407    
408            public List<FileEntry> getNoAssetFileEntries() {
409                    return null;
410            }
411    
412            public void moveFileEntry(FileEntry fileEntry)
413                    throws PortalException, SystemException {
414    
415                    if (!isStagingGroup(fileEntry.getGroupId())) {
416                            dlSyncLocalService.updateSync(
417                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
418                                    fileEntry.getTitle(), fileEntry.getDescription(),
419                                    DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
420                    }
421            }
422    
423            public FileEntry moveFileEntryFromTrash(
424                            long userId, FileEntry fileEntry, long newFolderId,
425                            ServiceContext serviceContext)
426                    throws PortalException, SystemException {
427    
428                    boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
429                            userId, fileEntry.getFileEntryId());
430    
431                    if (!hasLock) {
432                            dlFileEntryLocalService.lockFileEntry(
433                                    userId, fileEntry.getFileEntryId());
434                    }
435    
436                    try {
437                            return doMoveFileEntryFromTrash(
438                                    userId, fileEntry, newFolderId, serviceContext);
439                    }
440                    finally {
441                            if (!hasLock) {
442                                    dlFileEntryLocalService.unlockFileEntry(
443                                            fileEntry.getFileEntryId());
444                            }
445                    }
446            }
447    
448            /**
449             * Moves the file entry to the recycle bin.
450             *
451             * @param  userId the primary key of the user moving the file entry
452             * @param  fileEntry the file entry to be moved
453             * @return the moved file entry
454             * @throws PortalException if a user with the primary key could not be found
455             * @throws SystemException if a system exception occurred
456             */
457            public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
458                    throws PortalException, SystemException {
459    
460                    boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
461                            userId, fileEntry.getFileEntryId());
462    
463                    if (!hasLock) {
464                            dlFileEntryLocalService.lockFileEntry(
465                                    userId, fileEntry.getFileEntryId());
466                    }
467    
468                    try {
469                            return doMoveFileEntryToTrash(userId, fileEntry);
470                    }
471                    finally {
472                            if (!hasLock) {
473                                    dlFileEntryLocalService.unlockFileEntry(
474                                            fileEntry.getFileEntryId());
475                            }
476                    }
477            }
478    
479            public DLFileShortcut moveFileShortcutFromTrash(
480                            long userId, DLFileShortcut dlFileShortcut, long newFolderId,
481                            ServiceContext serviceContext)
482                    throws PortalException, SystemException {
483    
484                    if (dlFileShortcut.isInTrash()) {
485                            restoreFileShortcutFromTrash(userId, dlFileShortcut);
486                    }
487    
488                    return dlAppService.updateFileShortcut(
489                            dlFileShortcut.getFileShortcutId(), newFolderId,
490                            dlFileShortcut.getToFileEntryId(), serviceContext);
491            }
492    
493            /**
494             * Moves the file shortcut to the recycle bin.
495             *
496             * @param  userId the primary key of the user moving the file shortcut
497             * @param  dlFileShortcut the file shortcut to be moved
498             * @return the moved file shortcut
499             * @throws PortalException if a user with the primary key could not be found
500             * @throws SystemException if a system exception occurred
501             */
502            public DLFileShortcut moveFileShortcutToTrash(
503                            long userId, DLFileShortcut dlFileShortcut)
504                    throws PortalException, SystemException {
505    
506                    // File shortcut
507    
508                    int oldStatus = dlFileShortcut.getStatus();
509    
510                    dlFileShortcutLocalService.updateStatus(
511                            userId, dlFileShortcut.getFileShortcutId(),
512                            WorkflowConstants.STATUS_IN_TRASH, new ServiceContext());
513    
514                    // Social
515    
516                    socialActivityLocalService.addActivity(
517                            userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
518                            dlFileShortcut.getFileShortcutId(),
519                            SocialActivityConstants.TYPE_MOVE_TO_TRASH, StringPool.BLANK, 0);
520    
521                    // Trash
522    
523                    trashEntryLocalService.addTrashEntry(
524                            userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
525                            dlFileShortcut.getFileShortcutId(), oldStatus, null, null);
526    
527                    return dlFileShortcut;
528            }
529    
530            public void moveFolder(Folder folder)
531                    throws PortalException, SystemException {
532    
533                    if (!isStagingGroup(folder.getGroupId())) {
534                            dlSyncLocalService.updateSync(
535                                    folder.getFolderId(), folder.getParentFolderId(),
536                                    folder.getName(), folder.getDescription(),
537                                    DLSyncConstants.EVENT_UPDATE, "-1");
538                    }
539            }
540    
541            public Folder moveFolderFromTrash(
542                            long userId, Folder folder, long parentFolderId,
543                            ServiceContext serviceContext)
544                    throws PortalException, SystemException {
545    
546                    boolean hasLock = dlFolderService.hasFolderLock(folder.getFolderId());
547    
548                    Lock lock = null;
549    
550                    if (!hasLock) {
551                            lock = dlFolderService.lockFolder(folder.getFolderId());
552                    }
553    
554                    try {
555                            return doMoveFolderFromTrash(
556                                    userId, folder, parentFolderId, serviceContext);
557                    }
558                    finally {
559                            if (!hasLock) {
560                                    dlFolderService.unlockFolder(
561                                            folder.getGroupId(), folder.getFolderId(), lock.getUuid());
562                            }
563                    }
564            }
565    
566            /**
567             * Moves the folder to the recycle bin.
568             *
569             * @param  userId the primary key of the user moving the folder
570             * @param  folder the folder to be moved
571             * @return the moved folder
572             * @throws PortalException if a user with the primary key could not be found
573             * @throws SystemException if a system exception occurred
574             */
575            public Folder moveFolderToTrash(long userId, Folder folder)
576                    throws PortalException, SystemException {
577    
578                    boolean hasLock = dlFolderService.hasFolderLock(folder.getFolderId());
579    
580                    Lock lock = null;
581    
582                    if (!hasLock) {
583                            lock = dlFolderService.lockFolder(folder.getFolderId());
584                    }
585    
586                    try {
587                            return doMoveFolderToTrash(userId, folder);
588                    }
589                    finally {
590                            if (!hasLock) {
591                                    dlFolderService.unlockFolder(
592                                            folder.getGroupId(), folder.getFolderId(), lock.getUuid());
593                            }
594                    }
595            }
596    
597            public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
598                    throws PortalException, SystemException {
599    
600                    // File entry
601    
602                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
603    
604                    dlFileEntry.setTitle(
605                            TrashUtil.getOriginalTitle(dlFileEntry.getTitle()));
606    
607                    dlFileEntryPersistence.update(dlFileEntry);
608    
609                    FileVersion fileVersion = new LiferayFileVersion(
610                            dlFileEntry.getLatestFileVersion(true));
611    
612                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
613                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
614    
615                    // File version
616    
617                    Map<String, Serializable> workflowContext =
618                            new HashMap<String, Serializable>();
619    
620                    List<TrashVersion> trashVersions = trashEntryLocalService.getVersions(
621                            trashEntry.getEntryId());
622    
623                    workflowContext.put("trashVersions", (Serializable)trashVersions);
624    
625                    dlFileEntryLocalService.updateStatus(
626                            userId, fileVersion.getFileVersionId(), trashEntry.getStatus(),
627                            workflowContext, new ServiceContext());
628    
629                    if (!DLAppHelperThreadLocal.isEnabled()) {
630                            return;
631                    }
632    
633                    // File shortcut
634    
635                    dlFileShortcutLocalService.enableFileShortcuts(
636                            fileEntry.getFileEntryId());
637    
638                    // File rank
639    
640                    dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
641    
642                    // Social
643    
644                    socialActivityCounterLocalService.enableActivityCounters(
645                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
646    
647                    socialActivityLocalService.addActivity(
648                            userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
649                            fileEntry.getFileEntryId(),
650                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH, StringPool.BLANK,
651                            0);
652            }
653    
654            public void restoreFileShortcutFromTrash(
655                            long userId, DLFileShortcut dlFileShortcut)
656                    throws PortalException, SystemException {
657    
658                    // File shortcut
659    
660                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
661                            DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId());
662    
663                    dlFileShortcutLocalService.updateStatus(
664                            userId, dlFileShortcut.getFileShortcutId(), trashEntry.getStatus(),
665                            new ServiceContext());
666    
667                    // Social
668    
669                    socialActivityCounterLocalService.enableActivityCounters(
670                            DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId());
671    
672                    socialActivityLocalService.addActivity(
673                            userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
674                            dlFileShortcut.getFileShortcutId(),
675                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH, StringPool.BLANK,
676                            0);
677    
678                    // Trash
679    
680                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
681            }
682    
683            public void restoreFolderFromTrash(long userId, Folder folder)
684                    throws PortalException, SystemException {
685    
686                    // Folder
687    
688                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
689                            DLFolderConstants.getClassName(), folder.getFolderId());
690    
691                    dlFolderLocalService.updateStatus(
692                            userId, folder.getFolderId(), WorkflowConstants.STATUS_APPROVED,
693                            new HashMap<String, Serializable>(), new ServiceContext());
694    
695                    // File rank
696    
697                    dlFileRankLocalService.enableFileRanksByFolderId(folder.getFolderId());
698    
699                    // Trash
700    
701                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
702            }
703    
704            public AssetEntry updateAsset(
705                            long userId, FileEntry fileEntry, FileVersion fileVersion,
706                            long assetClassPk)
707                    throws PortalException, SystemException {
708    
709                    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
710                            DLFileEntryConstants.getClassName(), assetClassPk);
711                    String[] assetTagNames = assetTagLocalService.getTagNames(
712                            DLFileEntryConstants.getClassName(), assetClassPk);
713    
714                    AssetEntry assetEntry = assetEntryLocalService.getEntry(
715                            DLFileEntryConstants.getClassName(), assetClassPk);
716    
717                    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
718                            assetEntry.getEntryId());
719    
720                    long[] assetLinkIds = StringUtil.split(
721                            ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
722    
723                    return updateAsset(
724                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
725                            assetLinkIds);
726            }
727    
728            public AssetEntry updateAsset(
729                            long userId, FileEntry fileEntry, FileVersion fileVersion,
730                            long[] assetCategoryIds, String[] assetTagNames,
731                            long[] assetLinkEntryIds)
732                    throws PortalException, SystemException {
733    
734                    AssetEntry assetEntry = null;
735    
736                    boolean visible = false;
737    
738                    boolean addDraftAssetEntry = false;
739    
740                    if (fileEntry instanceof LiferayFileEntry) {
741                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
742    
743                            if (dlFileVersion.isApproved()) {
744                                    visible = true;
745                            }
746                            else {
747                                    String version = dlFileVersion.getVersion();
748    
749                                    if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
750                                            addDraftAssetEntry = true;
751                                    }
752                            }
753                    }
754                    else {
755                            visible = true;
756                    }
757    
758                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
759    
760                    if (addDraftAssetEntry) {
761                            assetEntry = assetEntryLocalService.updateEntry(
762                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
763                                    fileEntry.getModifiedDate(),
764                                    DLFileEntryConstants.getClassName(),
765                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
766                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
767                                    null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
768                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
769                                    false);
770                    }
771                    else {
772                            assetEntry = assetEntryLocalService.updateEntry(
773                                    userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
774                                    fileEntry.getModifiedDate(),
775                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
776                                    fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
777                                    assetTagNames, visible, null, null, null,
778                                    fileEntry.getMimeType(), fileEntry.getTitle(),
779                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
780                                    false);
781    
782                            List<DLFileShortcut> dlFileShortcuts =
783                                    dlFileShortcutPersistence.findByToFileEntryId(
784                                            fileEntry.getFileEntryId());
785    
786                            for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
787                                    assetEntryLocalService.updateEntry(
788                                            userId, dlFileShortcut.getGroupId(),
789                                            dlFileShortcut.getCreateDate(),
790                                            dlFileShortcut.getModifiedDate(),
791                                            DLFileShortcut.class.getName(),
792                                            dlFileShortcut.getFileShortcutId(),
793                                            dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
794                                            assetTagNames, true, null, null, null,
795                                            fileEntry.getMimeType(), fileEntry.getTitle(),
796                                            fileEntry.getDescription(), null, null, null, 0, 0, null,
797                                            false);
798                            }
799                    }
800    
801                    assetLinkLocalService.updateLinks(
802                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
803                            AssetLinkConstants.TYPE_RELATED);
804    
805                    return assetEntry;
806            }
807    
808            public AssetEntry updateAsset(
809                            long userId, Folder folder, long[] assetCategoryIds,
810                            String[] assetTagNames, long[] assetLinkEntryIds)
811                    throws PortalException, SystemException {
812    
813                    AssetEntry assetEntry = null;
814    
815                    boolean visible = false;
816    
817                    if (folder instanceof LiferayFolder) {
818                            DLFolder dlFolder = (DLFolder)folder.getModel();
819    
820                            if (dlFolder.isApproved() && !dlFolder.isHidden() &&
821                                    !dlFolder.isInHiddenFolder()) {
822    
823                                    visible = true;
824                            }
825                    }
826                    else {
827                            visible = true;
828                    }
829    
830                    assetEntry = assetEntryLocalService.updateEntry(
831                            userId, folder.getGroupId(), folder.getCreateDate(),
832                            folder.getModifiedDate(), DLFolderConstants.getClassName(),
833                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
834                            assetTagNames, visible, null, null, null, null, folder.getName(),
835                            folder.getDescription(), null, null, null, 0, 0, null, false);
836    
837                    assetLinkLocalService.updateLinks(
838                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
839                            AssetLinkConstants.TYPE_RELATED);
840    
841                    return assetEntry;
842            }
843    
844            public void updateDependentStatus(
845                            User user, List<Object> dlFileEntriesAndDLFolders, int status)
846                    throws PortalException, SystemException {
847    
848                    for (Object object : dlFileEntriesAndDLFolders) {
849                            if (object instanceof DLFileEntry) {
850                                    DLFileEntry dlFileEntry = (DLFileEntry)object;
851    
852                                    List<DLFileVersion> dlFileVersions =
853                                            dlFileVersionLocalService.getFileVersions(
854                                                    dlFileEntry.getFileEntryId(),
855                                                    WorkflowConstants.STATUS_ANY);
856    
857                                    dlFileVersions = ListUtil.copy(dlFileVersions);
858    
859                                    Collections.sort(
860                                            dlFileVersions, new FileVersionVersionComparator());
861    
862                                    DLFileVersion latestDlFileVersion = dlFileVersions.get(0);
863    
864                                    if ((status == WorkflowConstants.STATUS_APPROVED) &&
865                                            (latestDlFileVersion.getStatus() ==
866                                                    WorkflowConstants.STATUS_IN_TRASH)) {
867    
868                                            continue;
869                                    }
870    
871                                    // File shortcut
872    
873                                    if (status == WorkflowConstants.STATUS_APPROVED) {
874                                            dlFileShortcutLocalService.enableFileShortcuts(
875                                                    dlFileEntry.getFileEntryId());
876                                    }
877                                    else {
878                                            dlFileShortcutLocalService.disableFileShortcuts(
879                                                    dlFileEntry.getFileEntryId());
880                                    }
881    
882                                    // Asset
883    
884                                    if (status == WorkflowConstants.STATUS_APPROVED) {
885                                            if (latestDlFileVersion.isApproved()) {
886                                                    assetEntryLocalService.updateVisible(
887                                                            DLFileEntryConstants.getClassName(),
888                                                            dlFileEntry.getFileEntryId(), true);
889                                            }
890                                    }
891                                    else {
892                                            assetEntryLocalService.updateVisible(
893                                                    DLFileEntryConstants.getClassName(),
894                                                    dlFileEntry.getFileEntryId(), false);
895                                    }
896    
897                                    // Social
898    
899                                    JSONObject extraDataJSONObject =
900                                            JSONFactoryUtil.createJSONObject();
901    
902                                    extraDataJSONObject.put("title", dlFileEntry.getTitle());
903    
904                                    if (status == WorkflowConstants.STATUS_APPROVED) {
905                                            socialActivityCounterLocalService.enableActivityCounters(
906                                                    DLFileEntryConstants.getClassName(),
907                                                    dlFileEntry.getFileEntryId());
908    
909                                            socialActivityLocalService.addActivity(
910                                                    user.getUserId(), dlFileEntry.getGroupId(),
911                                                    DLFileEntryConstants.getClassName(),
912                                                    dlFileEntry.getFileEntryId(),
913                                                    SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
914                                                    extraDataJSONObject.toString(), 0);
915                                    }
916                                    else if (latestDlFileVersion.getStatus() ==
917                                                            WorkflowConstants.STATUS_APPROVED) {
918    
919                                            socialActivityCounterLocalService.disableActivityCounters(
920                                                    DLFileEntryConstants.getClassName(),
921                                                    dlFileEntry.getFileEntryId());
922    
923                                            socialActivityLocalService.addActivity(
924                                                    user.getUserId(), dlFileEntry.getGroupId(),
925                                                    DLFileEntryConstants.getClassName(),
926                                                    dlFileEntry.getFileEntryId(),
927                                                    SocialActivityConstants.TYPE_MOVE_TO_TRASH,
928                                                    extraDataJSONObject.toString(), 0);
929                                    }
930    
931                                    // Index
932    
933                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
934                                            DLFileEntry.class);
935    
936                                    indexer.reindex(dlFileEntry);
937    
938                                    // Workflow
939    
940                                    if (status != WorkflowConstants.STATUS_APPROVED) {
941                                            for (DLFileVersion dlFileVersion : dlFileVersions) {
942                                                    if (!dlFileVersion.isPending()) {
943                                                            continue;
944                                                    }
945    
946                                                    dlFileVersion.setStatus(WorkflowConstants.STATUS_DRAFT);
947    
948                                                    dlFileVersionPersistence.update(dlFileVersion);
949    
950                                                    workflowInstanceLinkLocalService.
951                                                            deleteWorkflowInstanceLink(
952                                                                    dlFileVersion.getCompanyId(),
953                                                                    dlFileVersion.getGroupId(),
954                                                                    DLFileEntryConstants.getClassName(),
955                                                                    dlFileVersion.getFileVersionId());
956                                            }
957                                    }
958                            }
959                            else if (object instanceof DLFolder) {
960                                    DLFolder dlFolder = (DLFolder)object;
961    
962                                    if (dlFolder.isInTrash()) {
963                                            continue;
964                                    }
965    
966                                    QueryDefinition queryDefinition = new QueryDefinition(
967                                            WorkflowConstants.STATUS_ANY);
968    
969                                    List<Object> foldersAndFileEntriesAndFileShortcuts =
970                                            dlFolderLocalService.
971                                                    getFoldersAndFileEntriesAndFileShortcuts(
972                                                            dlFolder.getGroupId(), dlFolder.getFolderId(), null,
973                                                            false, queryDefinition);
974    
975                                    updateDependentStatus(
976                                            user, foldersAndFileEntriesAndFileShortcuts, status);
977                            }
978                    }
979            }
980    
981            public void updateFileEntry(
982                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
983                            FileVersion destinationFileVersion, long assetClassPk)
984                    throws PortalException, SystemException {
985    
986                    if (!DLAppHelperThreadLocal.isEnabled()) {
987                            return;
988                    }
989    
990                    boolean updateAsset = true;
991    
992                    if (fileEntry instanceof LiferayFileEntry &&
993                            fileEntry.getVersion().equals(
994                                    destinationFileVersion.getVersion())) {
995    
996                            updateAsset = false;
997                    }
998    
999                    if (updateAsset) {
1000                            updateAsset(
1001                                    userId, fileEntry, destinationFileVersion, assetClassPk);
1002                    }
1003    
1004                    registerDLProcessorCallback(fileEntry, sourceFileVersion);
1005            }
1006    
1007            public void updateFileEntry(
1008                            long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
1009                            FileVersion destinationFileVersion, ServiceContext serviceContext)
1010                    throws PortalException, SystemException {
1011    
1012                    if (!DLAppHelperThreadLocal.isEnabled()) {
1013                            return;
1014                    }
1015    
1016                    updateAsset(
1017                            userId, fileEntry, destinationFileVersion,
1018                            serviceContext.getAssetCategoryIds(),
1019                            serviceContext.getAssetTagNames(),
1020                            serviceContext.getAssetLinkEntryIds());
1021    
1022                    registerDLProcessorCallback(fileEntry, sourceFileVersion);
1023            }
1024    
1025            public void updateFolder(
1026                            long userId, Folder folder, ServiceContext serviceContext)
1027                    throws PortalException, SystemException {
1028    
1029                    updateAsset(
1030                            userId, folder, serviceContext.getAssetCategoryIds(),
1031                            serviceContext.getAssetTagNames(),
1032                            serviceContext.getAssetLinkEntryIds());
1033    
1034                    if (!isStagingGroup(folder.getGroupId())) {
1035                            dlSyncLocalService.updateSync(
1036                                    folder.getFolderId(), folder.getParentFolderId(),
1037                                    folder.getName(), folder.getDescription(),
1038                                    DLSyncConstants.EVENT_UPDATE, "-1");
1039                    }
1040            }
1041    
1042            public void updateStatus(
1043                            long userId, FileEntry fileEntry, FileVersion latestFileVersion,
1044                            int oldStatus, int newStatus,
1045                            Map<String, Serializable> workflowContext,
1046                            ServiceContext serviceContext)
1047                    throws PortalException, SystemException {
1048    
1049                    if (!DLAppHelperThreadLocal.isEnabled()) {
1050                            return;
1051                    }
1052    
1053                    if (newStatus == WorkflowConstants.STATUS_APPROVED) {
1054    
1055                            // Asset
1056    
1057                            String latestFileVersionVersion = latestFileVersion.getVersion();
1058    
1059                            if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
1060                                    if (!latestFileVersionVersion.equals(
1061                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
1062    
1063                                            AssetEntry draftAssetEntry = null;
1064    
1065                                            try {
1066                                                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
1067    
1068                                                    draftAssetEntry = assetEntryLocalService.getEntry(
1069                                                            DLFileEntryConstants.getClassName(),
1070                                                            latestFileVersion.getPrimaryKey());
1071    
1072                                                    long[] assetCategoryIds =
1073                                                            draftAssetEntry.getCategoryIds();
1074                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
1075    
1076                                                    List<AssetLink> assetLinks =
1077                                                            assetLinkLocalService.getDirectLinks(
1078                                                                    draftAssetEntry.getEntryId(),
1079                                                                    AssetLinkConstants.TYPE_RELATED);
1080    
1081                                                    long[] assetLinkEntryIds = StringUtil.split(
1082                                                            ListUtil.toString(
1083                                                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
1084    
1085                                                    AssetEntry assetEntry =
1086                                                            assetEntryLocalService.updateEntry(
1087                                                                    userId, fileEntry.getGroupId(),
1088                                                                    fileEntry.getCreateDate(),
1089                                                                    fileEntry.getModifiedDate(),
1090                                                                    DLFileEntryConstants.getClassName(),
1091                                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
1092                                                                    fileEntryTypeId, assetCategoryIds,
1093                                                                    assetTagNames, true, null, null, null,
1094                                                                    draftAssetEntry.getMimeType(),
1095                                                                    fileEntry.getTitle(),
1096                                                                    fileEntry.getDescription(), null, null, null, 0,
1097                                                                    0, null, false);
1098    
1099                                                    assetLinkLocalService.updateLinks(
1100                                                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
1101                                                            AssetLinkConstants.TYPE_RELATED);
1102    
1103                                                    assetEntryLocalService.deleteEntry(
1104                                                            draftAssetEntry.getEntryId());
1105                                            }
1106                                            catch (NoSuchEntryException nsee) {
1107                                            }
1108                                    }
1109    
1110                                    assetEntryLocalService.updateVisible(
1111                                            DLFileEntryConstants.getClassName(),
1112                                            fileEntry.getFileEntryId(), true);
1113                            }
1114    
1115                            // Sync
1116    
1117                            String event = (String)workflowContext.get("event");
1118    
1119                            if (!isStagingGroup(fileEntry.getGroupId()) &&
1120                                    Validator.isNotNull(event)) {
1121    
1122                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
1123                                            dlSyncLocalService.addSync(
1124                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
1125                                                    fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
1126                                                    fileEntry.getFolderId(), fileEntry.getTitle(),
1127                                                    fileEntry.getDescription(), DLSyncConstants.TYPE_FILE,
1128                                                    fileEntry.getVersion());
1129                                    }
1130                                    else if (event.equals(DLSyncConstants.EVENT_UPDATE)) {
1131                                            dlSyncLocalService.updateSync(
1132                                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
1133                                                    fileEntry.getTitle(), fileEntry.getDescription(),
1134                                                    DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
1135                                    }
1136                            }
1137    
1138                            if ((oldStatus != WorkflowConstants.STATUS_IN_TRASH) &&
1139                                    !latestFileVersion.isInTrashContainer()) {
1140    
1141                                    // Social
1142    
1143                                    Date activityDate = latestFileVersion.getModifiedDate();
1144    
1145                                    int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
1146    
1147                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
1148                                            activityDate = latestFileVersion.getCreateDate();
1149    
1150                                            activityType = DLActivityKeys.ADD_FILE_ENTRY;
1151                                    }
1152    
1153                                    socialActivityLocalService.addUniqueActivity(
1154                                            latestFileVersion.getStatusByUserId(),
1155                                            fileEntry.getGroupId(), activityDate,
1156                                            DLFileEntryConstants.getClassName(),
1157                                            fileEntry.getFileEntryId(), activityType, StringPool.BLANK,
1158                                            0);
1159    
1160                                    // Subscriptions
1161    
1162                                    notifySubscribers(latestFileVersion, serviceContext);
1163                            }
1164                    }
1165                    else {
1166    
1167                            // Asset
1168    
1169                            boolean visible = false;
1170    
1171                            if (newStatus != WorkflowConstants.STATUS_IN_TRASH) {
1172                                    List<DLFileVersion> approvedFileVersions =
1173                                            dlFileVersionPersistence.findByF_S(
1174                                                    fileEntry.getFileEntryId(),
1175                                                    WorkflowConstants.STATUS_APPROVED);
1176    
1177                                    if (!approvedFileVersions.isEmpty()) {
1178                                            visible = true;
1179                                    }
1180                            }
1181    
1182                            assetEntryLocalService.updateVisible(
1183                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1184                                    visible);
1185                    }
1186            }
1187    
1188            protected FileEntry doMoveFileEntryFromTrash(
1189                            long userId, FileEntry fileEntry, long newFolderId,
1190                            ServiceContext serviceContext)
1191                    throws PortalException, SystemException {
1192    
1193                    // File entry
1194    
1195                    List<DLFileVersion> dlFileVersions =
1196                            dlFileVersionLocalService.getFileVersions(
1197                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1198    
1199                    dlFileVersions = ListUtil.sort(
1200                            dlFileVersions, new FileVersionVersionComparator());
1201    
1202                    FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
1203    
1204                    if (fileVersion.isInTrash()) {
1205                            restoreFileEntryFromTrash(userId, fileEntry);
1206    
1207                            DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
1208                                    userId, fileEntry.getFileEntryId(), newFolderId,
1209                                    serviceContext);
1210    
1211                            if (DLAppHelperThreadLocal.isEnabled()) {
1212                                    dlFileRankLocalService.enableFileRanks(
1213                                            fileEntry.getFileEntryId());
1214                            }
1215    
1216                            return new LiferayFileEntry(dlFileEntry);
1217                    }
1218                    else {
1219                            dlFileEntryLocalService.updateStatus(
1220                                    userId, fileVersion.getFileVersionId(), fileVersion.getStatus(),
1221                                    new HashMap<String, Serializable>(), serviceContext);
1222    
1223                            if (DLAppHelperThreadLocal.isEnabled()) {
1224    
1225                                    // File rank
1226    
1227                                    dlFileRankLocalService.enableFileRanks(
1228                                            fileEntry.getFileEntryId());
1229    
1230                                    // File shortcut
1231    
1232                                    dlFileShortcutLocalService.enableFileShortcuts(
1233                                            fileEntry.getFileEntryId());
1234                            }
1235    
1236                            // App helper
1237    
1238                            fileEntry = dlAppService.moveFileEntry(
1239                                    fileEntry.getFileEntryId(), newFolderId, serviceContext);
1240    
1241                            // Social
1242    
1243                            socialActivityCounterLocalService.enableActivityCounters(
1244                                    DLFileEntryConstants.getClassName(),
1245                                    fileEntry.getFileEntryId());
1246    
1247                            socialActivityLocalService.addActivity(
1248                                    userId, fileEntry.getGroupId(),
1249                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1250                                    SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1251                                    StringPool.BLANK, 0);
1252    
1253                            return fileEntry;
1254                    }
1255            }
1256    
1257            protected FileEntry doMoveFileEntryToTrash(long userId, FileEntry fileEntry)
1258                    throws PortalException, SystemException {
1259    
1260                    // File versions
1261    
1262                    List<DLFileVersion> dlFileVersions =
1263                            dlFileVersionLocalService.getFileVersions(
1264                                    fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1265    
1266                    dlFileVersions = ListUtil.sort(
1267                            dlFileVersions, new FileVersionVersionComparator());
1268    
1269                    FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
1270    
1271                    Map<String, Serializable> workflowContext =
1272                            new HashMap<String, Serializable>();
1273    
1274                    workflowContext.put("dlFileVersions", (Serializable)dlFileVersions);
1275    
1276                    int oldStatus = fileVersion.getStatus();
1277    
1278                    dlFileEntryLocalService.updateStatus(
1279                            userId, fileVersion.getFileVersionId(),
1280                            WorkflowConstants.STATUS_IN_TRASH, workflowContext,
1281                            new ServiceContext());
1282    
1283                    // File entry
1284    
1285                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1286    
1287                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
1288                            DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId());
1289    
1290                    String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
1291    
1292                    dlFileEntry.setTitle(trashTitle);
1293    
1294                    dlFileEntryPersistence.update(dlFileEntry);
1295    
1296                    if (!DLAppHelperThreadLocal.isEnabled()) {
1297                            return fileEntry;
1298                    }
1299    
1300                    // File shortcut
1301    
1302                    dlFileShortcutLocalService.disableFileShortcuts(
1303                            fileEntry.getFileEntryId());
1304    
1305                    // File rank
1306    
1307                    dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());
1308    
1309                    // Social
1310    
1311                    socialActivityCounterLocalService.disableActivityCounters(
1312                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
1313    
1314                    socialActivityLocalService.addActivity(
1315                            userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
1316                            fileEntry.getFileEntryId(),
1317                            SocialActivityConstants.TYPE_MOVE_TO_TRASH, StringPool.BLANK, 0);
1318    
1319                    // Workflow
1320    
1321                    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1322                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
1323                                    fileVersion.getCompanyId(), fileVersion.getGroupId(),
1324                                    DLFileEntryConstants.getClassName(),
1325                                    fileVersion.getFileVersionId());
1326                    }
1327    
1328                    return fileEntry;
1329            }
1330    
1331            protected Folder doMoveFolderFromTrash(
1332                            long userId, Folder folder, long parentFolderId,
1333                            ServiceContext serviceContext)
1334                    throws PortalException, SystemException {
1335    
1336                    DLFolder dlFolder = (DLFolder)folder.getModel();
1337    
1338                    if (dlFolder.isInTrash()) {
1339                            restoreFolderFromTrash(userId, folder);
1340                    }
1341                    else {
1342    
1343                            // Folder
1344    
1345                            dlFolderLocalService.updateStatus(
1346                                    userId, folder.getFolderId(), WorkflowConstants.STATUS_APPROVED,
1347                                    new HashMap<String, Serializable>(), new ServiceContext());
1348    
1349                            // File rank
1350    
1351                            dlFileRankLocalService.enableFileRanksByFolderId(
1352                                    folder.getFolderId());
1353                    }
1354    
1355                    return dlAppService.moveFolder(
1356                            folder.getFolderId(), parentFolderId, serviceContext);
1357            }
1358    
1359            protected Folder doMoveFolderToTrash(long userId, Folder folder)
1360                    throws PortalException, SystemException {
1361    
1362                    // Folder
1363    
1364                    DLFolder dlFolder = dlFolderLocalService.updateStatus(
1365                            userId, folder.getFolderId(), WorkflowConstants.STATUS_IN_TRASH,
1366                            new HashMap<String, Serializable>(), new ServiceContext());
1367    
1368                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
1369                            DLFolderConstants.getClassName(), dlFolder.getFolderId());
1370    
1371                    String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
1372    
1373                    dlFolder.setName(trashTitle);
1374    
1375                    dlFolderPersistence.update(dlFolder);
1376    
1377                    // File rank
1378    
1379                    dlFileRankLocalService.disableFileRanksByFolderId(folder.getFolderId());
1380    
1381                    return new LiferayFolder(dlFolder);
1382            }
1383    
1384            protected long getFileEntryTypeId(FileEntry fileEntry) {
1385                    if (fileEntry instanceof LiferayFileEntry) {
1386                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1387    
1388                            return dlFileEntry.getFileEntryTypeId();
1389                    }
1390                    else {
1391                            return 0;
1392                    }
1393            }
1394    
1395            protected boolean isStagingGroup(long groupId) {
1396                    try {
1397                            Group group = groupLocalService.getGroup(groupId);
1398    
1399                            return group.isStagingGroup();
1400                    }
1401                    catch (Exception e) {
1402                            return false;
1403                    }
1404            }
1405    
1406            protected void notifySubscribers(
1407                            FileVersion fileVersion, ServiceContext serviceContext)
1408                    throws PortalException, SystemException {
1409    
1410                    if (!fileVersion.isApproved()) {
1411                            return;
1412                    }
1413    
1414                    PortletPreferences preferences =
1415                            ServiceContextUtil.getPortletPreferences(serviceContext);
1416    
1417                    if (preferences == null) {
1418                            long ownerId = fileVersion.getGroupId();
1419                            int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
1420                            long plid = PortletKeys.PREFS_PLID_SHARED;
1421                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
1422                            String defaultPreferences = null;
1423    
1424                            preferences = portletPreferencesLocalService.getPreferences(
1425                                    fileVersion.getCompanyId(), ownerId, ownerType, plid, portletId,
1426                                    defaultPreferences);
1427                    }
1428    
1429                    if (serviceContext.isCommandAdd() &&
1430                            DLUtil.getEmailFileEntryAddedEnabled(preferences)) {
1431                    }
1432                    else if (serviceContext.isCommandUpdate() &&
1433                            DLUtil.getEmailFileEntryUpdatedEnabled(preferences)) {
1434                    }
1435                    else {
1436                            return;
1437                    }
1438    
1439                    String fromName = DLUtil.getEmailFromName(
1440                            preferences, fileVersion.getCompanyId());
1441                    String fromAddress = DLUtil.getEmailFromAddress(
1442                            preferences, fileVersion.getCompanyId());
1443    
1444                    Map<Locale, String> localizedSubjectMap = null;
1445                    Map<Locale, String> localizedBodyMap = null;
1446    
1447                    if (serviceContext.isCommandUpdate()) {
1448                            localizedSubjectMap = DLUtil.getEmailFileEntryUpdatedSubjectMap(
1449                                    preferences);
1450                            localizedBodyMap = DLUtil.getEmailFileEntryUpdatedBodyMap(
1451                                    preferences);
1452                    }
1453                    else {
1454                            localizedSubjectMap = DLUtil.getEmailFileEntryAddedSubjectMap(
1455                                    preferences);
1456                            localizedBodyMap = DLUtil.getEmailFileEntryAddedBodyMap(
1457                                    preferences);
1458                    }
1459    
1460                    FileEntry fileEntry = fileVersion.getFileEntry();
1461    
1462                    Folder folder = null;
1463    
1464                    long folderId = fileEntry.getFolderId();
1465    
1466                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1467                            folder = dlAppLocalService.getFolder(folderId);
1468                    }
1469    
1470                    String folderName = LanguageUtil.get(
1471                            serviceContext.getLocale(), "home");
1472    
1473                    if (folder != null) {
1474                            folderName = folder.getName();
1475                    }
1476    
1477                    SubscriptionSender subscriptionSender = new SubscriptionSender();
1478    
1479                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1480    
1481                    DLFileEntryType dlFileEntryType =
1482                            dlFileEntryTypeLocalService.getDLFileEntryType(
1483                                    dlFileEntry.getFileEntryTypeId());
1484    
1485                    subscriptionSender.setCompanyId(fileVersion.getCompanyId());
1486                    subscriptionSender.setContextAttributes(
1487                            "[$DOCUMENT_STATUS_BY_USER_NAME$]",
1488                            fileVersion.getStatusByUserName(), "[$DOCUMENT_TITLE$]",
1489                            fileVersion.getTitle(), "[$DOCUMENT_TYPE$]",
1490                            dlFileEntryType.getName(), "[$FOLDER_NAME$]", folderName);
1491                    subscriptionSender.setContextUserPrefix("DOCUMENT");
1492                    subscriptionSender.setFrom(fromAddress, fromName);
1493                    subscriptionSender.setHtmlFormat(true);
1494                    subscriptionSender.setLocalizedBodyMap(localizedBodyMap);
1495                    subscriptionSender.setLocalizedSubjectMap(localizedSubjectMap);
1496                    subscriptionSender.setMailId(
1497                            "file_entry", fileVersion.getFileEntryId());
1498                    subscriptionSender.setPortletId(PortletKeys.DOCUMENT_LIBRARY);
1499                    subscriptionSender.setReplyToAddress(fromAddress);
1500                    subscriptionSender.setScopeGroupId(fileVersion.getGroupId());
1501                    subscriptionSender.setServiceContext(serviceContext);
1502                    subscriptionSender.setUserId(fileVersion.getUserId());
1503    
1504                    subscriptionSender.addPersistedSubscribers(
1505                            Folder.class.getName(), fileVersion.getGroupId());
1506    
1507                    List<Long> folderIds = new ArrayList<Long>();
1508    
1509                    if (folder != null) {
1510                            folderIds.add(folder.getFolderId());
1511    
1512                            folderIds.addAll(folder.getAncestorFolderIds());
1513                    }
1514    
1515                    for (long curFolderId : folderIds) {
1516                            subscriptionSender.addPersistedSubscribers(
1517                                    Folder.class.getName(), curFolderId);
1518                    }
1519    
1520                    if (dlFileEntryType.getFileEntryTypeId() ==
1521                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
1522    
1523                            subscriptionSender.addPersistedSubscribers(
1524                                    DLFileEntryType.class.getName(), fileVersion.getGroupId());
1525                    }
1526                    else {
1527                            subscriptionSender.addPersistedSubscribers(
1528                                    DLFileEntryType.class.getName(),
1529                                    dlFileEntryType.getFileEntryTypeId());
1530                    }
1531    
1532                    subscriptionSender.flushNotificationsAsync();
1533            }
1534    
1535            protected void registerDLProcessorCallback(
1536                    final FileEntry fileEntry, final FileVersion fileVersion) {
1537    
1538                    TransactionCommitCallbackRegistryUtil.registerCallback(
1539                            new Callable<Void>() {
1540    
1541                                    public Void call() throws Exception {
1542                                            DLProcessorRegistryUtil.trigger(
1543                                                    fileEntry, fileVersion, true);
1544    
1545                                            return null;
1546                                    }
1547    
1548                            });
1549            }
1550    
1551    }