How To Implement JsonDataServiceExtraActions Hook

From InfiniteERP Wiki
Revision as of 12:19, 16 December 2021 by Wikiadmin (talk | contribs) (Created page with "== Introduction == This document explains how to implement '''JsonDataServiceExtraActions''' Hook. This hook is called before and after each operation of ''DefaultJSONDataSer...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

This document explains how to implement JsonDataServiceExtraActions Hook. This hook is called before and after each operation of DefaultJSONDataService class.

Hook Implementation

The hook is implemented by extending the JsonDataServiceExtraActions class. This class has two methods to implement:

doPreAction method:

This void method is called at the beginning of each DefaultJSONDataService action. It has 3 parameters:

parameters
The Map with the parameters of the DataSource call.
data
JSONArray with the records that are going to be inserted, updated or deleted. Modify this object in case it is required to modify the data before executing the action. Fetch operations receive an empty array.
action
DataSourceAction enum value with the action of the DataSource call. Possible values are FETCH, ADD, UPDATE and REMOVE.

doPostAction method:

This void method is called at the end of each DefaultJSONDataService action. It has 4 parameters:

parameters
The Map with the parameters of the DataSource call.
content
JSONObject with the current content that is returned to the client. Modify this object in case it is required to modify the data before is returned.
action
DataSourceAction enum value with the action of the DataSource call. Possible values are FETCH, ADD, UPDATE and REMOVE.
originalObject
JSONObject String available only on ADD and UPDATE with the original values of the data.

Example

This example logs a line every time a window is loaded. You can find the code described below in the org.openbravo.platform.features module.

<source lang="java">public class JsonDataServiceExtraActionsExample implements JsonDataServiceExtraActions {

 private static final Logger log = LoggerFactory
     .getLogger(JsonDataServiceExtraActionsExample.class);
 @Override
 public void doPreAction(Map<String, String> parameters, JSONArray newData, DataSourceAction action) {
   log.debug("JsonDataServiceExtraActionsExample doPreAction implementation");
 }
 @Override
 public void doPostAction(Map<String, String> parameters, JSONObject content,
     DataSourceAction action, String originalObject) {
 }

} </source>