org.apache.olingo.odata2.api.uri.expression.FilterExpression Java Examples

The following examples show how to use org.apache.olingo.odata2.api.uri.expression.FilterExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OlingoManager.java    From DataHubSystem with GNU Affero General Public License v3.0 8 votes vote down vote up
public List<Product> getProducts(User user, String uuid,
      FilterExpression filter_expr, OrderByExpression order_expr, int skip,
      int top) throws ExceptionVisitExpression, ODataApplicationException
{
   ProductSQLVisitor expV = new ProductSQLVisitor();
   Object visit_result = null;

   if (filter_expr != null)
   {
      visit_result = filter_expr.accept(expV);
   }
   if (order_expr != null)
   {
      visit_result = order_expr.accept(expV);
   }

   return productService.getProducts((DetachedCriteria) visit_result, uuid,
         skip, top);
}
 
Example #2
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonMember() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "Location/Country");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkMember(jsonMap, null);

  LinkedTreeMap<String, Object> source = (LinkedTreeMap<String, Object>) jsonMap.get(SOURCE);
  checkProperty(source, null, "Location");

  LinkedTreeMap<String, Object> path = (LinkedTreeMap<String, Object>) jsonMap.get(PATH);
  checkProperty(path, null, "Country");
}
 
Example #3
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonMember2() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "Location/Country/PostalCode");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkMember(jsonMap, null);

  LinkedTreeMap<String, Object> source1 = (LinkedTreeMap<String, Object>) jsonMap.get(SOURCE);
  checkMember(source1, null);

  LinkedTreeMap<String, Object> source2 = (LinkedTreeMap<String, Object>) source1.get(SOURCE);
  checkProperty(source2, null, "Location");

  LinkedTreeMap<String, Object> path1 = (LinkedTreeMap<String, Object>) source1.get(PATH);
  checkProperty(path1, null, "Country");

  LinkedTreeMap<String, Object> path = (LinkedTreeMap<String, Object>) jsonMap.get(PATH);
  checkProperty(path, null, "PostalCode");
}
 
Example #4
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonBinaryLiteral() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "'a' eq 'b'");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkBinary(jsonMap, "eq", "Edm.Boolean");

  LinkedTreeMap<String, Object> left = (LinkedTreeMap<String, Object>) jsonMap.get(LEFT);
  checkLiteral(left, "Edm.String", "a");

  LinkedTreeMap<String, Object> right = (LinkedTreeMap<String, Object>) jsonMap.get(RIGHT);
  checkLiteral(right, "Edm.String", "b");
}
 
Example #5
Source File: OlingoManager.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
public List<User> getUsers(
      FilterExpression filter_expr, OrderByExpression order_expr, int skip,
      int top) throws ExceptionVisitExpression, ODataApplicationException
{
   UserSQLVisitor expV = new UserSQLVisitor();
   Object visit = null;
   if (filter_expr != null)
   {
      visit = filter_expr.accept(expV);
   }
   if (order_expr != null)
   {
      visit = order_expr.accept(expV);
   }
   return userService.getUsers((DetachedCriteria) visit, skip, top);
}
 
Example #6
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonBinaryProperty() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "a eq b");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkBinary(jsonMap, "eq", null);

  LinkedTreeMap<String, Object> left = (LinkedTreeMap<String, Object>) jsonMap.get(LEFT);
  checkProperty(left, null, "a");

  LinkedTreeMap<String, Object> right = (LinkedTreeMap<String, Object>) jsonMap.get(RIGHT);
  checkProperty(right, null, "b");
}
 
Example #7
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uri_info,
      final String content_type) throws ODataException
{
   // Gets the `collection` part of the URI.
   EdmEntitySet targetES = uri_info.getTargetEntitySet();
   AbstractEntitySet entityset = Model.getEntitySet(targetES.getName());

   // Validity and security checks.
   if (!entityset.isAuthorized(Security.getCurrentUser()) ||
       uri_info.getNavigationSegments().isEmpty() && !entityset.isTopLevel())
   {
      throw new NotAllowedException();
   }

   // Builds the response.
   KeyPredicate startKP =
         (uri_info.getKeyPredicates().isEmpty()) ? null : uri_info.getKeyPredicates().get(0);

   Map<?, ?> results = Navigator.<Map>navigate(uri_info.getStartEntitySet(), startKP,
         uri_info.getNavigationSegments(), Map.class);

   FilterExpression filter = uri_info.getFilter();
   // Skip, Sort and Filter.
   if (results instanceof SubMap && (filter != null))
   {
      SubMapBuilder smb = ((SubMap) results).getSubMapBuilder();
      smb.setFilter(filter);
      results = smb.build();
   }

   return ODataResponse.entity(results.size()).build();
}
 
