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

The following examples show how to use org.apache.olingo.odata2.api.uri.expression.OrderExpression. 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: SQLVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Object visitOrder(OrderExpression order_expression,
      Object filter_result, SortOrder sort_order)
{
   Order order;
   String property = ((Member) filter_result).getName();
   switch (sort_order)
   {
      case asc:
      {
         order = Order.asc(property);
         break;
      }
      case desc:
      {
         order = Order.desc(property);
         break;
      }
      default:
      {
         throw new UnsupportedOperationException("Unsupported order: " + sort_order);
      }
   }
   return order;
}
 
Example #2
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> void sort(final List<T> data, final OrderByExpression orderBy) {
  Collections.sort(data, new Comparator<T>() {
    @Override
    public int compare(final T entity1, final T entity2) {
      try {
        int result = 0;
        for (final OrderExpression expression : orderBy.getOrders()) {
          String first = evaluateExpression(entity1, expression.getExpression());
          String second = evaluateExpression(entity2, expression.getExpression());

          if (first != null && second != null) {
            result = first.compareTo(second);
          } else if (first == null && second != null) {
            result = 1;
          } else if (first != null && second == null) {
            result = -1;
          }

          if (expression.getSortOrder() == SortOrder.desc) {
            result = -result;
          }

          if (result != 0) {
            break;
          }
        }
        return result;
      } catch (final ODataException e) {
        return 0;
      }
    }
  });
}
 
Example #3
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * @param uriInfo
 * @return
 */
private OrderByExpression mockOrderByExpressions(UriInfo uriInfo) {
  OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class);
  EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(new ArrayList<OrderExpression>());
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression);
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(null);
  return orderbyExpression;
}
 
Example #4
Source File: DebugInfoUri.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public void appendHtml(final Writer writer) throws IOException {
  if (exception != null && exception.getFilterTree() != null) {
    writer.append("<h2>Expression Information</h2>\n")
        .append("<pre class=\"code\">").append(exception.getFilterTree().getUriLiteral())
        .append("</pre>\n");
    // TODO: filter error position, filter tokens, filter tree
  }
  if (filter != null) {
    writer.append("<h2>Filter</h2>\n")
        .append("<ul class=\"expr\"><li>");
    appendExpression(filter.getExpression(), writer);
    writer.append("</li></ul>\n");
  }
  if (orderBy != null) {
    writer.append("<h2>Orderby</h2>\n")
        .append(orderBy.getOrdersCount() == 1 ? "<ul" : "<ol").append(" class=\"expr\">\n");
    for (final OrderExpression order : orderBy.getOrders()) {
      writer.append("<li>");
      appendExpression(order.getExpression(), writer);
      final ExpressionKind kind = order.getExpression().getKind();
      if (kind == ExpressionKind.PROPERTY || kind == ExpressionKind.LITERAL) {
        writer.append("<br />");
      }
      writer.append("<span class=\"order\">")
          .append(order.getSortOrder().toString())
          .append("</span></li>\n");
    }
    writer.append(orderBy.getOrdersCount() == 1 ? "</ul" : "</ol").append(">\n");
  }
  if (expandSelectTree != null) {
    writer.append("<h2>Expand/Select</h2>\n");
    appendExpandSelect(expandSelectTree, writer);
  }
}
 
Example #5
Source File: JsonVisitor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitOrder(final OrderExpression orderExpression, final Object filterResult,
    final SortOrder sortOrder) {
  try {
    StringWriter writer = new StringWriter();
    JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);
    jsonStreamWriter.beginObject().namedStringValueRaw("nodeType", orderExpression.getKind().toString()).separator()
        .namedStringValueRaw("sortorder", sortOrder.toString()).separator().name("expression").unquotedValue(
            filterResult.toString()).endObject();
    writer.flush();
    return writer.toString();
  } catch (final IOException e) {
    return null;
  }
}
 
