Note:
If you don’t like reading, just scroll down to the end of this page to view the full source code.
List<Map<String, Object>> peopleList = getAllPeople();
int skipNumber = request.getSkipOptionValue();
peopleList = peopleList.subList(skipNumber, fullSize);
List<Map<String, Object>> allPeopleList = getAllPeople();
int skip = request.getSkipOptionValue();
if(skip > -1) {
int fullSize = peopleList.size();
if(skip >= fullSize) {
// e.g. $skip=999 . we need to handle this to avoid exceptions
return QueryResponse.setSuccess().setDataAsMap(Collections.emptyList()).response();
}else {
// now handle skip
List<Map<String, Object>> skipList = peopleList.subList(skip, fullSize);
return QueryResponse.setSuccess().setDataAsMap(skipList).response();
}
}else {
return QueryResponse.setSuccess().setDataAsMap(allPeopleList).response();
}
Note:
This note is rather a BTW:
As usual, we have to know exactly what we’re doing, so we need to have a look at the spec
List<OrderByExpression> expressionsList = request.getOrderByProperties();
OrderByExpression expression = expressionsList.get(0);
String propertyName = expression.getOrderByProperty();
Collections.sort(allPeopleList, new Comparator<Map<String, Object>>() {
...
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String s1 = (String)o1.get(propertyName);
String s2 = (String)o2.get(propertyName);
return s1.compareTo(s2);
@Query(serviceName="DemoService", entity="People")
public QueryResponse getPeople(QueryRequest request) {
List<Map<String, Object>> allPeopleList = getAllPeople();
List<OrderByExpression> expressionsList = request.getOrderByProperties();
if(expressionsList != null && ! expressionsList.isEmpty()) {
OrderByExpression expression = expressionsList.get(0);
String propertyName = expression.getOrderByProperty();
Collections.sort(allPeopleList, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String s1 = (String)o1.get(propertyName);
String s2 = (String)o2.get(propertyName);
return s1.compareTo(s2);
}
});
}
return QueryResponse.setSuccess().setDataAsMap(allPeopleList).response();
}
boolean isDescending = expression.isDescending();
if (isDescending) {
result = -result; // change the (normal) ordering to the contrary
}
@Query(serviceName="DemoService", entity="People")
public QueryResponse getPeople(QueryRequest request) {
List<Map<String, Object>> allPeopleList = getAllPeople();
// handle orderby
List<OrderByExpression> orderByProperties = request.getOrderByProperties();
if(orderByProperties != null && ! orderByProperties.isEmpty()) {
// apply $orderby
OrderByExpression expression = orderByProperties.get(0);
String propertyName = expression.getOrderByProperty();
boolean isDescending = expression.isDescending();
// do the sorting
Collections.sort(allPeopleList, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int result = 0;
if(propertyName.equals("PersonId") || propertyName.equals("JobId") ) {
// handle integers
Integer id1 = (Integer)o1.get(propertyName);
Integer id2 = (Integer)o2.get(propertyName);
result = id1.compareTo(id2);
}else if (propertyName.equals("IsFriend") ){
// handle booleans
Boolean b1 = (Boolean)o1.get(propertyName);
Boolean b2 = (Boolean)o2.get(propertyName);
result = b1.compareTo(b2);
}else if (propertyName.equals("Email") ){
// special handling for mail: lets sort by mail provider.
String mail1 = (String)o1.get(propertyName);
String s1 = mail1.substring(mail1.indexOf("@") + 1);
String mail2 = (String)o2.get(propertyName);
String s2 = mail2.substring(mail2.indexOf("@") + 1);
result = s1.compareTo(s2);
}else {
// handle strings
String s1 = (String)o1.get(propertyName);
String s2 = (String)o2.get(propertyName);
result = s1.compareTo(s2);
}
// finally we need to handle the asc versus desc
if (isDescending) {
result = -result; // change the (normal) ordering to the contrary
}
return result;
}
});
}
// no sorting required, just return as usual
return QueryResponse.setSuccess().setDataAsMap(allPeopleList).response();
}
Note:
If the desired sort order is ascending, we don’t need to specify asc, because it is the default
If no unique ordering is imposed through an $orderby query option, the service MUST impose a stable ordering across requests that include $skip.
// Navigation from one job to all people who have this job.
// Example URI: /srv/Jobs(1)/Employees
@Query(serviceName = "DemoService", entity = "People", sourceEntity = "Jobs")
public QueryResponse getPeopleForJob(QueryRequest request) {
// 1-to-many navigation, then apply $orderby and $skip
@Query(serviceName = "DemoService", entity = "People", sourceEntity = "Jobs")
public QueryResponse getPeopleForJob(QueryRequest request) {
List<Map<String,Object>> responsePeopleList = new ArrayList<Map<String, Object>>();
// retrieve the navigation target people list
int jobId = (int) request.getSourceKeys().get("JobId");
responsePeopleList = getPeopleWithJob(jobId);
// after getting the navigation result list, now apply system query options
// handle $orderby on top of the list of target employees
responsePeopleList = applyOrderby(responsePeopleList, request.getOrderByProperties());
// finally apply $skip
responsePeopleList = applySkip(responsePeopleList, request.getSkipOptionValue());
// return the list, it might have been modified by above code, or not
return QueryResponse.setSuccess().setDataAsMap(responsePeopleList).response();
}
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="demo" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonId" />
</Key>
<Property Name="PersonId" Type="Edm.Int32"/>
<Property Name="FirstName" Type="Edm.String"/>
<Property Name="LastName" Type="Edm.String"/>
<Property Name="Email" Type="Edm.String"/>
<Property Name="JobId" Type="Edm.Int32" />
<Property Name="IsFriend" Type="Edm.Boolean"/>
</EntityType>
<EntityContainer Name="container">
<EntitySet Name="People" EntityType="demo.Person"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
package com.example.DemoProject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sap.cloud.sdk.service.prov.api.operations.Query;
import com.sap.cloud.sdk.service.prov.api.request.OrderByExpression;
import com.sap.cloud.sdk.service.prov.api.request.QueryRequest;
import com.sap.cloud.sdk.service.prov.api.response.QueryResponse;
public class ServiceImplementation {
@Query(serviceName="DemoService", entity="People")
public QueryResponse getPeople(QueryRequest request) {
// first get the requested list including all people. Afterwards we apply query options, if required
List<Map<String, Object>> peopleList = getAllPeople();
// handle $orderby
List<OrderByExpression> orderByProperties = request.getOrderByProperties();
if(orderByProperties != null && ! orderByProperties.isEmpty()) { // if not orderby was added to URL, then we get an empty list
// apply $orderby
OrderByExpression expression = orderByProperties.get(0); // let's support only one property for sorting
String propertyName = expression.getOrderByProperty();
boolean isDescending = expression.isDescending();
// do the sorting
Collections.sort(peopleList, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int result = 0;
if(propertyName.equals("PersonId") || propertyName.equals("JobId") ) {
// handle integers
Integer id1 = (Integer)o1.get(propertyName);
Integer id2 = (Integer)o2.get(propertyName);
result = id1.compareTo(id2);
}else if (propertyName.equals("IsFriend") ){
// handle booleans
Boolean b1 = (Boolean)o1.get(propertyName);
Boolean b2 = (Boolean)o2.get(propertyName);
result = b1.compareTo(b2);
}else if (propertyName.equals("Email") ){
// special handling for mail: lets sort by mail provider. Just for fun
String mail1 = (String)o1.get(propertyName);
String s1 = mail1.substring(mail1.indexOf("@") + 1);
String mail2 = (String)o2.get(propertyName);
String s2 = mail2.substring(mail2.indexOf("@") + 1);
result = s1.compareTo(s2);
}else {
// handle strings
String s1 = (String)o1.get(propertyName);
String s2 = (String)o2.get(propertyName);
result = s1.compareTo(s2);
}
// finally we need to handle the asc versus desc
if (isDescending) {
result = -result; // change the (normal) ordering to the contrary
}
return result;
}
});
}
// afterwards, apply $skip on the ordered list
int skip = request.getSkipOptionValue();
if(skip > -1) { // note that we always get a value, even if there's no skip in the URL
int fullSize = peopleList.size();
if(skip >= fullSize) {
// e.g. $skip=999 . we need to handle this to avoid exceptions
peopleList.clear();
}else {
// handle skip
peopleList = peopleList.subList(skip, fullSize);
}
}
// finally, return the list, it might have been modified by above code, or not
return QueryResponse.setSuccess().setDataAsMap(peopleList).response();
}
/* Dummy Database */
private List<Map<String, Object>> getAllPeople(){
List<Map<String, Object>> peopleList = new ArrayList<Map<String, Object>>();
// init the "database"
if(peopleList.isEmpty()) {
peopleList.add(createPerson(1, "Anna", "Martinez", "Anna@fastmail.com", 1, true));
peopleList.add(createPerson(2, "Eric", "Richards", "err@nicemail.com", 3, false));
peopleList.add(createPerson(3, "Debbie", "Huffington", "debbie@ahamail.com", 2, true));
peopleList.add(createPerson(4, "Fanny", "Mendelssohn", "fanny@funnymail.com", 3, true));
peopleList.add(createPerson(5, "Claudia", "Forrester", "CF@goodmail.com", 1, false));
peopleList.add(createPerson(6, "Berta", "Hughes", "Berta@webmail.com", 2, false));
}
return peopleList;
}
private Map<String, Object> createPerson(int personId, String firstName, String lastName, String mail, int jobId, boolean isFriend){
Map<String, Object> personMap = new HashMap<String, Object>();
personMap.put("PersonId", personId);
personMap.put("FirstName", firstName);
personMap.put("LastName", lastName);
personMap.put("JobId", jobId);
personMap.put("Email", mail);
personMap.put("IsFriend", isFriend);
return personMap;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
40 | |
25 | |
17 | |
13 | |
7 | |
7 | |
7 | |
6 | |
6 | |
6 |