@@ -295,6 +295,7 @@ object SearchHelper {
295
295
beans.asScala
296
296
// Filter out restricted attachments if the user does not have permissions to view them
297
297
.filter(a => ! a.isRestricted || hasRestrictedAttachmentPrivileges)
298
+ .map(sanitiseAttachmentBean)
298
299
.map(att => {
299
300
val broken = recurseBrokenAttachmentCheck(itemKey, att.getUuid)
300
301
def ifNotBroken [T ](f : () => Option [T ]) = if (! broken) f() else None
@@ -355,21 +356,33 @@ object SearchHelper {
355
356
}
356
357
}
357
358
359
+ /**
360
+ * Find out the latest version of the Item which a Custom Attachment points to.
361
+ *
362
+ * @param version Version of a linked Item. It is either 0 or 1 where 0 means using the latest version
363
+ * and 1 means always using version 1.
364
+ * @param uuid UUID of the linked Item.
365
+ */
366
+ def getLatestVersionForCustomAttachment (version : Int , uuid : String ): Int = {
367
+ version match {
368
+ // If version of is 0, find the real latest version of this Item.
369
+ case 0 => LegacyGuice .itemService.getLatestVersion(uuid)
370
+ case realVersion => realVersion
371
+ }
372
+ }
373
+
358
374
/**
359
375
* Determines if a given customAttachment is invalid. Required as these attachments can be recursive.
360
376
* @param customAttachment The attachment to check.
361
377
* @return If true, this attachment is broken.
362
378
*/
363
379
def getBrokenAttachmentStatusForResourceAttachment (
364
380
customAttachment : CustomAttachment ): Boolean = {
365
- val uuid = customAttachment.getData(" uuid" ).asInstanceOf [String ]
366
- // If version of the linked Item is 0, find the latest version of this Item.
367
- val version = customAttachment.getData(" version" ).asInstanceOf [Int ] match {
368
- case 0 => LegacyGuice .itemService.getLatestVersion(uuid)
369
- case realVersion => realVersion
370
- }
381
+ val uuid = customAttachment.getData(" uuid" ).asInstanceOf [String ]
382
+ val version = customAttachment.getData(" version" ).asInstanceOf [Int ]
383
+
384
+ val key = new ItemId (uuid, getLatestVersionForCustomAttachment(version, uuid))
371
385
372
- val key = new ItemId (uuid, version)
373
386
if (customAttachment.getType != " resource" ) {
374
387
return false ;
375
388
}
@@ -488,4 +501,23 @@ object SearchHelper {
488
501
*/
489
502
def getAttachmentDescription (itemKey : ItemKey , attachmentUuid : String ): Option [String ] =
490
503
Option (LegacyGuice .itemService.getAttachmentForUuid(itemKey, attachmentUuid).getDescription)
504
+
505
+ /**
506
+ * When an AttachmentBean is converted to SearchResultAttachment, it may require some extra sanitising
507
+ * to complete the conversion. The sanitising work includes tasks listed below.
508
+ *
509
+ * 1. Help ResourceAttachmentBean check the version of its linked resource.
510
+ *
511
+ * @param att An AttachmentBean to be sanitised.
512
+ */
513
+ def sanitiseAttachmentBean (att : AttachmentBean ): AttachmentBean = {
514
+ att match {
515
+ case bean : ResourceAttachmentBean =>
516
+ val latestVersion =
517
+ getLatestVersionForCustomAttachment(bean.getItemVersion, bean.getItemUuid)
518
+ bean.setItemVersion(latestVersion)
519
+ case _ =>
520
+ }
521
+ att
522
+ }
491
523
}
0 commit comments