Example #6
Source File: OrderByExpressionImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public Object accept(final ExpressionVisitor visitor) throws ExceptionVisitExpression, ODataApplicationException {
  ArrayList<Object> retParameters = new ArrayList<Object>();
  for (OrderExpression order : orders) {
    Object retParameter = order.accept(visitor);
    retParameters.add(retParameter);
  }

  Object ret = visitor.visitOrderByExpression(this, orderbyString, retParameters);
  return ret;
}
 
Example #7
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> void sort(final EdmEntitySet entitySet, final List<T> data, final OrderByExpression orderBy) {
  Collections.sort(data, new Comparator<T>() {
    @Override
    public int compare(final T entity1, final T entity2) {
      try {
        int result = 0;
        for (final OrderExpression expression : orderBy.getOrders()) {
          String first = evaluateExpression(entitySet, entity1, expression.getExpression());
          String second = evaluateExpression(entitySet, entity2, expression.getExpression());

          if (first != null && second != null) {
            result = first.compareTo(second);
          } else if (first == null && second != null) {
            result = 1;
          } else if (first != null && second == null) {
            result = -1;
          }

          if (expression.getSortOrder() == SortOrder.desc) {
            result = -result;
          }

          if (result != 0) {
            break;
          }
        }
        return result;
      } catch (final ODataException e) {
        return 0;
      }
    }
  });
}
 
Example #8
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private UriInfo mockURIInfoForEntitySet(EdmMapping mapping, String methodName) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty());
  
  EdmProperty edmProperty1 = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty1.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty1());
  
  EdmNavigationProperty navEdmProperty = EasyMock.createMock(EdmNavigationProperty.class);
  EasyMock.expect(navEdmProperty.getMapping()).andStubReturn((EdmMapping)mockNavEdmMappingForProperty());
  
  OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class);
  List<OrderExpression> orders = new ArrayList<OrderExpression>();
  EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(orders);
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression);
  FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class);
  MethodExpression commonExpression = EasyMock.createMock(MethodExpression.class);
  EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.METHOD);
  PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class);
  LiteralExpression literalExp = EasyMock.createMock(LiteralExpression.class);
  MemberExpression memberExp = EasyMock.createMock(MemberExpression.class);
  List<CommonExpression> parameterList = new ArrayList<CommonExpression>();
  
  PropertyExpression navPropExp = EasyMock.createMock(PropertyExpression.class);
  if ("substringof".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.SUBSTRINGOF);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
  } else if ("startsWith".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.STARTSWITH);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
  } else if ("endsWith".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.ENDSWITH);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
  } else if ("substringof_1".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.SUBSTRINGOF);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
    EasyMock.expect(memberExp.getKind()).andStubReturn(ExpressionKind.MEMBER);
    EasyMock.expect(memberExp.getProperty()).andStubReturn(propExp);
    
    EasyMock.expect(memberExp.getPath()).andStubReturn(navPropExp);
    EasyMock.expect(navPropExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    EasyMock.expect(navPropExp.getEdmProperty()).andStubReturn(navEdmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty1);
    
    parameterList.add(propExp);
  }
  
  EasyMock.expect(commonExpression.getParameters()).andStubReturn(parameterList);
  
  EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression);
  EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER);
  EasyMock.expect(filterExpression.getExpressionString()).andStubReturn("substringof('a.b.c',Id)");
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression);
  EasyMock.replay(edmEntityType, edmEntitySet, orderbyExpression, filterExpression, 
      commonExpression, literalExp, propExp, uriInfo, edmProperty, memberExp,
      edmProperty1, navEdmProperty);
  return uriInfo;

}
 
