Interface DocumentContentGetterInterface

The DocumentContentGetterInterface interface is used to get the content of an attached document knowing its properties (this cannot be used to get metadata documents).

The attached document content can be retrieved by an AttachedDocumentInfoInterface obtained by a call to InstanceMetadataInterface.getAttachedDocumentInfos. If the user knows the attached document id and its mime type, then the attached document can also be retrieved using this information and using DataSessionInterface.createAttachedDocumentInfo. attached document ids are string based.

Warning : any attached document with a URL will NOT be retrieved.

The DocumentContentGetterInterface interfaces are created through the DataSessionInterface.createDocumentContentGetter method.

/** 
* Sample to illustrate how to download an attached document.
*/
import { DataSessionInterface, DocumentContentGetterInterface, AttachedDocumentInfoInterface, DocumentContentGetterInterfaceSignal, tListenerCallback } from 'generated_files/documentation/appinfiniteapi';

// the DataSessionInterface has been created previously and is connected
let lDataSession : DataSessionInterface;
// user callback code called when the document data content is ready
let myCallback : tListenerCallback;

const lDocumentContentGetterInterface : DocumentContentGetterInterface = lDataSession.createDocumentContentGetter();
// get an AttachedDocumentInfoInterface from an IdCardGetterInterface
// ....
let lAttachedDocumentInfo : AttachedDocumentInfoInterface;

lDocumentContentGetterInterface.addEventListener(DocumentContentGetterInterfaceSignal.DocumentContentReady, myCallback);

// if no url, then we can retrieve data content
if (lAttachedDocumentInfo.documentUrl === '')
{
lDocumentContentGetterInterface.retrieveDocumentContent(lAttachedDocumentInfo);
}
// myCallback will be called when the data is available

or asynchronously :
/** 
* Sample to illustrate how to asynchronously download an attached document.
*/
import { DataSessionInterface, DocumentContentGetterInterface, AttachedDocumentInfoInterface, AsyncDocumentContentGetterResult, AsyncResultReason } from 'generated_files/documentation/appinfiniteapi';

// the DataSessionInterface has been created previously and is connected
let lDataSession : DataSessionInterface;

const lDocumentContentGetterInterface : DocumentContentGetterInterface = lDataSession.createDocumentContentGetter();
// get an AttachedDocumentInfoInterface from an IdCardGetterInterface
// ....
let lAttachedDocumentInfo : AttachedDocumentInfoInterface;

// the code to parse the document content
let handleDocumentContent : (pContent : string | Uint8Array) => void;

const getDocument = async () : Promise<void> =>
{
// if no url, then we can retrieve data content
if (lAttachedDocumentInfo.documentUrl === '')
{
const lResult : AsyncDocumentContentGetterResult = await lDocumentContentGetterInterface.asyncRetrieveDocumentContent(lAttachedDocumentInfo);
// it it successful ?
if (lResult.reason !== AsyncResultReason.ARR_Success)
{
console.log('some fancy error message');
return;
}
// was the document id correct ?
if (!lResult.value)
{
console.log('some other fancy error message');
return;
}
handleDocumentContent(lResult.value);
}
};

getDocument();

The list of signals the DocumentContentGetterInterface may trigger is available in the DocumentContentGetterInterfaceSignal enumeration.

The attached document retrieval procedure is triggered through the retrieveDocumentContent methods. The result is not available right away, but the event DocumentContentGetterInterfaceSignal.DocumentContentReady is triggered when the result of the DocumentContentGetterInterface is available. The result is available through the getDocumentContent function.

Warning : there may be cases when the getDocumentContent is not available such as when the DocumentContentGetterInterface is updating, i.e. when isRunning returns true, or when the DocumentContentGetterInterface has been cancelled, i.e. when isCancelled returns true.

A DocumentContentGetterInterface may be interrupted (cancelled) when the DocumentContentGetterInterface is updating and cancel is called. In such cases, the DocumentContentGetterInterfaceSignal.DocumentContentCancelled signal is fired, and shortly after, DocumentContentGetterInterfaceSignal.DocumentContentReady signal is fired, but getDocumentContent will return undefined. Just call retrieveDocumentContent with another (or the same) attached document properties to trigger a new retrieval.

