Java Code Examples for org.apache.olingo.commons.api.edm.EdmNavigationProperty#getType()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmNavigationProperty#getType() . 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: EdmNavigationPropertyImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public EdmNavigationProperty getPartner() {
  if (partnerNavigationProperty == null) {
    String partner = navigationProperty.getPartner();
    if (partner != null) {
      EdmStructuredType type = getType();
      EdmNavigationProperty property = null;
      final String[] split = partner.split("/");
      for (String element : split) {
        property = type.getNavigationProperty(element);
        if (property == null) {
          throw new EdmException("Cannot find navigation property with name: " + element
              + " at type " + type.getName());
        }
        type = property.getType();
      }
      partnerNavigationProperty = property;
    }
  }
  return partnerNavigationProperty;
}
 
Example 2
Source File: EdmNavigationPropertyImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void navigationProperty() throws Exception {
  CsdlEdmProvider provider = mock(CsdlEdmProvider.class);
  EdmProviderImpl edm = new EdmProviderImpl(provider);
  final FullQualifiedName entityTypeName = new FullQualifiedName("ns", "entity");
  CsdlEntityType entityTypeProvider = new CsdlEntityType();
  entityTypeProvider.setKey(Collections.<CsdlPropertyRef> emptyList());
  when(provider.getEntityType(entityTypeName)).thenReturn(entityTypeProvider);
  CsdlNavigationProperty propertyProvider = new CsdlNavigationProperty();
  propertyProvider.setType(entityTypeName);
  propertyProvider.setNullable(false);
  EdmNavigationProperty property = new EdmNavigationPropertyImpl(edm, propertyProvider);
  assertFalse(property.isCollection());
  assertFalse(property.isNullable());
  EdmType type = property.getType();
  assertEquals(EdmTypeKind.ENTITY, type.getKind());
  assertEquals("ns", type.getNamespace());
  assertEquals("entity", type.getName());
  assertNull(property.getReferencingPropertyName("referencedPropertyName"));
  assertNull(property.getPartner());
  assertFalse(property.containsTarget());

  // Test caching
  EdmType cachedType = property.getType();
  assertTrue(type == cachedType);
}
 
Example 3
Source File: EdmNavigationPropertyImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test(expected = EdmException.class)
public void navigationPropertyWithNonExistentType() throws Exception {
  EdmProviderImpl edm = mock(EdmProviderImpl.class);
  CsdlNavigationProperty propertyProvider = new CsdlNavigationProperty();
  EdmNavigationProperty property = new EdmNavigationPropertyImpl(edm, propertyProvider);
  property.getType();
}
 
Example 4
Source File: ODataBinderImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmType findEntityType(
    final String entitySetOrSingletonOrType, final EdmEntityContainer container) {

  EdmType type = null;

  final String firstToken = StringUtils.substringBefore(entitySetOrSingletonOrType, "/");
  EdmBindingTarget bindingTarget = container.getEntitySet(firstToken);
  if (bindingTarget == null) {
    bindingTarget = container.getSingleton(firstToken);
  }
  if (bindingTarget != null) {
    type = bindingTarget.getEntityType();
  }

  if (entitySetOrSingletonOrType.indexOf('/') != -1) {
    final String[] splitted = entitySetOrSingletonOrType.split("/");
    if (splitted.length > 1) {
      for (int i = 1; i < splitted.length && type != null; i++) {
        final EdmNavigationProperty navProp = ((EdmStructuredType) type).getNavigationProperty(splitted[i]);
        if (navProp == null) {
          EdmProperty property = ((EdmStructuredType) type).getStructuralProperty(splitted[i]);
          if (property != null) {
            type = property.getType();
          } else {
            type = null;
          }
        } else {
          type = navProp.getType();
        }
      }
    }
  }

  return type;
}
 
Example 5
Source File: AbstractUtility.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public NavPropertyBindingDetails getNavigationBindingDetails(
        final EdmStructuredType sourceEntityType, final EdmNavigationProperty property) {

  if (property.containsTarget()) {
    return new NavPropertyContainsTarget(edm, property.getType());
  }

  try {
    return getNavigationBindings(sourceEntityType, property);
  } catch (Exception e) {
    // maybe source entity type without entity set ...
    return getNavigationBindings(property.getType(), property.getName());
  }
}
 
Example 6
Source File: ODataBinderImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
/**
 * Infer type name from various sources of information including Edm and context URL, if available.
 *
 * @param candidateTypeName type name as provided by the service
 * @param contextURL context URL
 * @param metadataETag metadata ETag
 * @return Edm type information
 */