Example #9
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private UriInfo mockURIInfoForEntitySetWithBinaryFilterExpression
(EdmMapping mapping, String methodName) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty());
  OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class);
  List<OrderExpression> orders = new ArrayList<OrderExpression>();
  EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(orders);
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression);
  FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class);
  BinaryExpression commonExpression = EasyMock.createMock(BinaryExpression.class);
  EasyMock.expect(commonExpression.getOperator()).andStubReturn(BinaryOperator.EQ);
  EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.BINARY);
  MethodExpression methodExp = EasyMock.createMock(MethodExpression.class);
  EasyMock.expect(commonExpression.getLeftOperand()).andStubReturn(methodExp);
  EdmSimpleType type = EasyMock.createMock(EdmSimpleType.class);;
  EasyMock.expect(methodExp.getEdmType()).andStubReturn(type );
  
  LiteralExpression literalValueExp = EasyMock.createMock(LiteralExpression.class);
  EasyMock.expect(commonExpression.getRightOperand()).andStubReturn(literalValueExp);
  EasyMock.expect(literalValueExp.getUriLiteral()).andStubReturn("'a%.b*.c'");
  EasyMock.expect(literalValueExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
  EasyMock.expect(literalValueExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
  
  PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class);
  LiteralExpression literalExp1 = EasyMock.createMock(LiteralExpression.class);
  LiteralExpression literalExp2 = EasyMock.createMock(LiteralExpression.class);
  List<CommonExpression> parameterList = new ArrayList<CommonExpression>();
  if ("substring".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.SUBSTRING);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(3);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp1.getUriLiteral()).andStubReturn("1");
    EasyMock.expect(literalExp1.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp1.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance());
    parameterList.add(literalExp1);
    EasyMock.expect(literalExp2.getUriLiteral()).andStubReturn("2");
    EasyMock.expect(literalExp2.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp2.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance());
    parameterList.add(literalExp2);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  } else if ("toLower".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOLOWER);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  } else if ("toUpper".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOUPPER);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  }
  EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression);
  EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER);
  EasyMock.expect(filterExpression.getExpressionString()).andStubReturn("substring(CompanyName,1,2) eq 'a.b.c'");
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression);
  EasyMock.replay(edmEntityType, edmEntitySet, orderbyExpression, filterExpression, 
      commonExpression, literalExp1, literalExp2, propExp, uriInfo, edmProperty, 
      methodExp, literalValueExp);
  return uriInfo;

}
 
Example #10
Source File: OrderByExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public OrderByExpressionImpl(final String orderbyString) {
  this.orderbyString = orderbyString;
  orders = new ArrayList<OrderExpression>();
}
 
Example #11
Source File: OrderByExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public List<OrderExpression> getOrders() {
  return orders;
}
 
Example #12
Source File: OrderByExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public void addOrder(final OrderExpression orderNode) {
  orders.add(orderNode);
}
 
Example #13
Source File: VisitorTool.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitOrder(final OrderExpression orderExpression, final Object filterResult,
    final SortOrder sortOrder) {
  return "{o(" + filterResult + ", " + sortOrder.toString() + ")}";
}
 
Example #14
Source File: ODataParser.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitOrder(OrderExpression orderExpression, Object filterResult, SortOrder sortOrder) {
    throw new SearchParseException("Unsupported operation visitOrder: " + orderExpression
        + "," + filterResult + "," + sortOrder);
}
 
Example #15
Source File: FunctionalVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Called for each fields in the $orderby option.
 * returns a {@linkplain ExecutableExpressionTree.Node#createDuoNode(Transformer,
 * ExecutableExpressionTree.Node, ExecutableExpressionTree.Node) DuoNode}
 * {@link ExecutableExpressionTree.Node}
 * @param oe OrderExpression
 * @param filter an {@link ExecutableExpressionTree.Node}.
 * @param sort_order `asc` or `desc`.
 * @return an {@link ExecutableExpressionTree.Node}
 */
@Override
public Object visitOrder(OrderExpression oe, Object filter, SortOrder sort_order)
{
   ExecutableExpressionTree.Node param = ExecutableExpressionTree.Node.class.cast(filter);
   Transformer cmp = new ComparatorTransformer(sort_order == SortOrder.desc);

   return ExecutableExpressionTree.Node.createDuoNode(cmp, param, param);
}