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