Package org.openbravo.service.json
Class DefaultJsonDataService
- java.lang.Object
-
- org.openbravo.service.json.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 thegetInstance()
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DefaultJsonDataService.DataSourceAction
static class
DefaultJsonDataService.QueryResultWriter
-
Constructor Summary
Constructors Constructor Description DefaultJsonDataService()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
add(Map<String,String> parameters, String content)
Adds data passed as json.protected List<BaseOBObject>
bobFetchTransformation(List<BaseOBObject> bobs, Map<String,String> parameters)
static String
convertParameterToString(Map<String,String> parameters)
protected DataEntityQueryService
createSetQueryService(Map<String,String> parameters, boolean forCountOperation)
protected String
doPostAction(Map<String,String> parameters, String content, DefaultJsonDataService.DataSourceAction action, String originalObject)
protected void
doPostFetch(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject fetched)
Is called after fetching an object before the result is sent to the client, the fetchedJSONObject
can be changed.protected void
doPostInsert(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject inserted, String originalToInsert)
Is called after the insert action in the same transaction as the insert.protected void
doPostRemove(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject removed)
Is called after the remove in the database but before the commit.protected void
doPostUpdate(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject updated, String originalToUpdate)
Called after the updates have been done, within the same transaction as the main update.protected String
doPreAction(Map<String,String> parameters, String content, DefaultJsonDataService.DataSourceAction action)
protected void
doPreFetch(Map<String,String> parameters)
Is called before fetching an object.protected void
doPreInsert(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject toInsert)
Is called before an object is inserted.protected void
doPreRemove(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject toRemove)
Is called before the actual remove of the object.protected void
doPreUpdate(Map<String,String> parameters, org.codehaus.jettison.json.JSONObject toUpdate)
Called before the update of an object.String
fetch(Map<String,String> parameters)
Do a query forBaseOBObject
objects and return the result as a json string.String
fetch(Map<String,String> parameters, boolean filterOnReadableOrganizations)
void
fetch(Map<String,String> parameters, DefaultJsonDataService.QueryResultWriter writer)
static DefaultJsonDataService
getInstance()
String
remove(Map<String,String> parameters)
Remove an object, this method expects two parameters:JsonConstants.ID
andJsonConstants.ENTITYNAME
.static void
setInstance(DefaultJsonDataService instance)
String
update(Map<String,String> parameters, String content)
Updates data passed as json.
-
-
-
Method Detail
-
getInstance
public static DefaultJsonDataService getInstance()
-
setInstance
public static void setInstance(DefaultJsonDataService instance)
-
fetch
public String fetch(Map<String,String> parameters)
Description copied from interface:JsonDataService
Do a query forBaseOBObject
objects and return the result as a json string. One parameter is mandatory:JsonConstants.ENTITYNAME
. The possible query parameters can be found inJsonConstants
, 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 interfaceJsonDataService
- Parameters:
parameters
- the parameters driving the query, one parameter is mandatory:JsonConstants.ENTITYNAME
- Returns:
- a json result string
-
fetch
public void fetch(Map<String,String> parameters, DefaultJsonDataService.QueryResultWriter writer)
-
createSetQueryService
protected DataEntityQueryService createSetQueryService(Map<String,String> parameters, boolean forCountOperation)
-
convertParameterToString
public static String convertParameterToString(Map<String,String> parameters)
-
remove
public String remove(Map<String,String> parameters)
Description copied from interface:JsonDataService
Remove an object, this method expects two parameters:JsonConstants.ID
andJsonConstants.ENTITYNAME
. If they are not present an error is returned.- Specified by:
remove
in interfaceJsonDataService
- Parameters:
parameters
- two parameters should be present: id and entityName- Returns:
- the id and entityname of the removed object is returned.
-
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 theJsonDataService.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 interfaceJsonDataService
- 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 interfaceJsonDataService
- 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
-
bobFetchTransformation
protected List<BaseOBObject> bobFetchTransformation(List<BaseOBObject> bobs, Map<String,String> parameters)
-
doPreAction
protected String doPreAction(Map<String,String> parameters, String content, DefaultJsonDataService.DataSourceAction action)
-
doPostAction
protected String doPostAction(Map<String,String> parameters, String content, DefaultJsonDataService.DataSourceAction action, String originalObject)
-
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 fetchedJSONObject
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 toInsertJSONObject
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 insertedJSONObject
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 toUpdateJSONObject
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 updatedJSONObject
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
-
-