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