001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.getCompanyId(),
102                                    folder.getRepositoryId(), folder.getParentFolderId(),
103                                    DLSyncConstants.TYPE_FOLDER);
104                    }
105            }
106    
107            public void deleteFileEntry(FileEntry fileEntry)
108                    throws PortalException, SystemException {
109    
110                    // File previews
111    
112                    DLProcessorRegistryUtil.cleanUp(fileEntry);
113    
114                    // File ranks
115    
116                    dlFileRankLocalService.deleteFileRanksByFileEntryId(
117                            fileEntry.getFileEntryId());
118    
119                    // File shortcuts
120    
121                    dlFileShortcutLocalService.deleteFileShortcuts(
122                            fileEntry.getFileEntryId());
123    
124                    // Sync
125    
126                    if (!isStagingGroup(fileEntry.getGroupId())) {
127                            dlSyncLocalService.updateSync(
128                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
129                                    DLSyncConstants.EVENT_DELETE);
130                    }
131    
132                    // Asset
133    
134                    assetEntryLocalService.deleteEntry(
135                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
136    
137                    // Message boards
138    
139                    mbMessageLocalService.deleteDiscussionMessages(
140                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
141    
142                    // Ratings
143    
144                    ratingsStatsLocalService.deleteStats(
145                            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
146            }
147    
148            public void deleteFolder(Folder folder)
149                    throws PortalException, SystemException {
150    
151                    if (!isStagingGroup(folder.getGroupId())) {
152                            dlSyncLocalService.updateSync(
153                                    folder.getFolderId(), folder.getParentFolderId(),
154                                    DLSyncConstants.EVENT_DELETE);
155                    }
156            }
157    
158            public void getFileAsStream(
159                            long userId, FileEntry fileEntry, boolean incrementCounter)
160                    throws SystemException {
161    
162                    // File rank
163    
164                    if (userId > 0 && incrementCounter) {
165                            dlFileRankLocalService.updateFileRank(
166                                    fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
167                                    fileEntry.getFileEntryId(), new ServiceContext());
168                    }
169    
170                    // File read count
171    
172                    if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED && incrementCounter) {
173                            assetEntryLocalService.incrementViewCounter(
174                                    userId, DLFileEntryConstants.getClassName(),
175                                    fileEntry.getFileEntryId(), 1);
176    
177                            List<DLFileShortcut> fileShortcuts =
178                                    dlFileShortcutPersistence.findByToFileEntryId(
179                                    fileEntry.getFileEntryId());
180    
181                            for (DLFileShortcut fileShortcut : fileShortcuts) {
182                                    assetEntryLocalService.incrementViewCounter(
183                                            userId, DLFileShortcut.class.getName(),
184                                            fileShortcut.getFileShortcutId(), 1);
185                            }
186                    }
187            }
188    
189            public List<DLFileShortcut> getFileShortcuts(
190                            long groupId, long folderId, int status)
191                    throws SystemException {
192    
193                    return dlFileShortcutPersistence.findByG_F_S(groupId, folderId, status);
194            }
195    
196            public int getFileShortcutsCount(
197                            long groupId, long folderId, int status)
198                    throws SystemException {
199    
200                    return dlFileShortcutPersistence.countByG_F_S(
201                            groupId, folderId, status);
202            }
203    
204            public List<FileEntry> getNoAssetFileEntries() {
205                    return null;
206            }
207    
208            public AssetEntry updateAsset(
209                            long userId, FileEntry fileEntry, FileVersion fileVersion,
210                            long assetClassPk)
211                    throws PortalException, SystemException {
212    
213                    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
214                            DLFileEntryConstants.getClassName(), assetClassPk);
215                    String[] assetTagNames = assetTagLocalService.getTagNames(
216                            DLFileEntryConstants.getClassName(), assetClassPk);
217    
218                    AssetEntry assetEntry = assetEntryLocalService.getEntry(
219                            DLFileEntryConstants.getClassName(), assetClassPk);
220    
221                    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
222                            assetEntry.getEntryId());
223    
224                    long[] assetLinkIds = StringUtil.split(
225                            ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
226    
227                    return updateAsset(
228                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
229                            assetLinkIds);
230            }
231    
232            public AssetEntry updateAsset(
233                            long userId, FileEntry fileEntry, FileVersion fileVersion,
234                            long[] assetCategoryIds, String[] assetTagNames,
235                            long[] assetLinkEntryIds)
236                    throws PortalException, SystemException {
237    
238                    AssetEntry assetEntry = null;
239    
240                    boolean visible = false;
241    
242                    boolean addDraftAssetEntry = false;
243    
244                    if (fileEntry instanceof LiferayFileEntry) {
245                            DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
246    
247                            if (dlFileVersion.isApproved()) {
248                                    visible = true;
249                            }
250                            else {
251                                    String version = dlFileVersion.getVersion();
252    
253                                    if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
254                                            addDraftAssetEntry = true;
255                                    }
256                            }
257                    }
258                    else {
259                            visible = true;
260                    }
261    
262                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
263    
264                    if (addDraftAssetEntry) {
265                            assetEntry = assetEntryLocalService.updateEntry(
266                                    userId, fileEntry.getGroupId(),
267                                    DLFileEntryConstants.getClassName(),
268                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
269                                    fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
270                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
271                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
272                                    false);
273                    }
274                    else {
275                            assetEntry = assetEntryLocalService.updateEntry(
276                                    userId, fileEntry.getGroupId(),
277                                    DLFileEntryConstants.getClassName(),
278                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
279                                    fileEntryTypeId, assetCategoryIds, assetTagNames, visible, null,
280                                    null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
281                                    fileEntry.getDescription(), null, null, null, 0, 0, null,
282                                    false);
283    
284                            List<DLFileShortcut> dlFileShortcuts =
285                                    dlFileShortcutPersistence.findByToFileEntryId(
286                                            fileEntry.getFileEntryId());
287    
288                            for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
289                                    assetEntryLocalService.updateEntry(
290                                            userId, dlFileShortcut.getGroupId(),
291                                            DLFileShortcut.class.getName(),
292                                            dlFileShortcut.getFileShortcutId(),
293                                            dlFileShortcut.getUuid(), fileEntryTypeId,
294                                            assetCategoryIds, assetTagNames, true,
295                                            null, null, null, null, fileEntry.getMimeType(),
296                                            fileEntry.getTitle(), fileEntry.getDescription(), null,
297                                            null, null, 0, 0, null, false);
298                            }
299                    }
300    
301                    assetLinkLocalService.updateLinks(
302                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
303                            AssetLinkConstants.TYPE_RELATED);
304    
305                    return assetEntry;
306            }
307    
308            public void updateFileEntry(
309                            long userId, FileEntry fileEntry, FileVersion fileVersion,
310                            ServiceContext serviceContext)
311                    throws PortalException, SystemException {
312    
313                    updateAsset(
314                            userId, fileEntry, fileVersion,
315                            serviceContext.getAssetCategoryIds(),
316                            serviceContext.getAssetTagNames(),
317                            serviceContext.getAssetLinkEntryIds());
318    
319                    registerDLProcessorCallback(fileEntry);
320            }
321    
322            public void updateFolder(Folder folder, ServiceContext serviceContext)
323                    throws PortalException, SystemException {
324    
325                    if (!isStagingGroup(folder.getGroupId())) {
326                            dlSyncLocalService.updateSync(
327                                    folder.getFolderId(), folder.getParentFolderId(),
328                                    DLSyncConstants.EVENT_UPDATE);
329                    }
330            }
331    
332            public void updateStatus(
333                            long userId, FileEntry fileEntry, FileVersion latestFileVersion,
334                            int status, Map<String, Serializable> workflowContext)
335                    throws PortalException, SystemException {
336    
337                    if (status == WorkflowConstants.STATUS_APPROVED) {
338    
339                            // Asset
340    
341                            String latestFileVersionVersion = latestFileVersion.getVersion();
342    
343                            if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
344                                    if (!latestFileVersionVersion.equals(
345                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
346    
347                                            AssetEntry draftAssetEntry = null;
348    
349                                            try {
350                                                    long fileEntryTypeId = getFileEntryTypeId(fileEntry);
351    
352                                                    draftAssetEntry = assetEntryLocalService.getEntry(
353                                                            DLFileEntryConstants.getClassName(),
354                                                            latestFileVersion.getPrimaryKey());
355    
356                                                    long[] assetCategoryIds =
357                                                            draftAssetEntry.getCategoryIds();
358                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
359    
360                                                    List<AssetLink> assetLinks =
361                                                            assetLinkLocalService.getDirectLinks(
362                                                                    draftAssetEntry.getEntryId(),
363                                                                    AssetLinkConstants.TYPE_RELATED);
364    
365                                                    long[] assetLinkEntryIds = StringUtil.split(
366                                                            ListUtil.toString(
367                                                                    assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
368    
369                                                    AssetEntry assetEntry =
370                                                            assetEntryLocalService.updateEntry(
371                                                                    userId, fileEntry.getGroupId(),
372                                                                    DLFileEntryConstants.getClassName(),
373                                                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
374                                                                    fileEntryTypeId, assetCategoryIds,
375                                                                    assetTagNames, true, null, null, null, null,
376                                                                    draftAssetEntry.getMimeType(),
377                                                                    fileEntry.getTitle(),
378                                                                    fileEntry.getDescription(), null, null, null, 0,
379                                                                    0, null, false);
380    
381                                                    assetLinkLocalService.updateLinks(
382                                                            userId, assetEntry.getEntryId(),
383                                                            assetLinkEntryIds, AssetLinkConstants.TYPE_RELATED);
384    
385                                                    assetEntryLocalService.deleteEntry(
386                                                            draftAssetEntry.getEntryId());
387                                            }
388                                            catch (NoSuchEntryException nsee) {
389                                            }
390                                    }
391    
392                                    assetEntryLocalService.updateVisible(
393                                            DLFileEntryConstants.getClassName(),
394                                            fileEntry.getFileEntryId(), true);
395                            }
396    
397                            // Sync
398    
399                            String event = (String)workflowContext.get("event");
400    
401                            if (!isStagingGroup(fileEntry.getGroupId()) &&
402                                    Validator.isNotNull(event)) {
403    
404                                    if (event.equals(DLSyncConstants.EVENT_ADD)) {
405                                            dlSyncLocalService.addSync(
406                                                    fileEntry.getFileEntryId(), fileEntry.getCompanyId(),
407                                                    fileEntry.getRepositoryId(), fileEntry.getFolderId(),
408                                                    DLSyncConstants.TYPE_FILE);
409                                    }
410                                    else if (event.equals(DLSyncConstants.EVENT_UPDATE)) {
411                                            dlSyncLocalService.updateSync(
412                                                    fileEntry.getFileEntryId(), fileEntry.getFolderId(),
413                                                    DLSyncConstants.EVENT_UPDATE);
414                                    }
415                            }
416    
417                            // Social
418    
419                            int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
420    
421                            if (latestFileVersionVersion.equals(
422                                            DLFileEntryConstants.VERSION_DEFAULT)) {
423    
424                                    activityType = DLActivityKeys.ADD_FILE_ENTRY;
425                            }
426    
427                            socialActivityLocalService.addUniqueActivity(
428                                    latestFileVersion.getStatusByUserId(),
429                                    fileEntry.getGroupId(), latestFileVersion.getCreateDate(),
430                                    DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
431                                    activityType, StringPool.BLANK, 0);
432                    }
433                    else {
434    
435                            // Asset
436    
437                            if (Validator.isNull(fileEntry.getVersion())) {
438                                    assetEntryLocalService.updateVisible(
439                                            DLFileEntryConstants.getClassName(),
440                                            fileEntry.getFileEntryId(), false);
441                            }
442                    }
443            }
444    
445            protected long getFileEntryTypeId(FileEntry fileEntry) {
446                    if (fileEntry instanceof LiferayFileEntry) {
447                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
448    
449                            return dlFileEntry.getFileEntryTypeId();
450                    }
451                    else {
452                            return 0;
453                    }
454            }
455    
456            protected boolean isStagingGroup(long groupId) {
457                    try {
458                            Group group = groupLocalService.getGroup(groupId);
459    
460                            return group.isStagingGroup();
461                    }
462                    catch (Exception e) {
463                            return false;
464                    }
465            }
466    
467            protected void registerDLProcessorCallback(final FileEntry fileEntry) {
468                    TransactionCommitCallbackUtil.registerCallback(
469                            new Callable<Void>() {
470    
471                                    public Void call() throws Exception {
472                                            DLProcessorRegistryUtil.trigger(fileEntry);
473    
474                                            return null;
475                                    }
476    
477                            });
478            }
479    
480    }