Example #8
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonBinaryAdd() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "1d add 2d add 3d add 4d");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkBinary(jsonMap, "add", "Edm.Double");

  LinkedTreeMap<String, Object> left1 = (LinkedTreeMap<String, Object>) jsonMap.get(LEFT);
  checkBinary(left1, "add", "Edm.Double");

  LinkedTreeMap<String, Object> left2 = (LinkedTreeMap<String, Object>) left1.get(LEFT);
  checkBinary(left2, "add", "Edm.Double");

  LinkedTreeMap<String, Object> literal1 = (LinkedTreeMap<String, Object>) left2.get(LEFT);
  checkLiteral(literal1, "Edm.Double", "1");

  LinkedTreeMap<String, Object> literal2 = (LinkedTreeMap<String, Object>) left2.get(RIGHT);
  checkLiteral(literal2, "Edm.Double", "2");

  LinkedTreeMap<String, Object> literal3 = (LinkedTreeMap<String, Object>) left1.get(RIGHT);
  checkLiteral(literal3, "Edm.Double", "3");

  LinkedTreeMap<String, Object> right1 = (LinkedTreeMap<String, Object>) jsonMap.get(RIGHT);
  checkLiteral(right1, "Edm.Double", "4");
}
 
Example #9
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonMethod() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "concat('aa','b')");
  String jsonString = toJson(expression);
  Gson gsonConverter = new Gson();

  LinkedTreeMap<String, Object> jsonMap = gsonConverter.fromJson(jsonString, LinkedTreeMap.class);
  checkMethod(jsonMap, MethodOperator.CONCAT, "Edm.String");

  List<Object> parameter = (List<Object>) jsonMap.get(PARAMETERS);
  checkLiteral((LinkedTreeMap<String, Object>) parameter.get(0), "Edm.String", "aa");
  checkLiteral((LinkedTreeMap<String, Object>) parameter.get(1), "Edm.String", "b");
}
 
Example #10
Source File: CustomerQueryExtension.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public Query getQuery(GetEntitySetUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
  FilterExpression filter = uriInfo.getFilter();
  if(filter != null && filter.getExpressionString().startsWith("name")) {
    throw createApplicationError("Filter on name not allowed.", Locale.ENGLISH);
  }
  return null;
}
 
Example #11
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testToJsonUnary() throws Exception {
  FilterExpression expression = UriParser.parseFilter(null, null, "not 'a'");
  String jsonString = toJson(expression);

  LinkedTreeMap<String, Object> jsonMap = new Gson().fromJson(jsonString, LinkedTreeMap.class);
  checkUnary(jsonMap, UnaryOperator.NOT, null);

  LinkedTreeMap<String, Object> operand = (LinkedTreeMap<String, Object>) jsonMap.get(OPERAND);
  checkLiteral(operand, "Edm.String", "a");
}
 
Example #12
Source File: JPQLSelectStatementBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildQueryWithFilter() throws EdmException, ODataJPARuntimeException {
  OrderByExpression orderByExpression = EasyMock.createMock(OrderByExpression.class);
  FilterExpression filterExpression = null;// getFilterExpressionMockedObj();
  JPQLSelectContext jpqlSelectContextImpl = createSelectContext(orderByExpression, filterExpression);
  jpqlSelectContextImpl.setWhereExpression("E1.soID >= 1234");

  jpqlSelectStatementBuilder = new JPQLSelectStatementBuilder(jpqlSelectContextImpl);

  assertEquals("SELECT E1 FROM SalesOrderHeader E1 WHERE E1.soID >= 1234", jpqlSelectStatementBuilder.build()
      .toString());
}
 