If you call multiple times retrieveDocumentContent before receiving the DocumentContentGetterInterfaceSignal.DocumentContentReady, only one DocumentContentGetterInterfaceSignal.DocumentContentReady will be sent with the content of the last call to retrieveDocumentContent.

The result may be undefined (getDocumentContent) if no such document was found, this is not considered as an error.

The developer may trigger a download on the user browser by the following code :

/** 
* Sample to illustrate how to trigger a file dialog to save the content of an attached document.
*/
import { AttachedDocumentInfoInterface, DocumentContentGetterInterface } from 'generated_files/documentation/appinfiniteapi';

let getDocumentData : (pDocumentContentGetterInterface : DocumentContentGetterInterface) => boolean = undefined;

// pDocumentContentGetterInterface.retrieveDocumentContent(lAttachedDocumentInfo) has been called previously

getDocumentData = (pDocumentContentGetterInterface : DocumentContentGetterInterface) : boolean =>
{
// do nothing in case of error
if (pDocumentContentGetterInterface.getLastError() !== undefined)
{
return false;
}
const lAttachedDocumentInfo: AttachedDocumentInfoInterface | undefined = pDocumentContentGetterInterface.getAttachedDocumentInfo();
// document is set ?
if (!lAttachedDocumentInfo) {
return false;
}
const lDocumentContent: string | Uint8Array | undefined = pDocumentContentGetterInterface.getDocumentContent();
// document is ready ?
if (!lDocumentContent) {
return false;
}
// create a <a> markup for download
const downloadLink = document.createElement('a');
downloadLink.target = '_blank';
let lExtension: string;
switch (lAttachedDocumentInfo.documentMimeType) {
case 'application/json': lExtension = 'json'; break;
case 'application/xml': lExtension = 'xml'; break;
case 'application/x-qt-richtext': lExtension = 'html'; break;
case 'image/jpg': lExtension = 'jpg'; break;
case 'image/jpeg': lExtension = 'jpeg'; break;
case 'image/png': lExtension = 'png'; break;
case 'text/csv': lExtension = 'csv'; break;
case 'text/html': lExtension = 'html'; break;
case 'text/plain': lExtension = 'txt'; break;
case 'text/xml': lExtension = 'xml'; break;
// do nothing in case of "unknown" mime type
default: return false;
}
// nice default file name for download
downloadLink.download = lAttachedDocumentInfo.documentName + '.' + lExtension;
// convert downloaded data to a Blob
const blob = new Blob([(typeof lDocumentContent === 'string') ? lDocumentContent : <BlobPart> lDocumentContent.buffer], { type: lAttachedDocumentInfo.documentMimeType });
// create an object URL from the Blob
const URL = window.URL;
const downloadUrl = URL.createObjectURL(blob);
// set object URL as the anchors href
downloadLink.href = downloadUrl;
// append the anchor to document body
document.body.appendChild(downloadLink);
// fire a click event on the anchor
downloadLink.click();
// cleanup: remove element and revoke object URL
downloadLink.remove();
URL.revokeObjectURL(downloadUrl);
return true;
};

// lDocumentContentGetterInterface has been created previously and
// retrieveDocumentContent has been called
let lDocumentContentGetterInterface : DocumentContentGetterInterface;

getDocumentData(lDocumentContentGetterInterface);

Please refer to the AttachedDocumentInfoInterface for additional information.
Data Retrievers

interface DocumentContentGetterInterface {
    addEventListener(pType, pListener, pObject): string;
    addEventListener(pType, pListener): string;
    areSignalsBlocked(): boolean;
    asyncRetrieveDocumentContent(pAttachedDocumentInfo): Promise<AsyncDocumentContentGetterResult>;
    asyncRetrieveDocumentContent(pDocumentId, pMimeType?): Promise<AsyncDocumentContentGetterResult>;
    blockSignals(pBlock): void;
    cancel(): boolean;
    dispose(): void;
    getAttachedDocumentInfo(): AttachedDocumentInfoInterface;
    getDocumentContent(): string | Uint8Array;
    getInfiniteObjectType(): InfiniteObjectType;
    getLastError(): InfiniteError;
    getLastRequestId(): string;
    hasEventListener(pType, pListener): boolean;
    hasEventListenerById(pId): boolean;
    isCancelled(): boolean;
    isDisposed(): boolean;
    isRunning(): boolean;
    removeAllEventListeners(): boolean;
    removeEventListener(pType, pListener, pObject): boolean;
    removeEventListener(pType, pListener): boolean;
    removeEventListenerById(pId): boolean;
    retrieveDocumentContent(pAttachedDocumentInfo): boolean;
    retrieveDocumentContent(pDocumentId, pMimeType?): boolean;
}

