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

The following examples show how to use org.apache.olingo.odata2.api.uri.expression.SortOrder. 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: TestParser.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void orderBy() {

  GetPTO("name").aSerialized("{oc({o(name, asc)})}");
  GetPTO("name asc").aSerialized("{oc({o(name, asc)})}");
  GetPTO("name desc").aSerialized("{oc({o(name, desc)})}");
  GetPTO("name     asc").aSerialized("{oc({o(name, asc)})}");
  GetPTO("name     desc").aSerialized("{oc({o(name, desc)})}");

  GetPTO("name, test").aSerialized("{oc({o(name, asc)},{o(test, asc)})}");
  GetPTO("name   ,    test").aSerialized("{oc({o(name, asc)},{o(test, asc)})}");

  GetPTO("name, test asc").aSerialized("{oc({o(name, asc)},{o(test, asc)})}");

  GetPTO("name asc, test").aSerialized("{oc({o(name, asc)},{o(test, asc)})}");

  GetPTO("name asc, test asc").aSerialized("{oc({o(name, asc)},{o(test, asc)})}");
  GetPTO("name, test desc").aSerialized("{oc({o(name, asc)},{o(test, desc)})}");
  GetPTO("name desc, test").aSerialized("{oc({o(name, desc)},{o(test, asc)})}");
  GetPTO("name desc, test desc").aSerialized("{oc({o(name, desc)},{o(test, desc)})}");

  GetPTO("'name', 77").order(1).aSortOrder(SortOrder.asc);
  GetPTO("'name', 77 desc").root().order(0).aSortOrder(SortOrder.asc).aExpr().aEdmType(EdmString.getInstance())
      .root().order(1).aSortOrder(SortOrder.desc).aExpr().aEdmType(Uint7.getInstance());

}
 
Example #3
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 #4
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 #5
Source File: ParserTool.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public ParserTool aSortOrder(final SortOrder orderType) {
  String info = "GetSortOrder(" + expression + ")-->";

  aKind(ExpressionKind.ORDER);
  assertEquals(info, orderType, ((OrderExpressionImpl) curNode).getSortOrder());
  return this;
}
 
Example #6
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 #7
Source File: OrderByParserImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public OrderByExpression parseOrderByString(final String orderByExpression) throws ExpressionParserException,
    ExpressionParserInternalError {
  curExpression = orderByExpression;
  OrderByExpressionImpl orderCollection = new OrderByExpressionImpl(curExpression);

  try {
    tokenList = new Tokenizer(orderByExpression).tokenize(); // throws TokenizerMessage
  } catch (TokenizerException tokenizerException) {
    throw FilterParserExceptionImpl.createERROR_IN_TOKENIZER(tokenizerException, curExpression);
  }

  while (true) {
    CommonExpression node = null;
    try {
      CommonExpression nodeLeft = readElement(null);
      node = readElements(nodeLeft, 0);
    } catch (ExpressionParserException expressionException) {
      expressionException.setFilterTree(orderCollection);
      throw expressionException;
    }

    OrderExpressionImpl orderNode = new OrderExpressionImpl(node);

    // read the sort order
    Token token = tokenList.lookToken();
    if (token == null) {
      orderNode.setSortOrder(SortOrder.asc);
    } else if ((token.getKind() == TokenKind.LITERAL) && ("asc".equals(token.getUriLiteral()))) {
      orderNode.setSortOrder(SortOrder.asc);
      tokenList.next();
      token = tokenList.lookToken();
    } else if ((token.getKind() == TokenKind.LITERAL) && ("desc".equals(token.getUriLiteral()))) {
      orderNode.setSortOrder(SortOrder.desc);
      tokenList.next();
      token = tokenList.lookToken();
    } else if (token.getKind() == TokenKind.COMMA) {
      orderNode.setSortOrder(SortOrder.asc);
    } else {
      // Tested with TestParserExceptions.TestOPMparseOrderByString CASE 1
      throw FilterParserExceptionImpl.createINVALID_SORT_ORDER(token, curExpression);
    }

    orderCollection.addOrder(orderNode);

    // ls_token may be a ',' or empty.
    if (token == null) {
      break;
    } else if (token.getKind() == TokenKind.COMMA) {
      Token oldToken = token;
      tokenList.next();
      token = tokenList.lookToken();

      if (token == null) {
        // Tested with TestParserExceptions.TestOPMparseOrderByString CASE 2
        throw FilterParserExceptionImpl.createEXPRESSION_EXPECTED_AFTER_POS(oldToken, curExpression);
      }
    } else { // e.g. in case $orderby=String asc a

      throw FilterParserExceptionImpl.createCOMMA_OR_END_EXPECTED_AT_POS(token, curExpression);
    }

  }
  return orderCollection;
}
 
Example #8
Source File: OrderExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public SortOrder getSortOrder() {
  return orderType;
}
 
Example #9
Source File: OrderExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
void setSortOrder(final SortOrder orderType) {
  this.orderType = orderType;
}
 
Example #10
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 #11
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 #12
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);
}