@@ -278,6 +278,7 @@ object SearchHelper {
278
278
beans.asScala
279
279
// Filter out restricted attachments if the user does not have permissions to view them
280
280
.filter(a => ! a.isRestricted || hasRestrictedAttachmentPrivileges)
281
+ .map(sanitiseAttachmentBean)
281
282
.map(att => {
282
283
val broken =
283
284
recurseBrokenAttachmentCheck(
@@ -317,21 +318,33 @@ object SearchHelper {
317
318
}
318
319
}
319
320
321
+ /**
322
+ * Find out the latest version of the Item which a Custom Attachment points to.
323
+ *
324
+ * @param version Version of a linked Item. It is either 0 or 1 where 0 means using the latest version
325
+ * and 1 means always using version 1.
326
+ * @param uuid UUID of the linked Item.
327
+ */
328
+ def getLatestVersionForCustomAttachment (version : Int , uuid : String ): Int = {
329
+ version match {
330
+ // If version of is 0, find the real latest version of this Item.
331
+ case 0 => LegacyGuice .itemService.getLatestVersion(uuid)
332
+ case realVersion => realVersion
333
+ }
334
+ }
335
+
320
336
/**
321
337
* Determines if a given customAttachment is invalid. Required as these attachments can be recursive.
322
338
* @param customAttachment The attachment to check.
323
339
* @return If true, this attachment is broken.
324
340
*/
325
341
def getBrokenAttachmentStatusForResourceAttachment (
326
342
customAttachment : CustomAttachment ): Boolean = {
327
- val uuid = customAttachment.getData(" uuid" ).asInstanceOf [String ]
328
- // If version of the linked Item is 0, find the latest version of this Item.
329
- val version = customAttachment.getData(" version" ).asInstanceOf [Int ] match {
330
- case 0 => LegacyGuice .itemService.getLatestVersion(uuid)
331
- case realVersion => realVersion
332
- }
343
+ val uuid = customAttachment.getData(" uuid" ).asInstanceOf [String ]
344
+ val version = customAttachment.getData(" version" ).asInstanceOf [Int ]
345
+
346
+ val key = new ItemId (uuid, getLatestVersionForCustomAttachment(version, uuid))
333
347
334
- val key = new ItemId (uuid, version)
335
348
if (customAttachment.getType != " resource" ) {
336
349
return false ;
337
350
}
@@ -438,4 +451,23 @@ object SearchHelper {
438
451
*/
439
452
def isLatestVersion (itemID : ItemIdKey ): Boolean =
440
453
itemID.getVersion == LegacyGuice .itemService.getLatestVersion(itemID.getUuid)
454
+
455
+ /**
456
+ * When an AttachmentBean is converted to SearchResultAttachment, it may require some extra sanitising
457
+ * to complete the conversion. The sanitising work includes tasks listed below.
458
+ *
459
+ * 1. Help ResourceAttachmentBean check the version of its linked resource.
460
+ *
461
+ * @param att An AttachmentBean to be sanitised.
462
+ */
463
+ def sanitiseAttachmentBean (att : AttachmentBean ): AttachmentBean = {
464
+ att match {
465
+ case bean : ResourceAttachmentBean =>
466
+ val latestVersion =
467
+ getLatestVersionForCustomAttachment(bean.getItemVersion, bean.getItemUuid)
468
+ bean.setItemVersion(latestVersion)
469
+ case _ =>
470
+ }
471
+ att
472
+ }
441
473
}
0 commit comments