SAP JAVA VDM split $filter in input

224 views Asked by At

I've create a project VDM java.In Query response i would like to split the input filters is it possible?

public class ControStatus {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Query(serviceName = "CatalogService", entity = "ControStatus")
public QueryResponse getEntity(QueryRequest queryRequest) {

    DefaultZSCPSCPDISRVService ControlStatus = new DefaultZSCPSCPDISRVService();


        FilterExpression filtriFrontEnd = FilterExpressionConverter.convertTo(queryRequest.getQueryExpression());
        

        log.error("Filtri" + filtriFrontEnd.toString());

}

}

When they call srv passing some filters eg. .... / CatalogService / ControStatus? $ Filter = Bukrs eq "XXXX" and Werks eq "XXXX". I can read them with queryRequest.getQueryExpression(), in the form of filter.Expression. There is a way to turn the answer into a json and read the individual values.Es. What value did they pass for bukrs or werks?

1

There are 1 answers

0
Alexander Dümont On

I'm not aware of a way how to extract the information from the type FilterExpression. This is API coming from the CAP / SAP Service SDK library. You can find their official documentation here. My suggestion would be to ask the authors of this library. The tag sap-cloud-sdk refers to a different API, the SAP Cloud SDK. Let me know if you need help for establishing contact.

I know that the class is regularly used with Object#toString method for constructing a query string. In a similar fashion you could extract the field values via regex:

FilterExpression filtriFrontEnd;

String input = filtriFrontEnd.toString()

Pattern pattern = Pattern.compile("(and |or |)?(\\w+) (eq|ne|gt) ('[^']*?'|[\\d]+)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    // matcher.group(2)
    // matcher.group(3)
    // matcher.group(4)
}

Example:

String input = "$filter=Bukrs eq 'XXXX' and (Werks eq 'XXXX' or (Age gt 1))";

Pattern pattern = Pattern.compile("(and |or |)?(\\w+) (eq|ne|gt) ('[^']*?'|[\\d]+)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.printf("field: %s, operation: %s, value: %s%n", matcher.group(2), matcher.group(3), matcher.group(4));
}

---

field: Bukrs, operation: eq, value: 'XXXX'
field: Werks, operation: eq, value: 'XXXX'
field: Age, operation: gt, value: 1

The problem is: you wouldn't know whether the filters are part of a conjunction or disjunction, parentheses are also not considered. In addition you should consider other operations than eq/ne/gt as well.