org.apache.olingo.server.api.uri.queryoption.TopOption Java Examples

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.TopOption. 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: ExpandSystemQueryOptionHandler.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void applyOptionsToEntityCollection(final EntityCollection entitySet,
    final EdmBindingTarget edmBindingTarget,
    final FilterOption filterOption, final OrderByOption orderByOption, final CountOption countOption,
    final SkipOption skipOption, final TopOption topOption, final ExpandOption expandOption,
    final UriInfoResource uriInfo, final Edm edm)
    throws ODataApplicationException {

  FilterHandler.applyFilterSystemQuery(filterOption, entitySet, uriInfo, edm);
  OrderByHandler.applyOrderByOption(orderByOption, entitySet, uriInfo, edm);
  CountHandler.applyCountSystemQueryOption(countOption, entitySet);
  SkipHandler.applySkipSystemQueryHandler(skipOption, entitySet);
  TopHandler.applyTopSystemQueryOption(topOption, entitySet);

  // Apply nested expand system query options to remaining entities
  if (expandOption != null) {
    for (final Entity entity : entitySet.getEntities()) {
      applyExpandOptionToEntity(entity, edmBindingTarget, expandOption, uriInfo, edm);
    }
  }
}
 
Example #2
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private List<Entity> applyTopQueryOption(List<Entity> entityList, TopOption topOption)
    throws ODataApplicationException {

  if (topOption != null) {
    int topNumber = topOption.getValue();
    if (topNumber >= 0) {
      if(topNumber <= entityList.size()) {
        entityList = entityList.subList(0, topNumber);
      }  // else the client has requested more entities than available => return what we have
    } else {
      throw new ODataApplicationException("Invalid value for $top", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }
  
  return entityList;
}
 
Example #3
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method applies top query option to the given entity collection.
 *
 * @param topOption Top option
 * @param entitySet Entity Collection
 * @throws ODataApplicationException
 */
public static void applyTopSystemQueryOption(final TopOption topOption, final EntityCollection entitySet)
        throws ODataApplicationException {
    if (topOption.getValue() >= 0) {
        reduceToSize(entitySet, topOption.getValue());
    } else {
        throw new ODataApplicationException("Top value must be positive",
                                            HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
}
 
Example #4
Source File: TopHandler.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static void applyTopSystemQueryOption(final TopOption topOption, final EntityCollection entitySet)
    throws ODataApplicationException {

  if (topOption != null) {
    if (topOption.getValue() >= 0) {
      reduceToSize(entitySet, topOption.getValue());
    } else {
      throw new ODataApplicationException("Top value must be positive", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }
}
 
Example #5
Source File: ProductsEntityCollectionProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private static EntityCollection applyQueryOptions(UriInfo uriInfo, List<Entity> entityList, EntityCollection returnEntityCollection) throws ODataApplicationException {
    // handle $skip
    SkipOption skipOption = uriInfo.getSkipOption();
    if (skipOption != null) {
        int skipNumber = skipOption.getValue();
        if (skipNumber >= 0) {
            if (skipNumber <= entityList.size()) {
                entityList = entityList.subList(skipNumber, entityList.size());
            } else {
                // The client skipped all entities
                entityList.clear();
            }
        } else {
            throw new ODataApplicationException("Invalid value for $skip", HttpStatusCode.BAD_REQUEST.getStatusCode(),
                                                Locale.ROOT);
        }
    }

    // handle $top
    TopOption topOption = uriInfo.getTopOption();
    if (topOption != null) {
        int topNumber = topOption.getValue();
        if (topNumber >= 0) {
            if (topNumber <= entityList.size()) {
                entityList = entityList.subList(0, topNumber);
            } // else the client has requested more entities than available => return what we have
        } else {
            throw new ODataApplicationException("Invalid value for $top", HttpStatusCode.BAD_REQUEST.getStatusCode(),
                                                Locale.ROOT);
        }
    }

    // handle $filter
    FilterOption filterOption = uriInfo.getFilterOption();
    if(filterOption != null) {
        // Apply $filter system query option
        try {
              Iterator<Entity> entityIterator = entityList.iterator();

              // Evaluate the expression for each entity
              // If the expression is evaluated to "true", keep the entity otherwise remove it from the entityList
              while (entityIterator.hasNext()) {
                  // To evaluate the the expression, create an instance of the Filter Expression Visitor and pass
                  // the current entity to the constructor
                  Entity currentEntity = entityIterator.next();
                  Expression filterExpression = filterOption.getExpression();
                  FilterExpressionVisitor expressionVisitor = new FilterExpressionVisitor(currentEntity);

                  // Start evaluating the expression
                  Object visitorResult = filterExpression.accept(expressionVisitor);

                  // The result of the filter expression must be of type Edm.Boolean
                  if(visitorResult instanceof Boolean) {
                      if(!Boolean.TRUE.equals(visitorResult)) {
                        // The expression evaluated to false (or null), so we have to remove the currentEntity from entityList
                        entityIterator.remove();
                      }
                  } else {
                      throw new ODataApplicationException("A filter expression must evaulate to type Edm.Boolean",
                          HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
                  }
              }

            } catch (ExpressionVisitException e) {
              throw new ODataApplicationException("Exception in filter evaluation",
                  HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH, e);
            }
    }

    // after applying the system query options, create the EntityCollection based on the reduced list
    for (Entity entity : entityList) {
        returnEntityCollection.getEntities().add(entity);
    }

    // apply $orderby
    applyOrderby(uriInfo, returnEntityCollection);

    return returnEntityCollection;
}
 
Example #6
Source File: DebugTabUri.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void appendCommonJsonObjects(JsonGenerator gen,
    final CountOption countOption, final SkipOption skipOption, final TopOption topOption,
    final FilterOption filterOption, final OrderByOption orderByOption,
    final SelectOption selectOption, final ExpandOption expandOption, final SearchOption searchOption,
    final ApplyOption applyOption)
    throws IOException {
  if (countOption != null) {
    gen.writeBooleanField("isCount", countOption.getValue());
  }

  if (skipOption != null) {
    gen.writeNumberField("skip", skipOption.getValue());
  }

  if (topOption != null) {
    gen.writeNumberField("top", topOption.getValue());
  }

  if (filterOption != null) {
    gen.writeFieldName("filter");
    appendExpressionJson(gen, filterOption.getExpression());
  }

  if (orderByOption != null && !orderByOption.getOrders().isEmpty()) {
    gen.writeFieldName("orderby");
    gen.writeStartObject();
    gen.writeStringField("nodeType", "orderCollection");
    gen.writeFieldName("orders");
    appendOrderByItemsJson(gen, orderByOption.getOrders());
    gen.writeEndObject();
  }

  if (selectOption != null && !selectOption.getSelectItems().isEmpty()) {
    gen.writeFieldName("select");
    appendSelectedPropertiesJson(gen, selectOption.getSelectItems());
  }

  if (expandOption != null && !expandOption.getExpandItems().isEmpty()) {
    gen.writeFieldName("expand");
    appendExpandedPropertiesJson(gen, expandOption.getExpandItems());
  }

  if (searchOption != null) {
    gen.writeFieldName("search");
    appendSearchJson(gen, searchOption.getSearchExpression());
  }

  if (applyOption != null) {
    gen.writeFieldName("apply");
    appendApplyItemsJson(gen, applyOption.getApplyItems());
  }
}
 
Example #7
Source File: ExpandItemImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public TopOption getTopOption() {
  return topOption;
}
 
Example #8
Source File: UriInfoImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public TopOption getTopOption() {
  return (TopOption) systemQueryOptions.get(SystemQueryOptionKind.TOP);
}
 
Example #9
Source File: RequestURLHierarchyVisitor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(TopOption option) {
}
 
Example #10
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType responseFormat) throws ODataApplicationException, SerializerException {

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  // in our example, the first segment is the EntitySet
  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
  EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

  // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
  EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

  // 3rd: apply System Query Options
  // modify the result set according to the query options, specified by the end user
  List<Entity> entityList = entityCollection.getEntities();
  EntityCollection returnEntityCollection = new EntityCollection();

  // handle $count: always return the original number of entities, without considering $top and $skip
  CountOption countOption = uriInfo.getCountOption();
  if (countOption != null) {
    boolean isCount = countOption.getValue();
    if (isCount) {
      returnEntityCollection.setCount(entityList.size());
    }
  }
  
  // handle $skip
  SkipOption skipOption = uriInfo.getSkipOption();
  if (skipOption != null) {
    int skipNumber = skipOption.getValue();
    if (skipNumber >= 0) {
      if(skipNumber <= entityList.size()) {
        entityList = entityList.subList(skipNumber, entityList.size());
      } else {
        // The client skipped all entities
        entityList.clear();
      }
    } else {
      throw new ODataApplicationException("Invalid value for $skip", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }
  
  // handle $top
  TopOption topOption = uriInfo.getTopOption();
  if (topOption != null) {
    int topNumber = topOption.getValue();
    if (topNumber >= 0) {
      if(topNumber <= entityList.size()) {
        entityList = entityList.subList(0, topNumber);
      }  // else the client has requested more entities than available => return what we have
    } else {
      throw new ODataApplicationException("Invalid value for $top", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }

  // after applying the system query options, create the EntityCollection based on the reduced list
  for (Entity entity : entityList) {
    returnEntityCollection.getEntities().add(entity);
  }

  // 4th: create a serializer based on the requested format (json)
  ODataSerializer serializer = odata.createSerializer(responseFormat);

  // and serialize the content: transform from the EntitySet object to InputStream
  EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  EntityCollectionSerializerOptions opts =
      EntityCollectionSerializerOptions.with().contextURL(contextUrl).id(id).count(countOption).build();
  SerializerResult serializerResult =
      serializer.entityCollection(serviceMetadata, edmEntityType, returnEntityCollection, opts);
  InputStream serializedContent = serializerResult.getContent();

  // 5th: configure the response object: set the body, headers and status code
  response.setContent(serializedContent);
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #11
Source File: UriInfoResource.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $top option
 */
TopOption getTopOption();
 
Example #12
Source File: UriInfoAll.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $top option
 */
TopOption getTopOption();
 
Example #13
Source File: UriInfoCrossjoin.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $top option
 */
TopOption getTopOption();
 
Example #14
Source File: RequestURLVisitor.java    From olingo-odata4 with Apache License 2.0 votes vote down vote up
void visit(TopOption option);