org.odata4j.cxf.consumer.CxfConsumerQueryRequestBase Maven / Gradle / Ivy
package org.odata4j.cxf.consumer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.core4j.Enumerable;
import org.core4j.Func1;
import org.odata4j.consumer.ODataClientRequest;
import org.odata4j.core.OEntityKey;
import org.odata4j.core.OQueryRequest;
import org.odata4j.edm.EdmDataServices;
import org.odata4j.format.FormatType;
import org.odata4j.internal.EntitySegment;
public abstract class CxfConsumerQueryRequestBase implements OQueryRequest {
private FormatType formatType;
private final String serviceRootUri;
private final EdmDataServices metadata;
private Integer top;
private Integer skip;
private String orderBy;
private String filter;
private String select;
private String lastSegment;
private String expand;
private final List segments = new ArrayList();
private final Map customs = new HashMap();
public CxfConsumerQueryRequestBase(FormatType formatType, String serviceRootUri, EdmDataServices metadata, String lastSegment) {
this.formatType = formatType;
this.serviceRootUri = serviceRootUri;
this.metadata = metadata;
this.lastSegment = lastSegment;
}
protected FormatType getFormatType() {
return this.formatType;
}
protected String getLastSegment() {
return lastSegment;
}
protected String getServiceRootUri() {
return serviceRootUri;
}
protected EdmDataServices getMetadata() {
return metadata;
}
protected ODataClientRequest buildRequest(Func1 pathModification) {
String path = Enumerable.create(segments).join("/");
path += (path.length() == 0 ? "" : "/") + lastSegment;
if (pathModification != null)
path = pathModification.apply(path);
ODataClientRequest request = ODataClientRequest.get(serviceRootUri + path);
if (top != null) {
request = request.queryParam("$top", Integer.toString(top));
}
if (skip != null) {
request = request.queryParam("$skip", Integer.toString(skip));
}
if (orderBy != null) {
request = request.queryParam("$orderby", orderBy);
}
if (filter != null) {
request = request.queryParam("$filter", filter);
}
if (select != null) {
request = request.queryParam("$select", select);
}
for (String name : customs.keySet()) {
request = request.queryParam(name, customs.get(name));
}
if (expand != null) {
request = request.queryParam("$expand", expand);
}
return request;
}
@Override
public OQueryRequest top(int top) {
this.top = top;
return this;
}
@Override
public OQueryRequest skip(int skip) {
this.skip = skip;
return this;
}
@Override
public OQueryRequest orderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
@Override
public OQueryRequest filter(String filter) {
this.filter = filter;
return this;
}
@Override
public OQueryRequest select(String select) {
this.select = select;
return this;
}
@Override
public OQueryRequest expand(String expand) {
this.expand = expand;
return this;
}
@Override
public OQueryRequest nav(Object keyValue, String navProperty) {
return nav(OEntityKey.create(keyValue), navProperty);
}
@Override
public OQueryRequest nav(OEntityKey key, String navProperty) {
segments.add(new EntitySegment(lastSegment, key));
lastSegment = navProperty;
return this;
}
@Override
public OQueryRequest custom(String name, String value) {
customs.put(name, value);
return this;
}
@Override
public Iterator iterator() {
return execute().iterator();
}
}