Kony Fabric console User Guide: Object Services > Use Cases

Object Metadata for Controlling Client-side Logic

Object services from Kony V8 SP4 allow app developers to define metadata per field of the object in the Fabric server. This metadata of the Object allows app developers to control client-side logic by using the Data Pre and Post Processors for Models functionality which helps to access the metadata dynamically at runtime and has custom JS code written to transform the data based on the metadata. This allows app developers to modify business logic at the client app and control which fields get transformed and how.

The feature is available with object models generated from the Object services in Kony Visualizer and works for Kony Reference Architecture as well as Kony free form project types.

You can use data pre and post processors functionality on client apps as standalone or in conjunction with pre and post processors on Object services on server apps to achieve a variety of business requirements. For example, you can write your custom logic for fields in models to transform data in client before sending to back end and vice versa such as data encryption, decryption, currency or temperature conversion, prepend or append characters such as salutations (Mr. or Dr.) or currency symbol ($ or €) and so on.

Use Cases

Pre-requisites

Steps to enable Client-side Logic

  1. Define metadata for fields in Objects for which transformation is required.
  2. Generate models on the client app through Visualizer for the object services.
  3. Register Pre and Post processors for the models that require transformation.
  4. Write custom logic to process/transform data in the pre/post processors for the models.

Workflow of Data Pre and Post Processors on Client for Object Services

API signatures for Data Pre and Post Processors for Client Objects

For Visualizer (JavaScript)

Registering Processors

Registering Data Pre and Post Processors for Models in Object Services

 <model>.registerProcessors

Models expose the registerProcessors API which allows registration of processors for the model object. This step should be performed at least once for each model.

Signature

 <model>.registerProcessors(options, successCallback, failureCallback);

Parameters

Parameter Type Description Required
successCallback Function The function is invoked on successful registration. Yes
failureCallback Function The function is invoked on failure to register. Yes
options JSON See "Register Processor Options" for supported options. Yes

Register Processor Options

Parameter Type Description Required
preProcessor Function A callback function that is invoked before saving field details into the model instance. See "Pre/Post Processor Definition" syntax and details. No
postProcessor Function A callback function that is invoked before returning a value from the model instance. See "Pre/Post Processor Definition" syntax and details. No
getFromServer Boolean If the getFromServer is set to true, refreshes object's metadata from Fabric server. By default, it is set to false and object properties meta-data is fetched from the client device cache.

No

Return Type

void

Pre/Post Processor Definition

This section details the definition of Data pre and post-processor callbacks.

Processors should be valid function reference. A processor should accept two arguments - value and context.

Creating an Object service with meta-data and enabling Data Pre and Post processors on a Client app

You need to have an Object service with required fields enabled with meta-data in Kony Fabric. The following procedure explains you how to configure meta-data for fields in object services and enable Data pre and post processors based on meta-data for a client app.

  1. Go to Kony Visualizer Enterprise.
  2. Create a project based on Kony Architecture Reference.
    • For example, from the Project menu, navigate to New Project > Create Custom App.
  1. Connect to your Kony Fabric account.
  2. Configure an object and add fields:
    1. Create an object service.
    2. Create an object.
    3. Navigate to the object > Fields.
    4. Click Add to create a field. Specify the field name and data type and primary key, as required. Add fields in the object.
    5. Click SAVE to save the field.

      Important: The View Details button is active only after you save the field details.

    6. Click the View Details button under the DETAILS column. The field details page displays the additional columns including unique, nullable, max length, autogenerated, creatable, updatable, precision, description, and metadata.
    7. In the Metadata column, click ADD.

      The Metadata dialog appears.

      Note: You must enable sensitive fields with meta-data in object services to use the Data Pre and Post Processors for Models functionality.

    8. Specify name and value meta-data for the selected field.
    9. Click ADD.
    10. Publish Fabric app.
  3. Generate the object model as follows:
    1. Navigate to the project in Visualizer.
    2. Navigate to the Project menu, and right-click Kony Fabric app, and select Generate Object Model. Now, the object service is enabled for Data Pre and Post Processors for Models registration. You can now write your logic to be applied for selected fields in the data model.

  4.  Create a JS module and write your custom code as follows:
    1. Navigate to the Project menu, and right-click Kony Fabric app, and select New JS module. A new JS module is created, and the Code editor become active.

    2. In the Code editor, specify your custom JS code as follows:
      1. Specify your code for Data Pre and Post Processors.
         
        function preProcessor(value, context) {
            value = encrypt(value); //transformation logic to be applied before sending over the network.
            return value;
        }
         
        function postProcessor(value, context) {
            value = decrypt(value); //transformation logic to be applied before returning value to user.
            return value;
        } 
        
      2. Register fields in objects with theData Pre and Post Processors for the client app.

        After you register callbacks as pre and post processors for objects, the specified callbacks are automatically invoked for every CRUD operations on that object, as shown in the following sample code snippet.

         
         var objModelDef= kony.mvc.MDAApplication.getSharedInstance().modelStore.getModelDefinition("ObjectName");
         
        function registrationSuccess() {
            alert("Registration Success");
        }
         
        function registrationFailure(err) {
          alert("Registration Failure" + JSON.stringify(err));
        }
         
        var options = {'preProcessor' : preProcessor, "postProcessor" : postProcessor };
        objModelDef.registerProcessors(options, registrationSuccess, registrationFailure);
  5. Save and build the project.

    So now you have enabled security to the sensitive fields in objects in your client app.

 

