protected Entity readEntity()

in lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java [143:220]


  protected Entity readEntity(final UriInfoResource uriInfo, final boolean ignoreLastNavigation)
      throws ODataApplicationException {
    final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();

    Entity entity = null;
    if (resourcePaths.get(0) instanceof UriResourceEntitySet) {
      final UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
      EdmEntitySet entitySet = getEntitySetBasedOnTypeCast(uriResource);
      entity = dataProvider.read(entitySet, uriResource.getKeyPredicates());
    }else if (resourcePaths.get(0) instanceof UriResourceSingleton) {
      final UriResourceSingleton uriResource = (UriResourceSingleton) resourcePaths.get(0);
      entity = dataProvider.read( uriResource.getSingleton());
    } else if (resourcePaths.get(0) instanceof UriResourceFunction) {
      final UriResourceFunction uriResource = (UriResourceFunction) resourcePaths.get(0);
      final EdmFunction function = uriResource.getFunction();
      if (function.getReturnType().getType() instanceof EdmEntityType) {
        final List<UriParameter> key = uriResource.getKeyPredicates();
        if (key.isEmpty()) {
          if (uriResource.isCollection()) { // handled in readEntityCollection()
            return null;
          } else {
            entity = dataProvider.readFunctionEntity(function, uriResource.getParameters(), uriInfo);
          }
        } else {
          entity = dataProvider.read((EdmEntityType) function.getReturnType().getType(),
              dataProvider.readFunctionEntityCollection(function, uriResource.getParameters(), uriInfo),
              key);
        }
      } else {
        return null;
      }
    }
    if (entity == null) {
      throw new ODataApplicationException("Nothing found.", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
    }

    int readAtMostNavigations = resourcePaths.size();
    if (ignoreLastNavigation) {
      readAtMostNavigations = 0;
      for (int i = 1; i < resourcePaths.size(); i++) {
        if (resourcePaths.get(i) instanceof UriResourceNavigation) {
          readAtMostNavigations++;
        } else {
          break;
        }
      }
    }

    int navigationCount = 0;
	int navigationResCount = getNavigationResourceCount(resourcePaths);
    Link previous = null;
    while (++navigationCount < readAtMostNavigations
        && resourcePaths.get(navigationCount) instanceof UriResourceNavigation) {
      final UriResourceNavigation uriNavigationResource = (UriResourceNavigation) resourcePaths.get(navigationCount);
      final EdmNavigationProperty navigationProperty = uriNavigationResource.getProperty();
      final List<UriParameter> key = uriNavigationResource.getKeyPredicates();
      if (navigationProperty.isCollection() && key.isEmpty()) { // handled in readEntityCollection()
        return entity;
      }
      final Link link = entity.getNavigationLink(navigationProperty.getName());
      entity = link == null ? null :
          key.isEmpty() ?
              link.getInlineEntity() :
              dataProvider.read(navigationProperty.getType(), link.getInlineEntitySet(), key);
      EdmEntityType edmEntityType = getEntityTypeBasedOnNavPropertyTypeCast(uriNavigationResource);
      entity = edmEntityType != null ? dataProvider.readDataFromEntity(edmEntityType, key) : entity;
      if (entity == null) {
        if (key.isEmpty() && (previous != null || navigationResCount == 1)) {
          throw new ODataApplicationException("No Content", HttpStatusCode.NO_CONTENT.getStatusCode(), Locale.ROOT);
        } else {
          throw new ODataApplicationException("Not Found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
        }
      }
	  previous = link;
    }

    return entity;
  }