Example #13
Source File: JPQLSelectStatementBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private JPQLSelectContext createSelectContext(final OrderByExpression orderByExpression,
    final FilterExpression filterExpression) throws ODataJPARuntimeException, EdmException {
  // Object Instantiation

  JPQLSelectContext jpqlSelectContextImpl = null;
  GetEntitySetUriInfo getEntitySetView = EasyMock.createMock(GetEntitySetUriInfo.class);

  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  List<SelectItem> selectItemList = null;

  // Setting up the expected value

  EasyMock.expect(getEntitySetView.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EasyMock.expect(getEntitySetView.getOrderBy()).andStubReturn(orderByExpression);
  EasyMock.expect(getEntitySetView.getSelect()).andStubReturn(selectItemList);
  EasyMock.expect(getEntitySetView.getFilter()).andStubReturn(filterExpression);
  EasyMock.replay(getEntitySetView);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.replay(edmEntitySet);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(null);
  EasyMock.expect(edmEntityType.getName()).andStubReturn("SalesOrderHeader");
  EasyMock.replay(edmEntityType);

  JPQLContextBuilder contextBuilder1 = JPQLContext.createBuilder(JPQLContextType.SELECT, getEntitySetView);
  try {
    jpqlSelectContextImpl = (JPQLSelectContext) contextBuilder1.build();
  } catch (ODataJPAModelException e) {
    fail("Model Exception thrown");
  }

  return jpqlSelectContextImpl;
}
 
Example #14
Source File: JPQLSelectContextImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private FilterExpression getFilterExpressionMockedObj(final ExpressionKind leftOperandExpKind,
    final String propertyName) throws EdmException {
  FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class);
  EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER);
  EasyMock.expect(filterExpression.getExpression()).andStubReturn(
      getPropertyExpressionMockedObj(leftOperandExpKind, propertyName));
  EasyMock.replay(filterExpression);
  return filterExpression;
}
 
Example #15
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> boolean appliesFilter(final T data, final FilterExpression filter) throws ODataException {
  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement(getClass().getSimpleName(), "appliesFilter");

  try {
    return data != null && (filter == null || "true".equals(evaluateExpression(data, filter.getExpression())));
  } catch (final RuntimeException e) {
    return false;
  } finally {
    context.stopRuntimeMeasurement(timingHandle);
  }
}
 
Example #16
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> boolean appliesFilter(final EdmEntitySet entitySet, final T data, final FilterExpression filter)
    throws ODataException {
  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement(getClass().getSimpleName(), "appliesFilter");

  try {
    return data != null
        && (filter == null || evaluateExpression(entitySet, data, filter.getExpression()).equals("true"));
  } catch (final RuntimeException e) {
    return false;
  } finally {
    context.stopRuntimeMeasurement(timingHandle);
  }
}
 
Example #17
Source File: FunctionalVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Object visitFilterExpression(FilterExpression fe, String filter, Object exp)
{
   // Exp is a Node<?, Boolean>, returns an ExecutableExpressionTree.
   ExecutableExpressionTree.Node node = ExecutableExpressionTree.Node.class.cast(exp);
   return new ExecutableExpressionTree(node);
}
 
Example #18
Source File: OlingoManager.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int getUsersNumber(FilterExpression filter_expr)
      throws ExceptionVisitExpression, ODataApplicationException
{
   UserSQLVisitor expV = new UserSQLVisitor();
   Object visit = null;
   if (filter_expr != null)
   {
      visit = filter_expr.accept(expV);
   }
   return userService.countUsers((DetachedCriteria) visit);
}
 
Example #19
Source File: OlingoManager.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int getProductsNumber(String uuid, FilterExpression filter_expr)
         throws ExceptionVisitExpression, ODataApplicationException
{
   ProductSQLVisitor expV = new ProductSQLVisitor();
   Object visit = null;

   if (filter_expr != null)
   {
      visit = filter_expr.accept(expV);
   }

   return productService.countProducts((DetachedCriteria) visit, uuid);
}
 
Example #20
Source File: OlingoManager.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int getProductsNumber(FilterExpression filter_expr)
      throws ExceptionVisitExpression, ODataApplicationException
{
   // if no filter, using a count method with a smart cache
   if (filter_expr == null)
   {
      return productService.count();
   }
   return getProductsNumber(null, filter_expr);
}
 
Example #21
Source File: SQLVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Object visitFilterExpression(FilterExpression filter_expression,
      String expression_string, Object expression)
{
   if (expression != null)
   {
      criteria.add((Criterion) expression);
   }
   return criteria;
}
 
Example #22
Source File: CollectionMap.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
private CollectionMap (FilterExpression filter, OrderByExpression order,
   int skip, int top, String parent_id)
{
   this.filter = filter;
   this.orderBy = order;
   this.skip = skip;
   this.top = top;
   this.parentId = parent_id;
}
 
