Java Code Examples for org.apache.olingo.server.api.uri.queryoption.CountOption#getValue()

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.CountOption#getValue() . 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: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeExpandedNavigationProperty(final ServiceMetadata metadata,
    final EdmNavigationProperty property, final Link navigationLink,
    final ExpandOption innerExpand, final Integer toDepth, 
    final SelectOption innerSelect, final CountOption coutOption, 
    final boolean writeNavigationCount, final boolean writeOnlyRef,final String xml10InvalidCharReplacement,
    final Set<String> ancestors, String name,
    final XMLStreamWriter writer) throws XMLStreamException, SerializerException {
  if (property.isCollection()) {
    if (navigationLink != null && navigationLink.getInlineEntitySet() != null) {
      writer.writeStartElement(ATOM, Constants.ATOM_ELEM_FEED, NS_ATOM);
      if (writeNavigationCount) {
        writeCount(navigationLink.getInlineEntitySet(), writer);
      } else {
        if (coutOption != null && coutOption.getValue()) {
          writeCount(navigationLink.getInlineEntitySet(), writer);
        }
        writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand, toDepth,
            innerSelect, xml10InvalidCharReplacement, writer, writeOnlyRef, name, ancestors);
      }
      writer.writeEndElement();
    }
  } else {
    if (navigationLink != null && navigationLink.getInlineEntity() != null) {
      writeEntity(metadata, property.getType(), navigationLink.getInlineEntity(), null,
          innerExpand, toDepth, innerSelect, xml10InvalidCharReplacement, writer, 
          false, writeOnlyRef, name, ancestors);
    }
  }
}
 
Example 2
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private List<Entity> applyCountQueryOption(EntityCollection entityCollection, List<Entity> modifiedEntityList, 
    CountOption countOption) {

  // handle $count: always return the original number of entities, without considering $top and $skip
  if (countOption != null) {
    boolean isCount = countOption.getValue();
    if (isCount) {
      entityCollection.setCount(modifiedEntityList.size());
    }
  }
  
  return modifiedEntityList;
}
 
Example 3
Source File: ProductsEntityCollectionProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
    // 1st we have retrieve the requested EntitySet from the uriInfo object (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet)resourcePaths.get(0); // in our example, the first segment is the EntitySet
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName
    // it has to be delivered as EntitySet object
    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());
        }
    }

    applyQueryOptions(uriInfo, entityList, returnEntityCollection);

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

    // 4th: Now 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().id(id)
            .count(countOption)
            .contextURL(contextUrl)
            .build();

    SerializerResult serializerResult = serializer.entityCollection(serviceMetadata,
                                                                    edmEntityType,
                                                                    returnEntityCollection,
                                                                    opts);
    InputStream serializedContent = serializerResult.getContent();

    // Finally: 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 4
Source File: CountHandler.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static void applyCountSystemQueryOption(final CountOption countOption, final EntityCollection entitySet) {
  if (countOption != null && countOption.getValue()) {
    entitySet.setCount(entitySet.getEntities().size());
  }
}
 
Example 5
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void writeExpandedNavigationProperty(
    final ServiceMetadata metadata, final EdmNavigationProperty property,
    final Link navigationLink, final ExpandOption innerExpand,
    Integer toDepth, final SelectOption innerSelect, final CountOption innerCount,
    final boolean writeOnlyCount, final boolean writeOnlyRef, final Set<String> ancestors,
    String name, final JsonGenerator json) throws IOException, SerializerException, DecoderException {

  if (property.isCollection()) {
    if (writeOnlyCount) {
      if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
        writeInlineCount(property.getName(), 0, json);
      } else {
        writeInlineCount(property.getName(), navigationLink.getInlineEntitySet().getCount(), json);
      }
    } else {
      if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
        if (innerCount != null && innerCount.getValue()) {
          writeInlineCount(property.getName(), 0, json);
        }
        json.writeFieldName(property.getName());
        json.writeStartArray();
        json.writeEndArray();
      } else {
        if (innerCount != null && innerCount.getValue()) {
          writeInlineCount(property.getName(), navigationLink.getInlineEntitySet().getCount(), json);
        }
        json.writeFieldName(property.getName());
        writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand, toDepth,
            innerSelect, writeOnlyRef, ancestors, name, json);
      }
    }
  } else {
    json.writeFieldName(property.getName());
    if (navigationLink == null || navigationLink.getInlineEntity() == null) {
      json.writeNull();
    } else {
      writeEntity(metadata, property.getType(), navigationLink.getInlineEntity(), null,
          innerExpand, toDepth, innerSelect, writeOnlyRef, ancestors, name, json);
    }
  }
}
 
Example 6
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 7
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 2 votes vote down vote up
/**
 * This method applies count query option to the given entity collection.
 *
 * @param countOption Count option
 * @param entitySet   Entity collection
 */
public static void applyCountSystemQueryOption(final CountOption countOption, final EntityCollection entitySet) {
    if (countOption.getValue()) {
        entitySet.setCount(entitySet.getEntities().size());
    }
}