2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portlet.documentlibrary.service.impl;
\r
17 import com.liferay.portal.kernel.exception.PortalException;
\r
18 import com.liferay.portal.kernel.exception.SystemException;
\r
19 import com.liferay.portal.kernel.json.JSONFactoryUtil;
\r
20 import com.liferay.portal.kernel.json.JSONObject;
\r
21 import com.liferay.portal.kernel.repository.model.FileEntry;
\r
22 import com.liferay.portal.kernel.repository.model.FileVersion;
\r
23 import com.liferay.portal.kernel.repository.model.Folder;
\r
24 import com.liferay.portal.kernel.util.ListUtil;
\r
25 import com.liferay.portal.kernel.util.StringUtil;
\r
26 import com.liferay.portal.kernel.util.Validator;
\r
27 import com.liferay.portal.kernel.workflow.WorkflowConstants;
\r
28 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
\r
29 import com.liferay.portal.model.Group;
\r
30 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
\r
31 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
\r
32 import com.liferay.portal.service.ServiceContext;
\r
33 import com.liferay.portal.spring.transaction.TransactionCommitCallbackUtil;
\r
34 import com.liferay.portal.util.PropsValues;
\r
35 import com.liferay.portlet.asset.NoSuchEntryException;
\r
36 import com.liferay.portlet.asset.model.AssetEntry;
\r
37 import com.liferay.portlet.asset.model.AssetLink;
\r
38 import com.liferay.portlet.asset.model.AssetLinkConstants;
\r
39 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
\r
40 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
\r
41 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
\r
42 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
\r
43 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
\r
44 import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
\r
45 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
\r
46 import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
\r
48 import java.io.Serializable;
\r
50 import java.util.HashMap;
\r
51 import java.util.List;
\r
52 import java.util.Map;
\r
53 import java.util.concurrent.Callable;
\r
56 * @author Alexander Chow
\r
58 public class DLAppHelperLocalServiceImpl
\r
59 extends DLAppHelperLocalServiceBaseImpl {
\r
61 public void addFileEntry(
\r
62 long userId, FileEntry fileEntry, FileVersion fileVersion,
\r
63 ServiceContext serviceContext)
\r
64 throws PortalException, SystemException {
\r
67 userId, fileEntry, fileVersion,
\r
68 serviceContext.getAssetCategoryIds(),
\r
69 serviceContext.getAssetTagNames(),
\r
70 serviceContext.getAssetLinkEntryIds());
\r
72 if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
\r
73 mbMessageLocalService.addDiscussionMessage(
\r
74 fileEntry.getUserId(), fileEntry.getUserName(),
\r
75 fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
\r
76 fileEntry.getFileEntryId(), WorkflowConstants.ACTION_PUBLISH);
\r
79 if (fileVersion instanceof LiferayFileVersion) {
\r
80 DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
\r
82 Map<String, Serializable> workflowContext =
\r
83 new HashMap<String, Serializable>();
\r
85 workflowContext.put("event", DLSyncConstants.EVENT_ADD);
\r
87 WorkflowHandlerRegistryUtil.startWorkflowInstance(
\r
88 dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(),
\r
89 userId, DLFileEntryConstants.getClassName(),
\r
90 dlFileVersion.getFileVersionId(), dlFileVersion, serviceContext,
\r
94 registerDLProcessorCallback(fileEntry);
\r
97 public void addFolder(Folder folder, ServiceContext serviceContext)
\r
98 throws PortalException, SystemException {
\r
100 if (!isStagingGroup(folder.getGroupId())) {
\r
101 dlSyncLocalService.addSync(
\r
102 folder.getFolderId(), folder.getUuid(), folder.getCompanyId(),
\r
103 folder.getRepositoryId(), folder.getParentFolderId(),
\r
104 folder.getName(), folder.getDescription(),
\r
105 DLSyncConstants.TYPE_FOLDER, "-1");
\r
109 public void cancelCheckOut(
\r
110 long userId, FileEntry fileEntry,
\r
111 FileVersion destinationFileVersion, FileVersion draftFileVersion,
\r
112 ServiceContext serviceContext)
\r
113 throws PortalException, SystemException {
\r
116 userId, fileEntry, destinationFileVersion, serviceContext);
\r
118 if (draftFileVersion == null) {
\r
122 AssetEntry draftAssetEntry = null;
\r
125 draftAssetEntry = assetEntryLocalService.getEntry(
\r
126 DLFileEntryConstants.getClassName(),
\r
127 draftFileVersion.getPrimaryKey());
\r
129 assetEntryLocalService.deleteEntry(draftAssetEntry.getEntryId());
\r
131 catch (NoSuchEntryException nsee) {
\r
135 public void checkAssetEntry(
\r
136 long userId, FileEntry fileEntry, FileVersion fileVersion)
\r
137 throws PortalException, SystemException {
\r
139 AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
\r
140 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
\r
142 long[] assetCategoryIds = new long[0];
\r
143 String[] assetTagNames = new String[0];
\r
145 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
\r
147 if (fileEntryAssetEntry == null) {
\r
148 fileEntryAssetEntry = assetEntryLocalService.updateEntry(
\r
149 userId, fileEntry.getGroupId(),
\r
150 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
\r
151 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
\r
152 assetTagNames, false, null, null, null, null,
\r
153 fileEntry.getMimeType(), fileEntry.getTitle(),
\r
154 fileEntry.getDescription(), null, null, null, 0, 0, null,
\r
158 AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
\r
159 DLFileEntryConstants.getClassName(),
\r
160 fileVersion.getFileVersionId());
\r
162 if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
\r
163 !fileVersion.getVersion().equals(
\r
164 DLFileEntryConstants.VERSION_DEFAULT)) {
\r
166 assetCategoryIds = assetCategoryLocalService.getCategoryIds(
\r
167 DLFileEntryConstants.getClassName(),
\r
168 fileEntry.getFileEntryId());
\r
169 assetTagNames = assetTagLocalService.getTagNames(
\r
170 DLFileEntryConstants.getClassName(),
\r
171 fileEntry.getFileEntryId());
\r
173 fileVersionAssetEntry = assetEntryLocalService.updateEntry(
\r
174 userId, fileEntry.getGroupId(),
\r
175 DLFileEntryConstants.getClassName(),
\r
176 fileVersion.getFileVersionId(), fileEntry.getUuid(),
\r
177 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
\r
178 null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
\r
179 fileEntry.getDescription(), null, null, null, 0, 0, null,
\r
182 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
\r
183 fileEntryAssetEntry.getEntryId());
\r
185 long[] assetLinkIds = StringUtil.split(
\r
186 ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR),
\r
189 assetLinkLocalService.updateLinks(
\r
190 userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
\r
191 AssetLinkConstants.TYPE_RELATED);
\r
195 public void deleteFileEntry(FileEntry fileEntry)
\r
196 throws PortalException, SystemException {
\r
200 subscriptionLocalService.deleteSubscriptions(
\r
201 fileEntry.getCompanyId(), DLFileEntryConstants.getClassName(),
\r
202 fileEntry.getFileEntryId());
\r
206 DLProcessorRegistryUtil.cleanUp(fileEntry);
\r
210 dlFileRankLocalService.deleteFileRanksByFileEntryId(
\r
211 fileEntry.getFileEntryId());
\r
215 dlFileShortcutLocalService.deleteFileShortcuts(
\r
216 fileEntry.getFileEntryId());
\r
220 if (!isStagingGroup(fileEntry.getGroupId())) {
\r
221 dlSyncLocalService.updateSync(
\r
222 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
\r
223 fileEntry.getTitle(), fileEntry.getDescription(),
\r
224 DLSyncConstants.EVENT_DELETE, fileEntry.getVersion());
\r
227 /* Slow the folder delete function. We clean this unused data thanks cron job
\r
230 assetEntryLocalService.deleteEntry(
\r
231 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
\r
235 mbMessageLocalService.deleteDiscussionMessages(
\r
236 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
\r
240 ratingsStatsLocalService.deleteStats(
\r
241 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
\r
244 public void deleteFolder(Folder folder)
\r
245 throws PortalException, SystemException {
\r
247 if (!isStagingGroup(folder.getGroupId())) {
\r
248 dlSyncLocalService.updateSync(
\r
249 folder.getFolderId(), folder.getParentFolderId(),
\r
250 folder.getName(), folder.getDescription(),
\r
251 DLSyncConstants.EVENT_DELETE, "-1");
\r
255 public void getFileAsStream(
\r
256 long userId, FileEntry fileEntry, boolean incrementCounter)
\r
257 throws SystemException {
\r
259 if (!incrementCounter) {
\r
266 dlFileRankLocalService.updateFileRank(
\r
267 fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
\r
268 fileEntry.getFileEntryId(), new ServiceContext());
\r
273 assetEntryLocalService.incrementViewCounter(
\r
274 userId, DLFileEntryConstants.getClassName(),
\r
275 fileEntry.getFileEntryId(), 1);
\r
277 List<DLFileShortcut> fileShortcuts =
\r
278 dlFileShortcutPersistence.findByToFileEntryId(
\r
279 fileEntry.getFileEntryId());
\r
281 for (DLFileShortcut fileShortcut : fileShortcuts) {
\r
282 assetEntryLocalService.incrementViewCounter(
\r
283 userId, DLFileShortcut.class.getName(),
\r
284 fileShortcut.getFileShortcutId(), 1);
\r
288 public List<DLFileShortcut> getFileShortcuts(
\r
289 long groupId, long folderId, int status)
\r
290 throws SystemException {
\r
292 return dlFileShortcutPersistence.findByG_F_S(groupId, folderId, status);
\r
295 public int getFileShortcutsCount(long groupId, long folderId, int status)
\r
296 throws SystemException {
\r
298 return dlFileShortcutPersistence.countByG_F_S(
\r
299 groupId, folderId, status);
\r
302 public List<FileEntry> getNoAssetFileEntries() {
\r
306 public void moveFileEntry(FileEntry fileEntry)
\r
307 throws PortalException, SystemException {
\r
309 if (!isStagingGroup(fileEntry.getGroupId())) {
\r
310 dlSyncLocalService.updateSync(
\r
311 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
\r
312 fileEntry.getTitle(), fileEntry.getDescription(),
\r
313 DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
\r
317 public void moveFolder(Folder folder)
\r
318 throws PortalException, SystemException {
\r
320 if (!isStagingGroup(folder.getGroupId())) {
\r
321 dlSyncLocalService.updateSync(
\r
322 folder.getFolderId(), folder.getParentFolderId(), folder.getName(),
\r
323 folder.getDescription(), DLSyncConstants.EVENT_UPDATE, "-1");
\r
327 public AssetEntry updateAsset(
\r
328 long userId, FileEntry fileEntry, FileVersion fileVersion,
\r
330 throws PortalException, SystemException {
\r
332 long[] assetCategoryIds = new long[0];
\r
334 assetCategoryIds = assetCategoryLocalService.getCategoryIds(
\r
335 DLFileEntryConstants.getClassName(), assetClassPk);
\r
336 } catch (Exception e){
\r
337 // No Such Asset Categories
\r
340 String[] assetTagNames = new String[0];
\r
342 assetTagNames = assetTagLocalService.getTagNames(
\r
343 DLFileEntryConstants.getClassName(), assetClassPk);
\r
344 } catch (Exception e){
\r
345 // No Such Asset Tag Names
\r
348 AssetEntry assetEntry = assetEntryLocalService.getEntry(
\r
349 DLFileEntryConstants.getClassName(), assetClassPk);
\r
351 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
\r
352 assetEntry.getEntryId());
\r
354 long[] assetLinkIds = StringUtil.split(
\r
355 ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
\r
357 return updateAsset(
\r
358 userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
\r
362 public AssetEntry updateAsset(
\r
363 long userId, FileEntry fileEntry, FileVersion fileVersion,
\r
364 long[] assetCategoryIds, String[] assetTagNames,
\r
365 long[] assetLinkEntryIds)
\r
366 throws PortalException, SystemException {
\r
368 AssetEntry assetEntry = null;
\r
370 boolean visible = false;
\r
372 boolean addDraftAssetEntry = false;
\r
374 if (fileEntry instanceof LiferayFileEntry) {
\r
375 DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
\r
377 if (dlFileVersion.isApproved()) {
\r
381 String version = dlFileVersion.getVersion();
\r
383 if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
\r
384 addDraftAssetEntry = true;
\r
392 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
\r
394 if (addDraftAssetEntry) {
\r
395 assetEntry = assetEntryLocalService.updateEntry(
\r
396 userId, fileEntry.getGroupId(),
\r
397 DLFileEntryConstants.getClassName(),
\r
398 fileVersion.getFileVersionId(), fileEntry.getUuid(),
\r
399 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
\r
400 null, null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
\r
401 fileEntry.getDescription(), null, null, null, 0, 0, null,
\r
405 assetEntry = assetEntryLocalService.updateEntry(
\r
406 userId, fileEntry.getGroupId(),
\r
407 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
\r
408 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
\r
409 assetTagNames, visible, null, null, null, null,
\r
410 fileEntry.getMimeType(), fileEntry.getTitle(),
\r
411 fileEntry.getDescription(), null, null, null, 0, 0, null,
\r
414 List<DLFileShortcut> dlFileShortcuts =
\r
415 dlFileShortcutPersistence.findByToFileEntryId(
\r
416 fileEntry.getFileEntryId());
\r
418 for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
\r
419 assetEntryLocalService.updateEntry(
\r
420 userId, dlFileShortcut.getGroupId(),
\r
421 DLFileShortcut.class.getName(),
\r
422 dlFileShortcut.getFileShortcutId(),
\r
423 dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
\r
424 assetTagNames, true, null, null, null, null,
\r
425 fileEntry.getMimeType(), fileEntry.getTitle(),
\r
426 fileEntry.getDescription(), null, null, null, 0, 0, null,
\r
431 assetLinkLocalService.updateLinks(
\r
432 userId, assetEntry.getEntryId(), assetLinkEntryIds,
\r
433 AssetLinkConstants.TYPE_RELATED);
\r
438 public void updateFileEntry(
\r
439 long userId, FileEntry fileEntry, FileVersion fileVersion,
\r
441 throws PortalException, SystemException {
\r
443 boolean updateAsset = true;
\r
445 if (fileEntry instanceof LiferayFileEntry &&
\r
446 fileEntry.getVersion().equals(fileVersion.getVersion())) {
\r
448 updateAsset = false;
\r
452 updateAsset(userId, fileEntry, fileVersion, assetClassPk);
\r
455 registerDLProcessorCallback(fileEntry);
\r
458 public void updateFileEntry(
\r
459 long userId, FileEntry fileEntry, FileVersion fileVersion,
\r
460 ServiceContext serviceContext)
\r
461 throws PortalException, SystemException {
\r
464 userId, fileEntry, fileVersion,
\r
465 serviceContext.getAssetCategoryIds(),
\r
466 serviceContext.getAssetTagNames(),
\r
467 serviceContext.getAssetLinkEntryIds());
\r
469 registerDLProcessorCallback(fileEntry);
\r
472 public void updateFolder(Folder folder, ServiceContext serviceContext)
\r
473 throws PortalException, SystemException {
\r
475 if (!isStagingGroup(folder.getGroupId())) {
\r
476 dlSyncLocalService.updateSync(
\r
477 folder.getFolderId(), folder.getParentFolderId(),
\r
478 folder.getName(), folder.getDescription(),
\r
479 DLSyncConstants.EVENT_UPDATE, "-1");
\r
483 public void updateStatus(
\r
484 long userId, FileEntry fileEntry, FileVersion latestFileVersion,
\r
485 int status, Map<String, Serializable> workflowContext)
\r
486 throws PortalException, SystemException {
\r
488 if (status == WorkflowConstants.STATUS_APPROVED) {
\r
492 String latestFileVersionVersion = latestFileVersion.getVersion();
\r
494 if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
\r
495 if (!latestFileVersionVersion.equals(
\r
496 DLFileEntryConstants.VERSION_DEFAULT)) {
\r
498 AssetEntry draftAssetEntry = null;
\r
501 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
\r
503 draftAssetEntry = assetEntryLocalService.getEntry(
\r
504 DLFileEntryConstants.getClassName(),
\r
505 latestFileVersion.getPrimaryKey());
\r
507 long[] assetCategoryIds =
\r
508 draftAssetEntry.getCategoryIds();
\r
509 String[] assetTagNames = draftAssetEntry.getTagNames();
\r
511 List<AssetLink> assetLinks =
\r
512 assetLinkLocalService.getDirectLinks(
\r
513 draftAssetEntry.getEntryId(),
\r
514 AssetLinkConstants.TYPE_RELATED);
\r
516 long[] assetLinkEntryIds = StringUtil.split(
\r
518 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
\r
520 AssetEntry assetEntry =
\r
521 assetEntryLocalService.updateEntry(
\r
522 userId, fileEntry.getGroupId(),
\r
523 DLFileEntryConstants.getClassName(),
\r
524 fileEntry.getFileEntryId(), fileEntry.getUuid(),
\r
525 fileEntryTypeId, assetCategoryIds,
\r
526 assetTagNames, true, null, null, null, null,
\r
527 draftAssetEntry.getMimeType(),
\r
528 fileEntry.getTitle(),
\r
529 fileEntry.getDescription(), null, null, null, 0,
\r
532 assetLinkLocalService.updateLinks(
\r
533 userId, assetEntry.getEntryId(), assetLinkEntryIds,
\r
534 AssetLinkConstants.TYPE_RELATED);
\r
536 assetEntryLocalService.deleteEntry(
\r
537 draftAssetEntry.getEntryId());
\r
539 // MIGRATION MODIFICATION --> NoSuchEntryException
\r
540 catch (Exception e) {
\r
544 assetEntryLocalService.updateVisible(
\r
545 DLFileEntryConstants.getClassName(),
\r
546 fileEntry.getFileEntryId(), true);
\r
551 String event = (String)workflowContext.get("event");
\r
553 if (!isStagingGroup(fileEntry.getGroupId()) &&
\r
554 Validator.isNotNull(event)) {
\r
556 if (event.equals(DLSyncConstants.EVENT_ADD)) {
\r
557 dlSyncLocalService.addSync(
\r
558 fileEntry.getFileEntryId(), fileEntry.getUuid(),
\r
559 fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
\r
560 fileEntry.getFolderId(), fileEntry.getTitle(),
\r
561 fileEntry.getDescription(), DLSyncConstants.TYPE_FILE,
\r
562 fileEntry.getVersion());
\r
564 else if (event.equals(DLSyncConstants.EVENT_UPDATE)) {
\r
565 dlSyncLocalService.updateSync(
\r
566 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
\r
567 fileEntry.getTitle(), fileEntry.getDescription(),
\r
568 DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
\r
574 int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
\r
576 if (latestFileVersionVersion.equals(
\r
577 DLFileEntryConstants.VERSION_DEFAULT)) {
\r
579 activityType = DLActivityKeys.ADD_FILE_ENTRY;
\r
582 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
\r
584 extraDataJSONObject.put("title", fileEntry.getTitle());
\r
586 socialActivityLocalService.addUniqueActivity(
\r
587 latestFileVersion.getStatusByUserId(), fileEntry.getGroupId(),
\r
588 latestFileVersion.getCreateDate(),
\r
589 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
\r
590 activityType, extraDataJSONObject.toString(), 0);
\r
596 if (Validator.isNull(fileEntry.getVersion())) {
\r
597 assetEntryLocalService.updateVisible(
\r
598 DLFileEntryConstants.getClassName(),
\r
599 fileEntry.getFileEntryId(), false);
\r
604 protected long getFileEntryTypeId(FileEntry fileEntry) {
\r
605 if (fileEntry instanceof LiferayFileEntry) {
\r
606 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
\r
608 return dlFileEntry.getFileEntryTypeId();
\r
615 protected boolean isStagingGroup(long groupId) {
\r
617 Group group = groupLocalService.getGroup(groupId);
\r
619 return group.isStagingGroup();
\r
621 catch (Exception e) {
\r
626 protected void registerDLProcessorCallback(final FileEntry fileEntry) {
\r
627 TransactionCommitCallbackUtil.registerCallback(
\r
628 new Callable<Void>() {
\r
630 public Void call() throws Exception {
\r
631 DLProcessorRegistryUtil.trigger(fileEntry);
\r