Skip to Content
0
Former Member
Jul 27, 2015 at 08:47 AM

OData Query operation with filter for 4 parameters

424 Views

Hi Experts,

We have stuck in developing OData service with filter operation for 4 or more parameters for query operation.

we are able to do the filter for single paramater for query operation, but how can we implement the same for 4 parameters no idea.

below code works when we use the URL with filter for one paramater:

https://localhost:8083/gateway/odata/SAP/MYEMP;v=1/Set?$filter=DataID eq 'Ray'

here we have one input parameter (Name) which we are passing value as Ray.

/**

Function processRequestData will be called just after the Request

flow is triggered.

Implement processRequestData for additional functionalities as

$TOP, $SKIP, etc.

Prepare a HashMap and set it to message body. This HashMap has keys as

parameter names for the required web service operation, and values as

the values for those parameters.

*/

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;

}

importPackage(java.util);

var childMap2 = new LinkedHashMap();

function processRequestData(message) {

importPackage(com.sap.gateway.core.ip.component.commons);

var odataMethod = message.getHeaders().get(ODataExchangeHeaderProperty.ODataMethod.toString());

var uriInfo = getUriInfo(message);

//if query oeration

if(odataMethod == "GET_FEED"){

if (uriInfo.getFilter() != null){

importPackage(com.sap.core.odata.api.uri.expression);

var whereExp = uriInfo.getFilter();

parseToWhereExpression(whereExp);

message.setBody(childMap2);

}

importPackage (com.sap.gateway.ip.core.customdev.logging);

log.logErrors(LogMessage.TechnicalError, "This is first log:::"+message.getBody().toString());

}

return message;

}

function parseToWhereExpression(whereExpression) {

importPackage (com.sap.gateway.ip.core.customdev.logging);

var FILTER = "FILTER";

var BINARY ="BINARY";

if (whereExpression.getKind() == 'FILTER') {

log.logErrors(LogMessage.TechnicalError, "***KIND** is Filter");

log.logErrors(LogMessage.TechnicalError, "***WhereExp** in Filter"+ whereExpression.getExpression());

return parseToWhereExpression(whereExpression.getExpression());

}

else if (whereExpression.getKind() == 'BINARY') {

log.logErrors(LogMessage.TechnicalError, "***KIND** is Binary" );

var binaryExpression = whereExpression;

var left = parseToWhereExpression(binaryExpression.getLeftOperand());

var right= parseToWhereExpression(binaryExpression.getRightOperand());

childMap2.put("key:Name",right);

log.logErrors(LogMessage.TechnicalError, "***return** " + left + " EQ " +right);

return left + "EQ" +right;

}

else if (whereExpression.getKind() == 'PROPERTY') {

log.logErrors(LogMessage.TechnicalError, "***KIND** is Property" );

var property = whereExpression;

importPackage(org.apache.olingo.odata2.api.edm);

var prop = property.getEdmProperty();

var returnStr = prop.getName();

return returnStr;

}

else if (whereExpression.getKind() == 'LITERAL') {

log.logErrors(LogMessage.TechnicalError, "***KIND** is Literal" );

importPackage(org.apache.olingo.odata2.api.edm);

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;

}

else {

log.logErrors(LogMessage.TechnicalError, "***returning NULL****");

}

}

function processRequestXML(message) {

importPackage (com.sap.gateway.ip.core.customdev.logging);

log.logErrors(LogMessage.TechnicalError, "This is Request log:::"+message.getBody().toString());

return message;

}

/**

Gets the SOAP request XML here. This method can be used if extra headers

need to be added to the XML request.

*/

/**

Gets the web service response XML. If the web service returns values which are not mapped to EDM types,

those values can be filtered out here e.g. In line count, eTag, etc.

*/

function processResponseXML(message) {

importPackage (com.sap.gateway.ip.core.customdev.logging);

log.logErrors(LogMessage.TechnicalError, "This is second log:::"+message.getBody().toString());

return message;

}

function processResponseData(message) {

return message;

}

Now we have four parameter like: Name, LastName, Dept, EMPID.

these are four mandatory parameters which will retrieve the data accordingly this filter and no idea how to implement this filter operation.

Please help.