You are here:
RESTCPQChanger
Builds the command structure from the REST request, which is in JSON format. From the command structure, the system can get the command that is used in RESTResource.
Interface
RESTCPQInterfaceRESTCPQInterface
Sample Code
global with sharing class RESTCPQChanger implements GlobalInterfaces.RESTCPQInterface
{
global RESTCPQChanger()
{
}
/**
@param req (RestRequest)
@param parsed (List<Map<String, String>>) : list of PBE processed so far
@return List<Map<String, String>> : filtered list
@description <strong>Signature</strong>: global List<Map<String, String>> fillCommandStruct(RestRequest req, List<Map<String, String>> parsed) <br>
This builds the command structure from the REST request which is in JSON format.<br>
From the command structure we can get the command which will be used in RESTResource<br>
**/
global List<Map<string, string>> fillCommandStruct(RestRequest req, List<Map<string, string>> parsed)
{
Logger.dbg('In RESTCPQChanger');
// Default functionality
List<Map<string, string>> reqBody = new List<Map<string, string>>();
List<Map<string, string>> commandStruct;
string objId1 = getID(req.requestURI);
// Modified functionality that overwrites default functionality.
if(req.httpMethod.equals('PATCH'))
{
try
{
reqBody = (List<Map<string, string>>)JSON.deserialize(req.requestBody.toString(), List<Map<string, string>>.class);
}
catch(System.JSONException jsE)
{
}
// No body passed in. Figure out what to do.
for(Map<string, string> req1: reqBody)
{
req1.put('command', 'changeQty');
commandStruct = new List<Map<string, string>>{new Map<string, string>(req1)};
}
}
else if(req.httpMethod.equals('GET'))
{
string objId = getID(req.requestURI);
if(objId != null)
{
commandStruct = new List<Map<string, string>>{new Map<string, string>{
'command' => 'getOrderItems'}, new Map<string, string>{
'command' => 'getErrors'}};
}
else
{
string acctId = req.params.get('accountId');
if(acctId != null)
{
commandStruct = new List<Map<string, string>>{new Map<string, string>{
'command' => 'getOrders',
'accountId' => acctId}};
}
}
}
else if(req.httpMethod.equals('POST'))
{
try
{
reqBody = (List<Map<string, string>>)JSON.deserialize(req.requestBody.toString(), List<Map<string, string>>.class);
}
catch(System.JSONException jsE)
{
}
// No body passed in. Figure out what to do.
for(Map<string, string> req1: reqBody)
{
req1.put('command', 'newOrder');
commandStruct = new List<Map<string, string>>{new Map<string, string>(req1)};
}
}
return commandStruct;
}
/**
@param returnData (List<Map<String, String>>) : list of PBE processed so far
@return List<Map<String, String>> : filtered list
@description <strong>Signature</strong>: global List<Map<String, Object>> fillOutReturn(List<Map<String, Object>> returnData) <br>
Currently this method returns the data passed in <br>
*/
global List<Map<string, object>> fillOutReturn(List<Map<string, object>> returnData)
{
return returnData;
}
private static Id getID(string s)
{
List<string> path = s.split('/');
return checkValidID(path.get(path.size() - 1));// what if path.size == 0?
}
// This seems pointless now but eventually this may have extra checking so
// USE IT!
private static Id checkValidID(string id)
{
Id obj = null;
try
{
obj = (Id)id;
}
catch(System.StringException e)
{
Logger.dbg('Invalid ID string passed');
// If it is an invalid ID, this will throw a null pointer later on.
}
return obj;
}
}
Related Implementations
DefaultRESTCPQInterfaceImplementationDefaultRESTCPQInterfaceImplementation

