General approach to find and get documents

Hi.

I want to query for documents, get them, and do application logic. I wonder about the best approach to do so. I want to use dataservices public API only.

As far as I understood, I need the Spring Bean of DocumentQueryService and DocumentLoaderService and so something like:

{code}
@Autowired
private DocumentQueryService dqs;

@Autowired
private DocumentLoaderService dls;

public void bla(){
	String documentModelName = "Person";

	QueryResult qr = dqs.query(documentModelName, null, null, null);
	List<DocumentReference> drList = qr.getEntries();
	DocumentReference firstRef = drList.get(0);

	DataServicesDocument dsd = dls.getDocumentLoader(documentModelName).loadDocument(firstRef);

}

{code}

Questions:

  • In 2022.02, there is no DocumentLoaderService . Which code is suitable to replace the 2022.06 version?
  • Is there a simpler solution?

Hi @stefan-grey-canyon,

since 34.0.0 you can use the following code:
loading documents one by one:

		List<DataServicesDocument> documents = queryResult.getEntries()
			.stream()
			.map(docRef -> iDocumentReadRepository.getByDocumentReference(docRef))
			.filter(Optional::isPresent)
			.map(Optional::get)
			.collect(Collectors.toList());

you can change the iDocumentReadRepository for call to the DocumentLoaderService for additional permission checks. We, however, do not recommend doing this because it might break the paging. Security checks should have been injected in the filters before the query was executed using repositoryPolicies from authorizationDefinition file. If you are fine with broken paging, you still need to handle exceptions that loaders throw and in that case I would rather ask you to construct LIST_DOCUMENTS requests and call the json-rpc server directly.

If you do not want to use loaders you can also use this construct:


List<DataServicesDocument> documentsInBatch = iDocumentReadRepository.findDocumentsByDocRefs(queryResult.getEntries());

In versions 34.0.0- the DocumentLoaderFactory is a name for DocumentLoaderService