Hierarchy (view full)

Methods

  • Adds a listener to an event type.

    When an event of the type pType fires, the callback pListener will be called. This function returns a unique string id that may be used in removeEventListenerById to allow simple listener removal. It is possible to add an object that will be included in the callback to avoid creating too many closures. Calling twice addEventListener with the same parameters results in the second call to be ignored, only unique pairs callback / object are allowed, in order to avoid calling multiple times the same thing.

    Parameters

    • pType: string
      in
      The type of the event pListener will be called upon.
    • pListener: tListenerCallback
      in
      The listener function that fires when the given event type occurs.
    • pObject: Object
      in
      The optional object the callback will be called with when the given event fires.

    Returns string

    The id of the inserted callback (actually an UUID).

  • Adds a listener to an event type.

    When an event of the type pType fires, the callback pListener will be called. This function returns a unique string id that may be used in removeEventListenerById to allow simple listener removal.

    Parameters

    • pType: string
      in
      The type of the event pListener will be called upon.
    • pListener: tListenerCallback
      in
      The listener function that fires when the given event type occurs.

    Returns string

    The id of the inserted callback (actually an UUID).

  • Tells if signals sent by the object are blocked or not.

    If signals are blocked, no signal will be emitted nor buffered, such signal will be lost.

    Returns boolean

    true if signals are blocked.

  • Asynchronously gets the content of the specified attached document.

    The attached document must not have a URL, any AttachedDocumentInfoInterface with a non empty documentUrl will return a promise with a bad input result.

    The mime type sets the return type of the promise content, string if the attached document is a text one, Uint8Array if binary.

    The AttachedDocumentInfoInterface is retrieved when using a InstanceMetadataInterface.getAttachedDocumentInfos.

    Returns a promise.

    The result may be undefined (getDocumentContent, AsyncDocumentContentGetterResult.value) if no such document was found, this is not an error, and AsyncDocumentContentGetterResult.reason is equal to AsyncResultReason.ARR_Success.

    Please note that in case of multiple promises running at the same time on the same DocumentContentGetterInterface, the first promises will be signalled as cancelled, the last as ok, but all calls to getDocumentContent after awaiting will return the same value.

    Parameters

    Returns Promise<AsyncDocumentContentGetterResult>

    A promise. The promise is resolved with the reason (success, cancelled, disposed, bad input). In case of success, the promise contains the document content.

  • Asynchronously gets the content of the specified attached document by its id and mime type.

    The mime type sets the return type of the promise, string if the attached document is a text one, Uint8Array if binary.

    Returns a promise.

    The result may be undefined (getDocumentContent, AsyncDocumentContentGetterResult.value) if no such document was found, this is not an error, and AsyncDocumentContentGetterResult.reason is equal to AsyncResultReason.ARR_Success.

    Please note that in case of multiple promises running at the same time on the same DocumentContentGetterInterface, the first promises will be signalled as cancelled, the last as ok, but all calls to getDocumentContent after awaiting will return the same value.

    Parameters

    • pDocumentId: string
      in
      The id of the attached document to get.
    • Optional pMimeType: string
      in
      The optional mime type of the attached document to get.

    Returns Promise<AsyncDocumentContentGetterResult>

    A promise. The promise is resolved with the reason (success, cancelled, disposed, bad input). In case of success, the promise contains the document content.

  • Blocks / Unblocks all signals sent by the object.

    If signals are blocked, no signal will be emitted nor buffered, such signal will be lost.

    Parameters

    • pBlock: boolean
      in
      If set to true, all further signals will be silently discarded.

    Returns void

  • Gets the content of the requested attached document.

    The attached document content is returned if the DocumentContentGetterInterface has finished computing. Use addEventListener on the event DocumentContentGetterInterfaceSignal.DocumentContentReady to know when the DocumentContentGetterInterface is ready.

    DO NOT modify this data in place, this results in undefined behavior.

    The type of the data returned by the function depends on the mime type of the attached document.

    Returns string | Uint8Array

    const
    The data of the attached document, in string or binary data given its mime-type, or undefined if the DocumentContentGetterInterface is computing or if the DocumentContentGetterInterface is in error or cancelled.
  • Gets the last error returned by the update of the DocumentContentGetterInterface.

    Returns InfiniteError

    The last error (if any, or undefined if no error occurred).

  • Tells if the EventDispatcher has such a callback registered for the given event type.

    Parameters

    • pType: string
      in
      The type of the event to test.
    • pListener: tListenerCallback
      in
      The listener function that gets tested.

    Returns boolean

    true if such a listener is installed for the given type of event.

  • Tells if the EventDispatcher has such a callback registered for the given callback id.

    Parameters

    • pId: string
      in
      The id of the callback to test.

    Returns boolean

    true if such a listener is installed for the given callback id.

  • Tells if the DocumentContentGetterInterface has been cancelled.

    This is generally the case after calling cancel when the DocumentContentGetterInterface is retrieving data.

    Returns boolean

    true if the DocumentContentGetterInterface is cancelled.

  • Tells if the DocumentContentGetterInterface is updating.

    This is the case after calling retrieveDocumentContent.

    Returns boolean

    true if the DocumentContentGetterInterface is updating.

  • Removes a listener from an event type.

    If no such listener is found, then the function returns false and does nothing. You must use the exact parameters that were used in addEventListener to actually remove the listener.

    Parameters

    • pType: string
      in
      The type of the listener that gets removed.
    • pListener: tListenerCallback

      The listener function that gets removed.

    • pObject: Object

      The listener object that was used when addEventListener was called.

    Returns boolean

    true if the callback was removed else false.

  • Removes a listener from an event type.

    If no such listener is found, then the function returns false and does nothing. You must use the exact parameters that were used in addEventListener to actually remove the listener.

    Parameters

    • pType: string
      in
      The type of the listener that gets removed.
    • pListener: tListenerCallback

      The listener function that gets removed.

    Returns boolean

    true if the callback was removed else false.

  • Removes a listener by its id.

    If no such listener is found, then the function returns false and does nothing. You must use the return value of addEventListener to actually remove the listener.

    Parameters

    • pId: string
      in
      The id returned by the call to addEventListener that you want to remove.

    Returns boolean

    true if the callback was removed else false.

  • Gets the content of the specified attached document.

    The attached document must not have a URL, any AttachedDocumentInfoInterface with a non empty documentUrl will return false.

    The mime type sets the return type of getDocumentContent, string if the attached document is a text one, Uint8Array if binary.

    The AttachedDocumentInfoInterface is retrieved when using a InstanceMetadataInterface.getAttachedDocumentInfos.
    The event DocumentContentGetterInterfaceSignal.DocumentContentReady is fired when the attached document content result is ready.

    Returns true if the attached document procedure retrieval is started. If not, just call getLastError to get the reason why the procedure failed.

    The result may be undefined (getDocumentContent) if no such document was found.

    Parameters

    Returns boolean

    true if the retrieval procedure has begun.

  • Gets the content of the specified attached document by its id and mime type.

    The mime type sets the return type of getDocumentContent, string if the attached document is a text one, Uint8Array if binary.

    The event DocumentContentGetterInterfaceSignal.DocumentContentReady is fired when the attached document content result is ready.

    Returns true if the document procedure retrieval is started. If not, just call getLastError to get the reason why the procedure failed.

    The result may be undefined (getDocumentContent) if no such document was found.

    Parameters

    • pDocumentId: string
      in
      The id of the attached document to get.
    • Optional pMimeType: string
      in
      The optional mime type of the attached document to get.

    Returns boolean

    true if the retrieval procedure has begun.