private EdmType findType(final String candidateTypeName, final ContextURL contextURL, final String metadataETag) {
  EdmType type = null;

  if (client instanceof EdmEnabledODataClient) {
    final Edm edm = ((EdmEnabledODataClient) client).getEdm(metadataETag);
    if (StringUtils.isNotBlank(candidateTypeName)) {
      type = edm.getEntityType(new FullQualifiedName(candidateTypeName));
    }
    if (type == null && contextURL != null) {
      if (contextURL.getDerivedEntity() == null) {
        for (EdmSchema schema : edm.getSchemas()) {
          final EdmEntityContainer container = schema.getEntityContainer();
          if (container != null) {
            final EdmType structuredType = findEntityType(contextURL.getEntitySetOrSingletonOrType(), container);

            if (structuredType != null) {
              if (contextURL.getNavOrPropertyPath() == null) {
                type = structuredType;
              } else {
                final EdmNavigationProperty navProp =
                    ((EdmStructuredType) structuredType).getNavigationProperty(contextURL.getNavOrPropertyPath());

                type = navProp == null
                    ? structuredType
                    : navProp.getType();
              }
            }
          }
        }
        if (type == null) {
          type = new EdmTypeInfo.Builder().setEdm(edm).
              setTypeExpression(contextURL.getEntitySetOrSingletonOrType()).build().getType();
        }
      } else {
        type = edm.getEntityType(new FullQualifiedName(contextURL.getDerivedEntity()));
      }
    }
  }

  return type;
}
 
