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