Class DefaultJsonDataService

  • All Implemented Interfaces:
    JsonDataService

    public class DefaultJsonDataService
    extends Object
    implements JsonDataService
    Implements generic data operations which have parameters and json as an input and return results as json strings. Note the parameters, json input and generated json follow the Smartclient specs. See the Smartclient RestDataSource for more information. The main usage of this class is through the getInstance() method (as a singleton). This class can however also be extended and instantiated directly. There are several methods to override/implement update and insert hooks, see the pre* and post* methods.
    Author:
    mtaal
    • Constructor Detail

      • DefaultJsonDataService

        public DefaultJsonDataService()
    • Method Detail

      • fetch

        public String fetch​(Map<String,​String> parameters)
        Description copied from interface: JsonDataService
        Do a query for BaseOBObject objects and return the result as a json string. One parameter is mandatory: JsonConstants.ENTITYNAME. The possible query parameters can be found in JsonConstants, see the constants ending with _PARAMETER. All other request parameters which correspond to properties of the queried objects are considered to be filter properties. The result is a json string which contains paging information (startrow, endrow, rowcount) and the data itself. See the Smartclient RestDataSource for more information.
        Specified by:
        fetch in interface JsonDataService
        Parameters:
        parameters - the parameters driving the query, one parameter is mandatory: JsonConstants.ENTITYNAME
        Returns:
        a json result string
      • fetch

        public String fetch​(Map<String,​String> parameters,
                            boolean filterOnReadableOrganizations)
      • convertParameterToString

        public static String convertParameterToString​(Map<String,​String> parameters)
      • add

        public String add​(Map<String,​String> parameters,
                          String content)
        Description copied from interface: JsonDataService
        Adds data passed as json. Note that this method currently forwards to the JsonDataService.update(Map, String) method. The system follows this logic: if the json defines an id for an object and it exists in the database then it is updated, in all other cases an insert takes place.
        Specified by:
        add in interface JsonDataService
        Parameters:
        parameters - contains extra parameters, not used by this implementation but can be convenient for extenders of this service.
        content - the json string containing the data, the expected format is the same as defined by the Smartclient RestDataSource
        Returns:
        the inserted objects as json or the an error json string
      • update

        public String update​(Map<String,​String> parameters,
                             String content)
        Description copied from interface: JsonDataService
        Updates data passed as json. Note that this method currently both allows additions as well as updated. The system follows this logic: if the json defines an id for an object and it exists in the database then it is updated, in all other cases an insert takes place.
        Specified by:
        update in interface JsonDataService
        Parameters:
        parameters - contains extra parameters, not used by this implementation but can be convenient for extenders of this service.
        content - the json string containing the data, the expected format is the same as defined by the Smartclient RestDataSource
        Returns:
        the inserted objects as json or the an error json string
      • doPreRemove

        protected void doPreRemove​(Map<String,​String> parameters,
                                   org.codehaus.jettison.json.JSONObject toRemove)
                            throws org.codehaus.jettison.json.JSONException
        Is called before the actual remove of the object. The toRemove object contains the id and entity name of the to-be-deleted object.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPostRemove

        protected void doPostRemove​(Map<String,​String> parameters,
                                    org.codehaus.jettison.json.JSONObject removed)
                             throws org.codehaus.jettison.json.JSONException
        Is called after the remove in the database but before the commit. The removed parameter object can be changed, the changes are sent to the client. This method is called in the same transaction as the remove action.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPreFetch

        protected void doPreFetch​(Map<String,​String> parameters)
                           throws org.codehaus.jettison.json.JSONException
        Is called before fetching an object. This method is called in the same transaction as the main fetch operation.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPostFetch

        protected void doPostFetch​(Map<String,​String> parameters,
                                   org.codehaus.jettison.json.JSONObject fetched)
                            throws org.codehaus.jettison.json.JSONException
        Is called after fetching an object before the result is sent to the client, the fetched JSONObject can be changed. The changes are sent to the client. This method is called in the same transaction as the main fetch operation.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPreInsert

        protected void doPreInsert​(Map<String,​String> parameters,
                                   org.codehaus.jettison.json.JSONObject toInsert)
                            throws org.codehaus.jettison.json.JSONException
        Is called before an object is inserted. The toInsert JSONObject can be changed, the changes are persisted to the database. This method is called in the same transaction as the insert.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPostInsert

        protected void doPostInsert​(Map<String,​String> parameters,
                                    org.codehaus.jettison.json.JSONObject inserted,
                                    String originalToInsert)
                             throws org.codehaus.jettison.json.JSONException
        Is called after the insert action in the same transaction as the insert. The inserted JSONObject can be changed, the changes are sent to the client. The originalToInsert contains the json object/array string as it was passed into the doPreInsert method. The inserted JSONObject is the object read from the database after it was inserted. So it contains the changes done by stored procedures.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPreUpdate

        protected void doPreUpdate​(Map<String,​String> parameters,
                                   org.codehaus.jettison.json.JSONObject toUpdate)
                            throws org.codehaus.jettison.json.JSONException
        Called before the update of an object. Is called in the same transaction as the main update operation. Changes to the toUpdate JSONObject are persisted in the database.
        Throws:
        org.codehaus.jettison.json.JSONException
      • doPostUpdate

        protected void doPostUpdate​(Map<String,​String> parameters,
                                    org.codehaus.jettison.json.JSONObject updated,
                                    String originalToUpdate)
                             throws org.codehaus.jettison.json.JSONException
        Called after the updates have been done, within the same transaction as the main update. Changes to the updated JSONObject are sent to the client (but not persisted to the database). The originalToUpdate contains the json object/array string as it was passed into the doPreUpdate method. The updated JSONObject is the object read from the database after it was updated. So it contains all the changes done by stored procedures.
        Throws:
        org.codehaus.jettison.json.JSONException