[OLINGO-1144] How to use the FilterFactory to create a valid URI for filtering on a ComplexValue in a collection
This commit is contained in:
parent
387ba9c096
commit
ee919d812d
|
@ -78,7 +78,10 @@ import org.apache.olingo.client.api.domain.ClientValue;
|
||||||
import org.apache.olingo.client.api.edm.xml.Reference;
|
import org.apache.olingo.client.api.edm.xml.Reference;
|
||||||
import org.apache.olingo.client.api.edm.xml.XMLMetadata;
|
import org.apache.olingo.client.api.edm.xml.XMLMetadata;
|
||||||
import org.apache.olingo.client.api.serialization.ODataDeserializerException;
|
import org.apache.olingo.client.api.serialization.ODataDeserializerException;
|
||||||
|
import org.apache.olingo.client.api.uri.FilterArgFactory;
|
||||||
|
import org.apache.olingo.client.api.uri.FilterFactory;
|
||||||
import org.apache.olingo.client.api.uri.URIBuilder;
|
import org.apache.olingo.client.api.uri.URIBuilder;
|
||||||
|
import org.apache.olingo.client.api.uri.URIFilter;
|
||||||
import org.apache.olingo.client.core.ODataClientFactory;
|
import org.apache.olingo.client.core.ODataClientFactory;
|
||||||
import org.apache.olingo.client.core.uri.URIUtils;
|
import org.apache.olingo.client.core.uri.URIUtils;
|
||||||
import org.apache.olingo.commons.api.edm.Edm;
|
import org.apache.olingo.commons.api.edm.Edm;
|
||||||
|
@ -1663,4 +1666,43 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
|
||||||
assertEquals("EnumMember", expression.getExpressionName());
|
assertEquals("EnumMember", expression.getExpressionName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue1144() {
|
||||||
|
FilterFactory filFactory = getClient().getFilterFactory();
|
||||||
|
FilterArgFactory filArgFactory = filFactory.getArgFactory();
|
||||||
|
URIFilter andFilExp = filFactory.and(filFactory.eq("d/olingo.odata.test1.CTBase/AdditionalPropString", "ADD TEST"),
|
||||||
|
filFactory.eq("d/olingo.odata.test1.CTBase/AdditionalPropString", "ADD TEST"));
|
||||||
|
final URIFilter filter = filFactory.match(
|
||||||
|
filArgFactory.any(filArgFactory.property("CollPropertyComp"), "d", andFilExp));
|
||||||
|
String strFilter = filter.build();
|
||||||
|
ODataEntitySetRequest<ClientEntitySet> request = getClient().getRetrieveRequestFactory()
|
||||||
|
.getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
|
||||||
|
.appendEntitySetSegment(ES_MIX_PRIM_COLL_COMP).filter(strFilter).build());
|
||||||
|
assertNotNull(request);
|
||||||
|
setCookieHeader(request);
|
||||||
|
|
||||||
|
final ODataRetrieveResponse<ClientEntitySet> response = request.execute();
|
||||||
|
saveCookieHeader(response);
|
||||||
|
assertNotNull(response.getHeaderNames());
|
||||||
|
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
|
||||||
|
assertContentType(response.getContentType());
|
||||||
|
|
||||||
|
final ClientEntitySet entitySet = response.getBody();
|
||||||
|
assertNotNull(entitySet);
|
||||||
|
|
||||||
|
assertNull(entitySet.getCount());
|
||||||
|
assertNull(entitySet.getNext());
|
||||||
|
assertEquals(Collections.<ClientAnnotation> emptyList(), entitySet.getAnnotations());
|
||||||
|
assertNull(entitySet.getDeltaLink());
|
||||||
|
|
||||||
|
final List<ClientEntity> entities = entitySet.getEntities();
|
||||||
|
assertNotNull(entities);
|
||||||
|
assertEquals(3, entities.size());
|
||||||
|
final ClientEntity entity = entities.get(2);
|
||||||
|
assertNotNull(entity);
|
||||||
|
final ClientProperty property = entity.getProperty("CollPropertyComp");
|
||||||
|
assertNotNull(property);
|
||||||
|
assertEquals(3, property.getCollectionValue().size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,6 +111,10 @@ public interface FilterArgFactory {
|
||||||
|
|
||||||
FilterArg any(FilterArg collection, URIFilter expression);
|
FilterArg any(FilterArg collection, URIFilter expression);
|
||||||
|
|
||||||
|
FilterArg any(FilterArg collection, String lambdaVariable, URIFilter expression);
|
||||||
|
|
||||||
FilterArg all(FilterArg collection, URIFilter expression);
|
FilterArg all(FilterArg collection, URIFilter expression);
|
||||||
|
|
||||||
|
FilterArg all(FilterArg collection, String lambdaVariable, URIFilter expression);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,12 +246,22 @@ public class FilterArgFactoryImpl implements FilterArgFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FilterArg any(final FilterArg collection, final URIFilter expression) {
|
public FilterArg any(final FilterArg collection, final URIFilter expression) {
|
||||||
return new FilterLambda(collection, "any", expression);
|
return new FilterLambda(collection, "any", expression, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FilterArg all(final FilterArg collection, final URIFilter expression) {
|
public FilterArg all(final FilterArg collection, final URIFilter expression) {
|
||||||
return new FilterLambda(collection, "all", expression);
|
return new FilterLambda(collection, "all", expression, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilterArg any(FilterArg collection, String lambdaVariable, URIFilter expression) {
|
||||||
|
return new FilterLambda(collection, "any", expression, lambdaVariable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilterArg all(FilterArg collection, String lambdaVariable, URIFilter expression) {
|
||||||
|
return new FilterLambda(collection, "all", expression, lambdaVariable);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,18 +29,38 @@ public class FilterLambda implements FilterArg {
|
||||||
|
|
||||||
private final URIFilter expression;
|
private final URIFilter expression;
|
||||||
|
|
||||||
public FilterLambda(final FilterArg collection, final String operator, final URIFilter expression) {
|
private final String lambdaVariable;
|
||||||
|
|
||||||
|
public static final String COLON = ":";
|
||||||
|
|
||||||
|
public static final String OPENBRAC = "(";
|
||||||
|
|
||||||
|
public static final String CLOSEBRAC = ")";
|
||||||
|
|
||||||
|
public static final String SLASH = "/";
|
||||||
|
|
||||||
|
public FilterLambda(final FilterArg collection, final String operator, final URIFilter expression,
|
||||||
|
final String predicate) {
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
|
this.lambdaVariable = predicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String build() {
|
public String build() {
|
||||||
return new StringBuilder(collection.build()).
|
StringBuilder builder = new StringBuilder(collection.build()).
|
||||||
append('/').
|
append(SLASH).
|
||||||
append(operator).
|
append(operator);
|
||||||
|
if (this.lambdaVariable != null && this.lambdaVariable.length() > 0) {
|
||||||
|
builder.append(OPENBRAC).
|
||||||
|
append(lambdaVariable).append(COLON).
|
||||||
append(expression.build()).
|
append(expression.build()).
|
||||||
toString();
|
append(CLOSEBRAC);
|
||||||
|
} else {
|
||||||
|
builder.
|
||||||
|
append(expression.build());
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,4 +91,30 @@ public class FilterFactoryTest extends AbstractTest {
|
||||||
filter.build());
|
filter.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue1144Any() {
|
||||||
|
URIFilter andFilExp = getFilterFactory().and(getFilterFactory().eq("d/Quantity", 100),
|
||||||
|
getFilterFactory().eq("d/Quantity", 50));
|
||||||
|
final URIFilter filter = getFilterFactory().match(
|
||||||
|
getFilterArgFactory().any(getFilterArgFactory().property("Items"), "d", andFilExp));
|
||||||
|
assertEquals("Items/any(d:((d/Quantity eq 100) and (d/Quantity eq 50)))", filter.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void all() {
|
||||||
|
final URIFilter filter = getFilterFactory().match(
|
||||||
|
getFilterArgFactory().all(getFilterArgFactory().property("Items"),
|
||||||
|
getFilterFactory().gt("d:d/Quantity", 100)));
|
||||||
|
|
||||||
|
assertEquals("Items/all(d:d/Quantity gt 100)", filter.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue1144All() {
|
||||||
|
URIFilter andFilExp = getFilterFactory().and(getFilterFactory().eq("d/Quantity", 100),
|
||||||
|
getFilterFactory().eq("d/Quantity", 50));
|
||||||
|
final URIFilter filter = getFilterFactory().match(
|
||||||
|
getFilterArgFactory().all(getFilterArgFactory().property("Items"), "d", andFilExp));
|
||||||
|
assertEquals("Items/all(d:((d/Quantity eq 100) and (d/Quantity eq 50)))", filter.build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue