FourDInterface Class - fourctv/JS44D GitHub Wiki

This is the base service class that implements most of the calls to 4D RESTApi.

It is an Injectable class, and it should be injected in the constructor of classes that need access to its functions.

The class provides the following static properties:

  • currentUser: the currently signed in user name
  • currentUserID: the currently signed in user ID (longint)
  • authentication: authentication response object, returned by the REST_Authenticate call
  • sessionKey: the current session token, that must be present in all requests sent to 4D
  • fourDUrl: the URL to the 4D Database backend web server; it defaults to http://localhost:8080 but can be modified by the main app to point to your 4D web server URL. That can be easily done by using code like this in your app.component.ts constructor or onInit method:
FourDInterface.fourDUrl = 'http://4D webserver IP:port';

The class also generates the following events that can be trapped by the main app:

  • userHasSignedIn: that event is dispatched whenever a user has successfully signed in, after a call to the signIn function; the event's payload is the signed in user name

The following functions are provided by this service class:


call4DRESTMethod Function

This is a wrapper function that can be used to make HTTP POST requests to the 4D RESTApi entry points. The function automatically adds the required Session Token and message hash to the POST payload.

Function takes 2 arguments:

  • fourdMethod: the 4D Method name to call via a 4DAction HTTP POST request
  • body: the POST request body to send to 4D; an object that will be converted into a POST form vars

The function returns an Observable, which the caller can subscribe to, to act upon 4D's response to the request.

Example:

  this.fourD.call4DRESTMethod('REST_LoadData', body)
    .subscribe(
        response => {
              ..... do whatever you need to do with the response, which is usually a JSON object
        },
        error => {
             console.log('error:' + error.text());
        });

signIn Function

This function will attempt to sign into a 4D Database backend, by sending a REST_Authenticate request. The response from 4D is handled by the function, which will populate the FourDInterface static variables above.

The function takes 2 arguments:

  • user: the user name
  • pwd: the user password

The function returns a Promise, that can be subscribed to to handle 4D's authentication response.

The function also dispatches the userHasSignedIn event, which the main app can trap and act upon.

Example:

   this.fourD.signIn(this.username, this.password)
       .then((authentication) => {
                console.log('authenticated:', authentication);
            })
       .catch((e) => {
                console.log(e);
            });

getRecordCount() Function

Use this function to retrieve the number of records from a 4D Database, that match a given QueryString. It sends a REST_CountRecords request to 4D, and returns the number of records found.

The function call takes up to 3 arguments, all optional, except for the first two:

  • tableName: the name of the 4D Table to apply the query, and filterOptions, to.
  • query: a Query String with the criteria for the desired record selection to retrieve
  • filterOptions: an additional Query String, to further reduce the resulting record selection; if set, the resulting record selection will have to meet both the query and the filterOptions criteria; if null, the default filterOptions will be used; if blank, then it'll override the default filterOptions

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record count is received. The Promise result is the number of records that match the given criteria.

get4DList Function

A function that will call 4D Database backend to obtain all items of a 4D Choice List.

The function takes only one argument:

  • listName: the 4D Choice List name to retrieve

Function returns a Promise, of a String Array, which user can subscribe to, in order to get the 4D Choice List contents.

The retrieved 4D Choice Lists contents is cached by this class, so any subsequent calls for the same listName will return immediately, without calling 4D. That reduces network traffic.

Example call:

   this.fourD.get4DList(this.listName)
       .then((values) => { this.listOptions = ['', ...values]; })
       .catch((reason) => { console.log('error', reason); });

update4DList Function

This function will send a request to 4D Database backend to update the items, contents, of a 4D Choice List.

The function takes 2 arguments:

  • listName: the 4D Choice List name to update
  • _listValues-: an array with all the 4D Choice List items value; items will replace the Choice List contents on 4D side

Function returns a Promise, which user can subscribe to act upon the request completion.

The 4D Choice List cache maintained by this class will be updated with the new Choice List contents.

Example call:

   this.fourD.update4DList(this.selectedListName, this.listItems);