About the data4life API

Performing queries and actions

The data4life API follows the CRUD pattern (create, read, update, and delete).

Read operations are divided into a simple fetch and a full download of the resource.

  • Fetching a resource returns only the resource itself without attachments.

  • Downloading a resource returns the resource with all its attachments.

Creating a new record

To create a new record, the API provides the createRecord(resource, listener) method with the resource and the result listener as arguments. To build the resource that shall be created, use the DocumentReferenceBuilder class. For FHIR Attachment.data, the SDK supports these file formats for attachments:

  • JPEG, including JPEG/JFIF

  • PNG – Portable Network Graphics

  • TIFF – Tagged Image File Format

  • DCM – Digital Imaging and Communications in Medicine

  • PDF – Portable Document Format

The SDK checks the following when users create resources:

  • File format: When users provide an unsupported file format for attachment content, the SDK throws an DataRestrictionException.UnsupportedFileType exception.

  • File size: When the Attachment.data is larger than 10 MB, the SDK throws a DataRestrictionException.MaxDataSizeViolation exception.

public <T extends DomainResource> void createRecord(T resource, ResultListener<Record<T>> listener)

The following example shows how you create the DocumentReference resource with the DocumentReferenceBuilder class.

DocumentReference documentReference = DocumentReferenceBuilder.buildWith(
        documentTitle,
        creationDate,
        DocumentReferenceStatus.CURRENT,
        attachments,
        documentType,
        author)

