[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:
ramya vasanth 2017-12-18 15:04:16 +05:30
parent 387ba9c096
commit ee919d812d
5 changed files with 111 additions and 9 deletions

View File

@ -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.XMLMetadata;
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.URIFilter;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.client.core.uri.URIUtils;
import org.apache.olingo.commons.api.edm.Edm;
@ -1663,4 +1666,43 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
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());
}
}

View File

@ -110,7 +110,11 @@ public interface FilterArgFactory {
FilterArg geoLength(FilterArg first, FilterArg second);
FilterArg any(FilterArg collection, URIFilter expression);
FilterArg any(FilterArg collection, String lambdaVariable, URIFilter expression);
FilterArg all(FilterArg collection, URIFilter expression);
FilterArg all(FilterArg collection, String lambdaVariable, URIFilter expression);
}

View File

@ -246,12 +246,22 @@ public class FilterArgFactoryImpl implements FilterArgFactory {
@Override
public FilterArg any(final FilterArg collection, final URIFilter expression) {
return new FilterLambda(collection, "any", expression);
return new FilterLambda(collection, "any", expression, null);
}
@Override
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);
}
}

View File

@ -28,19 +28,39 @@ public class FilterLambda implements FilterArg {
private final String operator;
private 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) {
public FilterLambda(final FilterArg collection, final String operator, final URIFilter expression,
final String predicate) {
this.collection = collection;
this.operator = operator;
this.expression = expression;
this.lambdaVariable = predicate;
}
@Override
public String build() {
return new StringBuilder(collection.build()).
append('/').
append(operator).
append(expression.build()).
toString();
StringBuilder builder = new StringBuilder(collection.build()).
append(SLASH).
append(operator);
if (this.lambdaVariable != null && this.lambdaVariable.length() > 0) {
builder.append(OPENBRAC).
append(lambdaVariable).append(COLON).
append(expression.build()).
append(CLOSEBRAC);
} else {
builder.
append(expression.build());
}
return builder.toString();
}
}

View File

@ -90,5 +90,31 @@ public class FilterFactoryTest extends AbstractTest {
assertEquals("(OrderDate ge " + Encoder.encode("2011-03-08T14:21:12-08:00") + ")",
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());
}
}