How can I find the attachment id using the document reference or document id in the backend? I have currently implemented a workaround where I am iterating through all attachments, which is not ideal. However, I believe there should be a straightforward method for retrieving attachments based on the docRef.
AttachmentReference attachmentReference = AttachmentReference.fromDocRef(docRef);
List attachmentHeaderEntityList = attachmentHeaderJpaRepository.findAll();
Optional<AttachmentHeaderEntity> attachment = attachmentHeaderEntityList.stream().findFirst()
.filter(attachmentHeaderEntity -> attachmentHeaderEntity.getReferences().stream()
.anyMatch(attachmentReferenceEntity -> attachmentReferenceEntity.getReference().startsWith(attachmentReference.getReference().toString())));
HI @n.garryyev ,
So far we had no requirements for functionality like this so it does not exist yet. I would recommend not iterating over all attachments and search for references but rather find attachment references in a document. The API like this exists but is currently private therefore I would ask you to create a ticket and we will make it public for you.
This answer is valid for DS versions 33.0.0 - 36.0.0
Hi @tomas-thin-gale,
If you look at my code snippet that I shared, I first retrieve the attachmentReference , and then iterate through all the attachments to find a match using that reference. However, from what I understand, there might be a method available to directly retrieve the attachment using the attachmentReference . Could you please provide me with the code for that method, if it exists?
I have found a better solution than my previous one. I can extract the attachment id from the
documentSpec . However, I’m still wondering if there is any method available to retrieve the attachment using the attachmentReference .
DataServicesDocument dataServicesDocument = documentService.load(new DocumentReference(docRef))
.orElseThrow(() → new NotFoundException(DOCUMENT_NOT_FOUND_ERROR_KEY, String.format(“Document [%s] not found”, docRef)));
DocumentSpec documentSpec = documentConvertor.convertToDocumentSpec(dataServicesDocument);
Hi @n.garryyev,
DS has a bean AttachmentHandler which have a method List<String> collectAttachmentIDs(IDocument document). This method will look into the document and collect all attachments of this particular document.
private List<Optional<AttachmentHeader>> collectAllAttachmentHeaders(DocumentReference docRef) {
DataServicesDocument dataServicesDocument = documentService.load(docRef)
.orElseThrow(() -> new NotFoundException(DOCUMENT_NOT_FOUND_ERROR_KEY, String.format("Document [%s] not found", docRef)));
List<String> attachmentIDs = attachmentHandler.collectAttachmentIDs(dataServicesDocument.getKernelDocument());
return attachmentIDs.stream()
.map(attachmentHeaderService::load)
.toList();
}
Unfortunately the whole AttachmentHandler is internal therefore you need to create a ticket to make it public or implement logic to look up attachments from documents by yourself. I do not recommend using lower level API without paging on all attachments in the DB to find attachments of a document but rather to introspect a single document and load the attachments that it has
@tomas-thin-gale Great! This is exactly what I was looking for.
Thanks