sdk.createRecord(documentReference, new ResultListener<Record<DocumentReference>>() {
    @Override
    public void onSuccess(Record<DocumentReference> record) {
        // Created record
    }
    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Creating multiple new records

To create multiple records at once, use the createRecords(resources, listener) method. The resources have the DomainResource type.

The onSuccess(result) method of the callback is called even if the resources are only partially created. The CreateResult object contains the successful and the failed operations with the created records and the records that couldn’t get created. The object also includes the individual error message for each failed request.

public <T extends DomainResource> void createRecords(List<T> resources, ResultListener<CreateResult<T>> listener)
List<DocumentReference> resources = asList(documentReference);
sdk.createRecords(resources, new ResultListener<CreateResult<DocumentReference>>() {
    @Override
    public void onSuccess(CreateResult<DocumentReference> result) {
        // The result of the operation
        // CreateResult contains a list with the successfully created records
        // and with the failed to create ones
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Fetching a record based on its ID

The fetchRecord(recordId, listener) method fetches a record where the ID of the record is passed. The record is returned in the result listener.

public <T extends DomainResource> void fetchRecord(String recordId, ResultListener<Record<T>> listener)
sdk.fetchRecord("recordId", new ResultListener<Record<DocumentReference>>() {
    @Override
    public void onSuccess(Record<DocumentReference> record) {
        // Fetched record
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Fetching multiple records based on their IDs

To fetch multiple records for the given record IDs, use the fetchRecords(recordIdList, listener) method.

public <T extends DomainResource> void fetchRecords(List<String> recordIdList, ResultListener<FetchResult<T>> listener)
sdk.fetchRecords(asList("recordId1", "recordId2"), new ResultListener<FetchResult<DomainResource>>() {
    @Override
    public void onSuccess(FetchResult<DomainResource> fetchResult) {
        // Fetched records
    }

    @Override
    public void onError(D4LException exception) {
        // Add exception handling
    }
});

Fetching records

To fetch records, use the the fetchRecords method. For example, when a client has no data and initially fetches records after a new login. The method lets you specify the following:

  • Fetch records by type

  • Order records by date

  • Paginate loaded records by providing the pageSize and an offset.

public <T extends DomainResource> void fetchRecords(Class<T> resourceType, LocalDate startDate, LocalDate endDate, Integer pageSize, Integer offset, ResultListener<List<Record<T>>> listener)
sdk.fetchRecords(DocumentReference.class, fromDate, toDate, 20, offset, new ResultListener<List<Record<DocumentReference>>>() {
    @Override
    public void onSuccess(List<Record<DocumentReference>> records) {
        // Fetched records
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Downloading a record based on its ID

To download one record for the given ID with all the contained references, use the downloadRecords method with the recordId parameter.

public <T extends DomainResource> void downloadRecord(String recordId, ResultListener<Record<T>> listener)
sdk.downloadRecord("recordId", new ResultListener<Record<DocumentReference>>() {
    @Override
    public void onSuccess(Record<DocumentReference> record) {
        // Downloaded record with all contained references
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Downloading multiple records based on their IDs

To download records for the given IDs with all their contained references, use the downloadRecords method with record IDs as parameters.

public <T extends DomainResource> void downloadRecords(List<String> recordIds, ResultListener<DownloadResult<T>> listener)
sdk.downloadRecords(asList("recordId1", "recordId2"), new ResultListener<DownloadResult<DocumentReference>>() {
    @Override
    public void onSuccess(DownloadResult<DocumentReference> records) {
        // Downloaded records
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Updating a record

To update the given record with the given updated document, use the updateRecord method.

public <T extends DomainResource> void updateRecord(T resource, ResultListener<Record<T>> listener)
DocumentReference updatedDocument = ...;

sdk.updateRecord(updatedDocument, new ResultListener<Record<DocumentReference>>() {
    @Override
    public void onSuccess(Record<DocumentReference> record) {
        // Updated record
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Updating multiple records

To update records with updated documents, use the updateRecords method.

public <T extends DomainResource> void updateRecords(List<T> resources, ResultListener<UpdateResult<T>> listener)
List<DocumentReference> resources = asList(updatedDocumentRef1, updatedDocumentRef2);
// Update records in the records list
sdk.updateRecords(resources, new ResultListener<UpdateResult<DocumentReference>>() {
    @Override
    public void onSuccess(UpdateResult<DocumentReference> result) {
        // Operation result with successfully updated and failed to update record list
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Deleting a record based on its ID

To delete a record with its given record ID, use the deleteRecord method.

public void deleteRecord(String recordId, Callback listener)
sdk.deleteRecord("recordId", new Callback() {
    @Override
    public void onSuccess() {
        // Record deleted
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Deleting multiple records based on their IDs

To delete the records with their given IDs, use the deleteRecords method.

public <T extends DomainResource> void deleteRecords(List<String> recordIds, ResultListener<DeleteResult<T>> listener)
sdk.deleteRecords(asList("recordId1", "recordId2"), new ResultListener<DeleteResult<DomainResource>>() {
    @Override
    public void onSuccess(DeleteResult<DomainResource> result) {
        // Result of the operation is split in two lists in the DeleteResult
        // Successfully deleted record ids are accessible by using `getSuccessfulDeletes()`
        // Failed to delete record IDs are accessible by using `getFailedDeletes()`
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Downloading an attachment based on its ID

To download a single attachment from a record by the given attachment ID, use the downloadAttachment method.

public void downloadAttachment(String recordId, String attachmentId, ResultListener<Attachment> listener)
sdk.downloadAttachment(recordId, attachmentId, new ResultListener<Attachment>() {
    @Override
    public void onSuccess(Attachment attachment) {
        // Attachment downloaded
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Downloading multiple attachments based on their IDs

To download specific attachments from a record by the given attachment IDs, use the downloadAttachments method.

public void downloadAttachments(String recordId, List<String> attachmentIds, ResultListener<List<Attachment>> listener)
client.downloadAttachments(recordId, attachmentIdsList, new ResultListener<List<Attachment>>() {
    @Override
    public void onSuccess(List<Attachment> attachments) {
        // Attachment downloaded
    }

    @Override
    public void onError(D4LException exception) {

    }
});

Checking if a user is logged in

To check if a user is currently logged in, use the isUserLoggedIn method. .

public boolean isUserLoggedIn(ResultListener<Boolean> listener)
sdk.isUserLoggedIn(new ResultListener<Boolean>() {
    @Override
    public void onSuccess(Boolean isLoggedIn) {
        // Login state is reflected in isLoggedIn
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Counting stored records

To count the stored records per record type, use the countRecords method. The results are returned in the onSuccess(Integer count) method of the result listener.

public <T extends DomainResource> void countRecords(Class<T> clazz, ResultListener<Integer> listener)
sdk.countRecords(DocumentReference.class, new ResultListener<Integer>() {
    @Override
    public void onSuccess(Integer count) {
        // The count for the given class type
    }

    @Override
    public void onError(D4LException exception) {
        // Exception
    }
});

Logging out a user

To log out the currently authenticated user, the logout(listener) is used where either the onSuccess method or the onError(D4LException) method is called.

public void logout(Callback listener)
sdk.logout(new Callback() {
    @Override
    public void onSuccess() {
        // User was logged out
    }

    @Override
    public void onError(D4LException exception) {
        // Failed to log out user
    }
});

With your consent, we will analyze your behaviour on the website to improve the website for all users. If you consent to the transmission of analysis data, click “Accept”. You can use the website without giving consent to analyze your behaviour. For further information, visit the privacy policy.