001    /**
002     * Copyright (c) 2000-2011 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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.FileVersion;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.spring.transaction.TransactionCommitCallbackUtil;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.asset.NoSuchEntryException;
035    import com.liferay.portlet.asset.model.AssetEntry;
036    import com.liferay.portlet.asset.model.AssetLink;
037    import com.liferay.portlet.asset.model.AssetLinkConstants;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
040    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
041    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
042    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
043    import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
044    import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
045    import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
046    
047    import java.io.Serializable;
048    
049    import java.util.HashMap;
050    import java.util.List;
051    import java.util.Map;
052    import java.util.concurrent.Callable;
053    
054    /**
055     * @author Alexander Chow
056     */
057    public class DLAppHelperLocalServiceImpl
058            extends DLAppHelperLocalServiceBaseImpl {
059    
060            public void addFileEntry(
061                            long userId, FileEntry fileEntry, FileVersion fileVersion,
062                            ServiceContext serviceContext)
063                    throws PortalException, SystemException {
064    
065                    updateAsset(
066                            userId, fileEntry, fileVersion,
067                            serviceContext.getAssetCategoryIds(),
068                            serviceContext.getAssetTagNames(),
069                            serviceContext.getAssetLinkEntryIds());
070    
071                    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
072                            mbMessageLocalService.addDiscussionMessage(
073                                    fileEntry.getUserId(), fileEntry.getUserName(),
074                                    fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
075                                    fileEntry.getFileEntryId(), WorkflowConstants.ACTION_PUBLISH);
076                    }
077    
078                    if (fileVersion instanceof LiferayFileVersion) {
079                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
080    
081                            Map<String, Serializable> workflowContext =
082                                    new HashMap<String, Serializable>();
083    
084                            workflowContext.put("event", DLSyncConstants.EVENT_ADD);
085    
086                            WorkflowHandlerRegistryUtil.startWorkflowInstance(
087                                    dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(),
088                                    userId, DLFileEntry.class.getName(),
089                                    dlFileVersion.getFileVersionId(), dlFileVersion, serviceContext,
090                                    workflowContext);
091                    }
092    
093                    registerDLProcessorCallback(fileEntry);
094            }
095    
096            public void addFolder(Folder folder, ServiceContext serviceContext)
097                    throws SystemException {
098    
099                    if (!isStagingGroup(folder.getGroupId())) {
100                            dlSyncLocalService.addSync(
101                                    folder.getFolderId(), folder.getUuid(), folder.getCompanyId(),
102                                    folder.getRepositoryId(), folder.getParentFolderId(),
103                                    folder.getName(), DLSyncConstants.TYPE_FOLDER, "-1");
104                    }
105            }
106    
107            public void checkAssetEntry(
108                            long userId, FileEntry fileEntry, FileVersion fileVersion)
109                    throws PortalException, SystemException {
110    
111                    AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
112                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
113    
114                    long[] assetCategoryIds = new long[0];
115                    String[] assetTagNames = new String[0];
116    
117                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
118    
119                    if (fileEntryAssetEntry == null) {
120                            fileEntryAssetEntry = assetEntryLocalService.updateEntry(
121                                    userId, fileEntry.getGroupId(),
122                                    DLFileEntryConstants.getClassName(),
123                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
124                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
125                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
126                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
127                                    false);
128                    }
129    
130                    AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
131                            DLFileEntryConstants.getClassName(),
132                            fileVersion.getFileVersionId());
133    
134                    if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
135                            !fileVersion.getVersion().equals(
136                                    DLFileEntryConstants.VERSION_DEFAULT)) {
137    
138                            assetCategoryIds = assetCategoryLocalService.getCategoryIds(
139                                    DLFileEntryConstants.getClassName(),
140                                    fileEntry.getFileEntryId());
141                            assetTagNames = assetTagLocalService.getTagNames(
142                                    DLFileEntryConstants.getClassName(),
143                                    fileEntry.getFileEntryId());
144    
145                            fileVersionAssetEntry = assetEntryLocalService.updateEntry(
146                                    userId, fileEntry.getGroupId(),
147                                    DLFileEntryConstants.getClassName(),
148                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
149                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
150                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
151                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
152                                    false);
153    
154                            List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
155                                    fileEntryAssetEntry.getEntryId());
156    
157                            long[] assetLinkIds = StringUtil.split(
158                                    ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR),
159                                    0L);
160    
161                            assetLinkLocalService.updateLinks(
162                                    userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
163                                    AssetLinkConstants.TYPE_RELATED);
164                    }
165            }
166    
167            public void deleteFileEntry(FileEntry fileEntry)
168                    throws PortalException, SystemException {
169    
170                    // File previews
171    
172                    DLProcessorRegistryUtil.cleanUp(fileEntry);
173    
174                    // File ranks
175    
176                    dlFileRankLocalService.deleteFileRanksByFileEntryId(
177                            fileEntry.getFileEntryId());
178    
179                    // File shortcuts
180    
181                    dlFileShortcutLocalService.deleteFileShortcuts(
182                            fileEntry.getFileEntryId());
183    
184                    // Sync
185    
186                    if (!isStagingGroup(fileEntry.getGroupId())) {
187                            dlSyncLocalService.updateSync(
188                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
189                                    fileEntry.getTitle(), DLSyncConstants.EVENT_DELETE,
190                                    fileEntry.getVersion());
191                    }
192    
193                    // Asset
194    
195                    assetEntryLocalService.deleteEntry(
196                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
197    
198                    // Message boards
199    
200                    mbMessageLocalService.deleteDiscussionMessages(
201                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
202    
203                    // Ratings
204    
205                    ratingsStatsLocalService.deleteStats(
206                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
207            }
208    
209            public void deleteFolder(Folder folder)
210                    throws PortalException, SystemException {
211    
212                    if (!isStagingGroup(folder.getGroupId())) {
213                            dlSyncLocalService.updateSync(
214                                    folder.getFolderId(), folder.getParentFolderId(),
215                                    folder.getName(), DLSyncConstants.EVENT_DELETE, "-1");
216                    }
217            }
218    
219            public void getFileAsStream(
220                            long userId, FileEntry fileEntry, boolean incrementCounter)
221                    throws SystemException {
222    
223                    // File rank
224    
225                    if (userId > 0 && incrementCounter) {
226                            dlFileRankLocalService.updateFileRank(
227                                    fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
228                                    fileEntry.getFileEntryId(), new ServiceContext());
229                    }
230    
231                    // File read count
232    
233                    if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED && incrementCounter) {
234                            assetEntryLocalService.incrementViewCounter(
235                                    userId, DLFileEntryConstants.getClassName(),
236                                    fileEntry.getFileEntryId(), 1);
237    
238                            List<DLFileShortcut> fileShortcuts =
239                                    dlFileShortcutPersistence.findByToFileEntryId(
240                                    fileEntry.getFileEntryId());
241    
242                            for (DLFileShortcut fileShortcut : fileShortcuts) {
243                                    assetEntryLocalService.incrementViewCounter(
244                                            userId, DLFileShortcut.class.getName(),
245                                            fileShortcut.getFileShortcutId(), 1);
246                            }
247                    }
248            }
249    
250            public List<DLFileShortcut> getFileShortcuts(
251                            long groupId, long folderId, int status)
252                    throws SystemException {
253    
254                    return dlFileShortcutPersistence.findByG_F_S(groupId, folderId, status);
255            }
256    
257            public int getFileShortcutsCount(long groupId, long folderId, int status)
258                    throws SystemException {
259    
260                    return dlFileShortcutPersistence.countByG_F_S(
261                            groupId, folderId, status);
262            }
263    
264            public List<FileEntry> getNoAssetFileEntries() {
265                    return null;
266            }
267    
268            public void moveFileEntry(FileEntry fileEntry)
269                    throws PortalException, SystemException {
270    
271                    dlSyncLocalService.updateSync(
272                            fileEntry.getFileEntryId(), fileEntry.getFolderId(),
273                            fileEntry.getTitle(), DLSyncConstants.EVENT_UPDATE,
274                            fileEntry.getVersion());
275            }
276    
277            public void moveFolder(Folder folder)
278                    throws PortalException, SystemException {
279    
280                    dlSyncLocalService.updateSync(
281                            folder.getFolderId(), folder.getParentFolderId(),
282                            folder.getName(), DLSyncConstants.EVENT_UPDATE, "-1");
283            }
284    
285            public AssetEntry updateAsset(
286                            long userId, FileEntry fileEntry, FileVersion fileVersion,
287                            long assetClassPk)
288                    throws PortalException, SystemException {
289    
290                    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
291                            DLFileEntryConstants.getClassName(), assetClassPk);
292                    String[] assetTagNames = assetTagLocalService.getTagNames(
293                            DLFileEntryConstants.getClassName(), assetClassPk);
294    
295                    AssetEntry assetEntry = assetEntryLocalService.getEntry(
296                            DLFileEntryConstants.getClassName(), assetClassPk);
297    
298                    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
299                            assetEntry.getEntryId());
300    
301                    long[] assetLinkIds = StringUtil.split(
302                            ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
303    
304                    return updateAsset(
305                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
306                            assetLinkIds);
307            }
308    
309            public AssetEntry updateAsset(
310                            long userId, FileEntry fileEntry, FileVersion fileVersion,
311                            long[] assetCategoryIds, String[] assetTagNames,
312                            long[] assetLinkEntryIds)
313                    throws PortalException, SystemException {
314    
315                    AssetEntry assetEntry = null;
316    
317                    boolean visible = false;
318    
319                    boolean addDraftAssetEntry = false;
320    
321                    if (fileEntry instanceof LiferayFileEntry) {
322                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
323    
324                            if (dlFileVersion.isApproved()) {
325                                    visible = true;
326                            }
327                            else {
328                                    String version = dlFileVersion.getVersion();
329    
330                                    if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
331                                            addDraftAssetEntry = true;
332                                    }
333                            }
334                    }
335                    else {
336                            visible = true;
337                    }
338    
339                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
340    
341                    if (addDraftAssetEntry) {
342                            assetEntry = assetEntryLocalService.updateEntry(
343                                    userId, fileEntry.getGroupId(),
344                                    DLFileEntryConstants.getClassName(),
345                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
346                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
347                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
348                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
349                                    false);
350                    }
351                    else {
352                            assetEntry = assetEntryLocalService.updateEntry(
353                                    userId, fileEntry.getGroupId(),
354                                    DLFileEntryConstants.getClassName(),
355                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
356                                    fileEntryTypeId, assetCategoryIds, assetTagNames, visible, null,
357                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
358                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
359                                    false);
360    
361                            List<DLFileShortcut> dlFileShortcuts =
362                                    dlFileShortcutPersistence.findByToFileEntryId(
363                                            fileEntry.getFileEntryId());
364    
365                            for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
366                                    assetEntryLocalService.updateEntry(
367                                            userId, dlFileShortcut.getGroupId(),
368                                            DLFileShortcut.class.getName(),
369                                            dlFileShortcut.getFileShortcutId(),
370                                            dlFileShortcut.getUuid(), fileEntryTypeId,
371                                            assetCategoryIds, assetTagNames, true,
372                                            null, null, null, null, fileEntry.getMimeType(),
373                                            fileEntry.getTitle(), fileEntry.getDescription(), null,
374                                            null, null, 0, 0, null, false);
375                            }
376                    }
377    
378                    assetLinkLocalService.updateLinks(
379                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
380                            AssetLinkConstants.TYPE_RELATED);
381    
382                    return assetEntry;
383            }
384    
385            public void updateFileEntry(
386                            long userId, FileEntry fileEntry, FileVersion fileVersion,
387                            long assetClassPk)
388                    throws PortalException, SystemException {
389    
390                    boolean updateAsset = true;
391    
392                    if (fileEntry instanceof LiferayFileEntry &&
393                            fileEntry.getVersion().equals(fileVersion.getVersion())) {
394    
395                            updateAsset = false;
396                    }
397    
398                    if (updateAsset) {
399                            updateAsset(userId, fileEntry, fileVersion, assetClassPk);
400                    }
401    
402                    registerDLProcessorCallback(fileEntry);
403            }
404    
405            public void updateFileEntry(
406                            long userId, FileEntry fileEntry, FileVersion fileVersion,
407                            ServiceContext serviceContext)
408                    throws PortalException, SystemException {
409    
410                    updateAsset(
411                            userId, fileEntry, fileVersion,
412                            serviceContext.getAssetCategoryIds(),
413                            serviceContext.getAssetTagNames(),
414                            serviceContext.getAssetLinkEntryIds());
415    
416                    registerDLProcessorCallback(fileEntry);
417            }
418    
419            public void updateFolder(Folder folder, ServiceContext serviceContext)
420                    throws PortalException, SystemException {
421    
422                    if (!isStagingGroup(folder.getGroupId())) {
423                            dlSyncLocalService.updateSync(
424                                    folder.getFolderId(), folder.getParentFolderId(),
425                                    folder.getName(), DLSyncConstants.EVENT_UPDATE, "-1");
426                    }
427            }
428    
429            public void updateStatus(
430                            long userId, FileEntry fileEntry, FileVersion latestFileVersion,
431                            int status, Map<String, Serializable> workflowContext)
432                    throws PortalException, SystemException {
433    
434                    if (status == WorkflowConstants.STATUS_APPROVED) {
435    
436                            // Asset
437    
438                            String latestFileVersionVersion = latestFileVersion.getVersion();
439    
440                            if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
441                                    if (!latestFileVersionVersion.equals(
442                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
443    
444                                            AssetEntry draftAssetEntry = null;
445    
446                                            try {
447                                                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
448    
449                                                    draftAssetEntry = assetEntryLocalService.getEntry(
450                                                            DLFileEntryConstants.getClassName(),
451                                                            latestFileVersion.getPrimaryKey());
452    
453                                                    long[] assetCategoryIds =
454                                                            draftAssetEntry.getCategoryIds();
455                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
456    
457                                                    List<AssetLink> assetLinks =
458                                                            assetLinkLocalService.getDirectLinks(
459                                                                    draftAssetEntry.getEntryId(),
460                                                                    AssetLinkConstants.TYPE_RELATED);
461    
462                                                    long[] assetLinkEntryIds = StringUtil.split(
463                                                            ListUtil.toString(
464                                                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
465    
466                                                    AssetEntry assetEntry =
467                                                            assetEntryLocalService.updateEntry(
468                                                                    userId, fileEntry.getGroupId(),
469                                                                    DLFileEntryConstants.getClassName(),
470                                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
471                                                                    fileEntryTypeId, assetCategoryIds,
472                                                                    assetTagNames, true, null, null, null, null,
473                                                                    draftAssetEntry.getMimeType(),
474                                                                    fileEntry.getTitle(),
475                                                                    fileEntry.getDescription(), null, null, null, 0,
476                                                                    0, null, false);
477    
478                                                    assetLinkLocalService.updateLinks(
479                                                            userId, assetEntry.getEntryId(),
480                                                            assetLinkEntryIds, AssetLinkConstants.TYPE_RELATED);
481    
482                                                    assetEntryLocalService.deleteEntry(
483                                                            draftAssetEntry.getEntryId());
484                                            }
485                                            catch (NoSuchEntryException nsee) {
486                                            }
487                                    }
488    
489                                    assetEntryLocalService.updateVisible(
490                                            DLFileEntryConstants.getClassName(),
491                                            fileEntry.getFileEntryId(), true);
492                            }
493    
494                            // Sync
495    
496                            String event = (String)workflowContext.get("event");
497    
498                            if (!isStagingGroup(fileEntry.getGroupId()) &&
499                                    Validator.isNotNull(event)) {
500    
501                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
502                                            dlSyncLocalService.addSync(
503                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
504                                                    fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
505                                                    fileEntry.getFolderId(), fileEntry.getTitle(),
506                                                    DLSyncConstants.TYPE_FILE, fileEntry.getVersion());
507                                    }
508                                    else if (event.equals(DLSyncConstants.EVENT_UPDATE)) {
509                                            dlSyncLocalService.updateSync(
510                                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
511                                                    fileEntry.getTitle(), DLSyncConstants.EVENT_UPDATE,
512                                                    fileEntry.getVersion());
513                                    }
514                            }
515    
516                            // Social
517    
518                            int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
519    
520                            if (latestFileVersionVersion.equals(
521                                            DLFileEntryConstants.VERSION_DEFAULT)) {
522    
523                                    activityType = DLActivityKeys.ADD_FILE_ENTRY;
524                            }
525    
526                            socialActivityLocalService.addUniqueActivity(
527                                    latestFileVersion.getStatusByUserId(),
528                                    fileEntry.getGroupId(), latestFileVersion.getCreateDate(),
529                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
530                                    activityType, StringPool.BLANK, 0);
531                    }
532                    else {
533    
534                            // Asset
535    
536                            if (Validator.isNull(fileEntry.getVersion())) {
537                                    assetEntryLocalService.updateVisible(
538                                            DLFileEntryConstants.getClassName(),
539                                            fileEntry.getFileEntryId(), false);
540                            }
541                    }
542            }
543    
544            protected long getFileEntryTypeId(FileEntry fileEntry) {
545                    if (fileEntry instanceof LiferayFileEntry) {
546                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
547    
548                            return dlFileEntry.getFileEntryTypeId();
549                    }
550                    else {
551                            return 0;
552                    }
553            }
554    
555            protected boolean isStagingGroup(long groupId) {
556                    try {
557                            Group group = groupLocalService.getGroup(groupId);
558    
559                            return group.isStagingGroup();
560                    }
561                    catch (Exception e) {
562                            return false;
563                    }
564            }
565    
566            protected void registerDLProcessorCallback(final FileEntry fileEntry) {
567                    TransactionCommitCallbackUtil.registerCallback(
568                            new Callable<Void>() {
569    
570                                    public Void call() throws Exception {
571                                            DLProcessorRegistryUtil.trigger(fileEntry);
572    
573                                            return null;
574                                    }
575    
576                            });
577            }
578    
579    }