Example 7
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 {
  
  EdmEntitySet responseEdmEntitySet = null; // we'll need this to build the ContextURL
  EntityCollection responseEntityCollection = null; // we'll need this to set the response body

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  int segmentCount = resourceParts.size();

  UriResource uriResource = resourceParts.get(0); // in our example, the first segment is the EntitySet
  if (!(uriResource instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
  EdmEntitySet startEdmEntitySet = uriResourceEntitySet.getEntitySet();

  if (segmentCount == 1) { // this is the case for: DemoService/DemoService.svc/Categories
    responseEdmEntitySet = startEdmEntitySet; // the response body is built from the first (and only) entitySet

    // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
    responseEntityCollection = storage.readEntitySetData(startEdmEntitySet);
  } else if (segmentCount == 2) { // in case of navigation: DemoService.svc/Categories(3)/Products

    UriResource lastSegment = resourceParts.get(1); // in our example we don't support more complex URIs
    if (lastSegment instanceof UriResourceNavigation) {
      UriResourceNavigation uriResourceNavigation = (UriResourceNavigation) lastSegment;
      EdmNavigationProperty edmNavigationProperty = uriResourceNavigation.getProperty();
      EdmEntityType targetEntityType = edmNavigationProperty.getType();
      // from Categories(1) to Products
      responseEdmEntitySet = Util.getNavigationTargetEntitySet(startEdmEntitySet, edmNavigationProperty);

      // 2nd: fetch the data from backend
      // first fetch the entity where the first segment of the URI points to
      List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
      // e.g. for Categories(3)/Products we have to find the single entity: Category with ID 3
      Entity sourceEntity = storage.readEntityData(startEdmEntitySet, keyPredicates);
      // error handling for e.g. DemoService.svc/Categories(99)/Products
      if (sourceEntity == null) {
        throw new ODataApplicationException("Entity not found.",
            HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
      }
      // then fetch the entity collection where the entity navigates to
      // note: we don't need to check uriResourceNavigation.isCollection(),
      // because we are the EntityCollectionProcessor
      responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
    }
  } else { // this would be the case for e.g. Products(1)/Category/Products
    throw new ODataApplicationException("Not supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  // 3rd: create and configure a serializer
  ContextURL contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build();
  final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName();
  EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
      .contextURL(contextUrl).id(id).build();
  EdmEntityType edmEntityType = responseEdmEntitySet.getEntityType();

  ODataSerializer serializer = odata.createSerializer(responseFormat);
  SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType,
      responseEntityCollection, opts);

  // 4th: configure the response object: set the body, headers and status code
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 8
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 {
  
  EdmEntitySet responseEdmEntitySet = null; // we'll need this to build the ContextURL
  EntityCollection responseEntityCollection = null; // we'll need this to set the response body

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  int segmentCount = resourceParts.size();

  UriResource uriResource = resourceParts.get(0); // in our example, the first segment is the EntitySet
  if (!(uriResource instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
  EdmEntitySet startEdmEntitySet = uriResourceEntitySet.getEntitySet();

  if (segmentCount == 1) { // this is the case for: DemoService/DemoService.svc/Categories
    responseEdmEntitySet = startEdmEntitySet; // the response body is built from the first (and only) entitySet

    // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
    responseEntityCollection = storage.readEntitySetData(startEdmEntitySet);
  } else if (segmentCount == 2) { // in case of navigation: DemoService.svc/Categories(3)/Products

    UriResource lastSegment = resourceParts.get(1); // in our example we don't support more complex URIs
    if (lastSegment instanceof UriResourceNavigation) {
      UriResourceNavigation uriResourceNavigation = (UriResourceNavigation) lastSegment;
      EdmNavigationProperty edmNavigationProperty = uriResourceNavigation.getProperty();
      EdmEntityType targetEntityType = edmNavigationProperty.getType();
      // from Categories(1) to Products
      responseEdmEntitySet = Util.getNavigationTargetEntitySet(startEdmEntitySet, edmNavigationProperty);

      // 2nd: fetch the data from backend
      // first fetch the entity where the first segment of the URI points to
      List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
      // e.g. for Categories(3)/Products we have to find the single entity: Category with ID 3
      Entity sourceEntity = storage.readEntityData(startEdmEntitySet, keyPredicates);
      // error handling for e.g. DemoService.svc/Categories(99)/Products
      if (sourceEntity == null) {
        throw new ODataApplicationException("Entity not found.",
            HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
      }
      // then fetch the entity collection where the entity navigates to
      // note: we don't need to check uriResourceNavigation.isCollection(),
      // because we are the EntityCollectionProcessor
      responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
    }
  } else { // this would be the case for e.g. Products(1)/Category/Products
    throw new ODataApplicationException("Not supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  // 3rd: create and configure a serializer
  ContextURL contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build();
  final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName();
  EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
      .contextURL(contextUrl).id(id).build();
  EdmEntityType edmEntityType = responseEdmEntitySet.getEntityType();

  ODataSerializer serializer = odata.createSerializer(responseFormat);
  SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType,
      responseEntityCollection, opts);

  // 4th: configure the response object: set the body, headers and status code
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 9
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 {

  EdmEntitySet responseEdmEntitySet = null; // we'll need this to build the ContextURL
  EntityCollection responseEntityCollection = null; // we'll need this to set the response body
  EdmEntityType responseEdmEntityType = null;

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  int segmentCount = resourceParts.size();

  UriResource uriResource = resourceParts.get(0); // in our example, the first segment is the EntitySet
  if (!(uriResource instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
  EdmEntitySet startEdmEntitySet = uriResourceEntitySet.getEntitySet();

  if (segmentCount == 1) { // this is the case for: DemoService/DemoService.svc/Categories
    responseEdmEntitySet = startEdmEntitySet; // the response body is built from the first (and only) entitySet

    // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
    responseEntityCollection = storage.readEntitySetData(startEdmEntitySet);
  } else if (segmentCount == 2) { // in case of navigation: DemoService.svc/Categories(3)/Products

    UriResource lastSegment = resourceParts.get(1); // in our example we don't support more complex URIs
    if (lastSegment instanceof UriResourceNavigation) {
      UriResourceNavigation uriResourceNavigation = (UriResourceNavigation) lastSegment;
      EdmNavigationProperty edmNavigationProperty = uriResourceNavigation.getProperty();
      EdmEntityType targetEntityType = edmNavigationProperty.getType();
      if (!edmNavigationProperty.containsTarget()) {
     // from Categories(1) to Products
        responseEdmEntitySet = Util.getNavigationTargetEntitySet(startEdmEntitySet, edmNavigationProperty);
      } else {
        responseEdmEntitySet = startEdmEntitySet;
        responseEdmEntityType = targetEntityType;
      }

      // 2nd: fetch the data from backend
      // first fetch the entity where the first segment of the URI points to
      List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
      // e.g. for Categories(3)/Products we have to find the single entity: Category with ID 3
      Entity sourceEntity = storage.readEntityData(startEdmEntitySet, keyPredicates);
      // error handling for e.g. DemoService.svc/Categories(99)/Products
      if (sourceEntity == null) {
        throw new ODataApplicationException("Entity not found.",
            HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
      }
      // then fetch the entity collection where the entity navigates to
      // note: we don't need to check uriResourceNavigation.isCollection(),
      // because we are the EntityCollectionProcessor
      responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
    }
  } else { // this would be the case for e.g. Products(1)/Category/Products
    throw new ODataApplicationException("Not supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  ContextURL contextUrl = null;
  EdmEntityType edmEntityType = null;
  // 3rd: create and configure a serializer
  if (isContNav(uriInfo)) {
    contextUrl = ContextURL.with().entitySetOrSingletonOrType(request.getRawODataPath()).build();
    edmEntityType = responseEdmEntityType;
  } else {
    contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build(); 
    edmEntityType = responseEdmEntitySet.getEntityType();
  }
  final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName();
  EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
      .contextURL(contextUrl).id(id).build();
  
  ODataSerializer serializer = odata.createSerializer(responseFormat);
  SerializerResult serializerResult = serializer.entityCollection(this.srvMetadata, edmEntityType,
      responseEntityCollection, opts);

  // 4th: configure the response object: set the body, headers and status code
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}