Sample Code to Encrypt/Decrypt Meta-data of the Password field for Client App

The following sample code demonstrates how to encrypt and decrypt a value for the field password over the network call on a client app.

You have created an object service as follows:

Write the logic/code for post data processor for that value password.

Registering Processors

 
function preProcessor(value, context) {
   //transformation logic to be applied before sending over the network.
    return value;
}
 
function postProcessor(value, context) {
    //transformation logic to be applied before returning value to user.
    return value;
}
  1. PreProcessor performs encryption for fields for which metadata is defined. In this implementation, it performs encryption for the password before sending data over network.

    Below snippet shows implementation of PreProcessor for the Employee.

     var AESKey = "ABCDEFGHIJKLMNOP";
    var algo = "aes";
    var prptobj = {padding:"pkcs5", mode:"ecb"};
     
    /**
    ** returns Base64 encoded cipherText using AES 128 algorithm.
    ** AES is symmetric algorithm
    */
    function encrypt(plainText) { //pass key in wordArray format. var key = CryptoJS.enc.Utf8.parse(AESKey); var myEncryptedText = kony.crypto.encrypt(algo, key, plainText, prptobj); return (JSON.parse(myEncryptedText).ct); } function preProcessor(value, context) { if(context.metadata.enableEncryption === "true" && context.metadata.encryptionAlgorithm === "AES") { value = encrypt(value); } return value; }
  2. PostProcessor performs decryption for fields for which metadata is defined. In this implementation, it performs decryption for the password before sending data over network.

    Below snippet shows implementation of PostProcessor for the Employee.

     var AESKey = "ABCDEFGHIJKLMNOP";
    var algo = "aes";
    var prptobj = {padding:"pkcs5", mode:"ecb"};
     
    /**
    ** returns plainText from encrypted using AES 128 algorithm
    ** AES is symmetric algorithm
    */
    function decrypt(cipherText) {
      //pass key in wordArray format.
      var key = CryptoJS.enc.Utf8.parse(AESKey);
      var stringifyCipher = JSON.stringify({'ct':cipherText});
      var myClearText = kony.crypto.decrypt(algo, key, stringifyCipher, prptobj);
      return myClearText;
    }
     
    function postProcessor(value, context) {
          if(context.metadata.enableEncryption === "true" && context.metadata.encryptionAlgorithm === "AES") {
                value = decrypt(value);
          }
          return value;
    }

 

Sample Code for Context

The following sample JSON code snippet demonstrates for context, for which context is defined for the password property of Employee model object in Organization object service:

 
context = {
    "datatype": "string",
    "object": "Employee",
    "objectService": "Organization",
    "field": "password",
    "metadata": {"enableEncryption": true, "encryptionAlgorithm": "AES", "encryptionKey": "XXXXXX"}
}

 

Advanced Features in Data Pre and Post Processors for Client App

De-registering Processors

To de-register the processors, use null/undefined as callback function, as shown in the following sample code snippet.

 

var employeeModel = kony.mvc.MDAApplication.getSharedInstance().modelStore.getModelDefinition("Employee");
 
function registrationSuccess() {
    alert("Registration Success");
}
 
function registrationFailure(err) {
  alert("Registration Failure" + JSON.stringify(err));
}
 
//unregister preProcessor.
var options = {'preProcessor' : null};
 
employeeModel.registerProcessors(options, registrationSuccess, registrationFailure);

Refreshing metadata in Processors

Use the option getFromServer while registering the processors to refresh meta-data associated with object properties from the server. By default the getFromServer is set to false and cached meta-data is used.

 
function preProcessor(value, context) {
    value = encrypt(value); //transformation logic to be applied before saving value to database or sending to network.
    return value;
}
 
function postProcessor(value, context) {
    value = decrypt(value); //transformation logic to be applied before returning value to user.
    return value;
}
 
var employeeModel = kony.mvc.MDAApplication.getSharedInstance().modelStore.getModelDefinition("Employee");
function registrationSuccess() {
    alert("Registration Success");
}
 
function registrationFailure(err) {
  alert("Registration Failure" + JSON.stringify(err));
}
 
// Refresh metadata from server
var options = {'preProcessor' : preProcessor, "postProcessor" : postProcessor, "getFromServer": true };
 
employeeModel.registerProcessors(options, registrationSuccess, registrationFailure);

 

 

Copyright © 2020 Kony, Inc. All rights reserved.