Example #23
Source File: ProductsMap.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Private constructor used by {@link ProductsMap#getSubMapBuilder()}. */
private ProductsMap (FilterExpression filter, OrderByExpression order,
   int skip, int top)
{
   this.filter = filter;
   this.orderBy = order;
   this.skip = skip;
   this.top = top;
}
 
Example #24
Source File: CollectionProductsMap.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Private constructor used by
 * {@link CollectionProductsMap#getSubMapBuilder()}
 */
public CollectionProductsMap (String collection_uuid, FilterExpression filter,
   OrderByExpression order, int skip, int top)
{
   this.collectionUUID = collection_uuid;

   this.filter = filter;
   this.orderBy = order;
   this.skip = skip;
   this.top = top;
}
 
Example #25
Source File: UserMap.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Private constructor used by {@link ProductsMap#getSubMapBuilder()}. */
private UserMap (FilterExpression filter, OrderByExpression order, int skip,
   int top)
{
   this.filter = filter;
   this.orderBy = order;
   this.skip = skip;
   this.top = top;

   hasRole = Security.currentUserHasRole(Role.SYSTEM_MANAGER, Role.USER_MANAGER);
}
 
Example #26
Source File: ODataParser.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public SearchCondition<T> parse(String searchExpression) throws SearchParseException {
    try {
        final T condition = conditionClass.newInstance();
        final FilterExpression expression = parser.parseFilterString(searchExpression);
        final FilterExpressionVisitor visitor = new FilterExpressionVisitor(condition);
        return (SearchCondition< T >)expression.accept(visitor);
    } catch (ODataMessageException | ODataApplicationException
        | InstantiationException | IllegalAccessException ex) {
        throw new SearchParseException(ex);
    }
}
 
Example #27
Source File: ODataDebugResponseWrapperTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void uri() throws Exception {
  final ODataContext context = mockContext(ODataHttpMethod.GET);
  final ODataResponse wrappedResponse = mockResponse(HttpStatusCodes.OK, null, null);

  UriInfo uriInfo = mock(UriInfo.class);
  final FilterExpression filter = UriParser.parseFilter(null, null, "true");
  when(uriInfo.getFilter()).thenReturn(filter);
  final OrderByExpression orderBy = UriParser.parseOrderBy(null, null, "true");
  when(uriInfo.getOrderBy()).thenReturn(orderBy);
  List<ArrayList<NavigationPropertySegment>> expand = new ArrayList<ArrayList<NavigationPropertySegment>>();
  NavigationPropertySegment segment = mock(NavigationPropertySegment.class);
  EdmNavigationProperty navigationProperty = mock(EdmNavigationProperty.class);
  when(navigationProperty.getName()).thenReturn("nav");
  when(segment.getNavigationProperty()).thenReturn(navigationProperty);
  ArrayList<NavigationPropertySegment> segments = new ArrayList<NavigationPropertySegment>();
  segments.add(segment);
  expand.add(segments);
  when(uriInfo.getExpand()).thenReturn(expand);
  SelectItem select1 = mock(SelectItem.class);
  SelectItem select2 = mock(SelectItem.class);
  EdmProperty property = mock(EdmProperty.class);
  when(property.getName()).thenReturn("property");
  when(select1.getProperty()).thenReturn(property);
  when(select2.getProperty()).thenReturn(property);
  when(select2.getNavigationPropertySegments()).thenReturn(segments);
  when(uriInfo.getSelect()).thenReturn(Arrays.asList(select1, select2));

  ODataResponse response = new ODataDebugResponseWrapper(context, wrappedResponse, uriInfo, null,
      ODataDebugResponseWrapper.ODATA_DEBUG_JSON).wrapResponse();
  String entity = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertEquals(EXPECTED.replace("null}}", "null,"
      + "\"uri\":{\"filter\":{\"nodeType\":\"LITERAL\",\"type\":\"Edm.Boolean\",\"value\":\"true\"},"
      + "\"orderby\":{\"nodeType\":\"order collection\","
      + "\"orders\":[{\"nodeType\":\"ORDER\",\"sortorder\":\"asc\","
      + "\"expression\":{\"nodeType\":\"LITERAL\",\"type\":\"Edm.Boolean\",\"value\":\"true\"}}]},"
      + "\"expandSelect\":{\"all\":false,\"properties\":[\"property\"],"
      + "\"links\":[{\"nav\":{\"all\":false,\"properties\":[\"property\"],\"links\":[]}}]}}}}"),
      entity);

  response = new ODataDebugResponseWrapper(context, wrappedResponse, uriInfo, null,
      ODataDebugResponseWrapper.ODATA_DEBUG_HTML).wrapResponse();
  entity = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertTrue(entity.contains("Edm.Boolean"));
  assertTrue(entity.contains("asc"));
}
 
Example #28
Source File: UriInfoImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public FilterExpression getFilter() {
  return filter;
}
 
Example #29
Source File: JsonVisitor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitFilterExpression(final FilterExpression filterExpression, final String expressionString,
    final Object expression) {
  return expression;
}
 
Example #30
Source File: FilterToJsonTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private static String toJson(final FilterExpression expression) throws ExceptionVisitExpression,
    ODataApplicationException {
  return (String) expression.accept(new JsonVisitor());
}