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