This blog is the extension to my earlier submission where I tried explain how we can write custom Java script code in Integration Gateway of SMP while exposing the web services as OData .
Let's see how to add filtering capabilities to an OData service with Integration component of SMP 3.0 .
Business Example :
You want to build a service which retrieves list of applications based on the status of the Application.
Tasks
First we need to create "SAP Mobile Platform OData Implementation Project" ,please follow the steps mentioned in the "Writing custom logic using java script in SMP Integration Gateway"
1) Understanding the web service.
The payload of the web service is like below.
This means ,we need to create similar payload from Integration Gateway so that it executes the web service with correct payload.
The above service sample retrieves the list of applications with status equals 'completed'(COMP).This value will be sent to OData as a filter from the UI (Mobile/Desktop).
2) Writing the custom code
The custom code should read the URL and read the filter information like field name and value from the URL.Developer needs to write the custom code to do the same in the IGW project.
We can add the below code to the processRequestData method so that we can read the filter parameters.
var uriInfo = getUriInfo(message);
if (oDataMethod == "GET_FEED"){
uriInfo = (GetEntitySetUriInfo)(uriInfo);
var whereExp = uriInfo.getFilter();
if(whereExp != null){
innerpartneHashMap = new LinkedHashMap();
newpartneHashMap = new LinkedHashMap();
newpartneHashMap.put("applicationStatus", innerpartneHashMap);
innerpartneHashMap.put("boundaryType", "EQUALS");
parseToWhereExpression(whereExp);
partneHashMap.put("arg0", newpartneHashMap);
}
}
Also , add the below methods to the JS file.
function getUriInfo(message) {
importPackage(org.apache.olingo.odata2.api.uri);
importPackage(com.sap.gateway.core.ip.component.commons);
var uriInfo = message.getHeaders().get(ODataExchangeHeaderProperty.UriInfo.toString());
return uriInfo;
}
function parseToWhereExpression(whereExpression) {
importPackage (com.sap.gateway.ip.core.customdev.logging);
importPackage(org.apache.olingo.odata2.api.edm);
var FILTER = "FILTER";
var BINARY ="BINARY";
if (whereExpression.getKind() == 'FILTER') {
return parseToWhereExpression(whereExpression.getExpression());
}
else if (whereExpression.getKind() == 'BINARY') {
var binaryExpression = whereExpression;
var left = parseToWhereExpression(binaryExpression.getLeftOperand());
var right= parseToWhereExpression(binaryExpression.getRightOperand());
innerpartneHashMap.put("highValue",right);
innerpartneHashMap.put("lowValue",right);
return left + "EQ" +right;
}
else if (whereExpression.getKind() == 'PROPERTY') {
var property = whereExpression;
var prop = property.getEdmProperty();
var returnStr = prop.getName();
return returnStr;
}
else if (whereExpression.getKind() == 'LITERAL') {
var literal = whereExpression;
var literalType = literal.getEdmType();
var value = literalType.valueToString(literalType.valueOfString(
literal.getUriLiteral(), EdmLiteralKind.URI, null,
literalType.getDefaultType()),
EdmLiteralKind.DEFAULT, null);
return value;
}
return whereExpression;
}
3) Testing the OData service with filters
https://<<Hostname>>:<<port>>/gateway/odata/SAP/<<servicedocumentname>>/<,Entityset>>?$filter=<,Attributename>> eq '<<value>>'
Also developer can insert the below logging statements so that they can check the SMP logs to check the values that being sent during the execution of the service.
log.logErrors(LogMessage.TechnicalError,<<Your custom message>>);