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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmNavigationProperty#containsTarget() . 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: DataRequest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static String getTargetEntitySet(EdmBindingTarget root, LinkedList<UriResourceNavigation> navigations) {
  EdmEntityType type = root.getEntityType();
  EdmBindingTarget targetEntitySet = root;
  String targetEntitySetName = root.getName();
  String name = null;
  for (UriResourceNavigation nav:navigations) {
    name = nav.getProperty().getName();
    EdmNavigationProperty property = type.getNavigationProperty(name);
    if (property.containsTarget()) {
      return root.getName();
    }
    type = nav.getProperty().getType();
    
    for(EdmNavigationPropertyBinding enb:targetEntitySet.getNavigationPropertyBindings()) {
      if (enb.getPath().equals(name)) {
        targetEntitySetName = enb.getTarget();
      } else if (enb.getPath().endsWith("/"+name)) {
        targetEntitySetName = enb.getTarget();
      }
    }
  }
  return targetEntitySetName;
}
 
Example 2
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 3
Source File: MetadataDocumentJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void appendNavigationProperties(final JsonGenerator json, 
    final EdmStructuredType type) throws SerializerException, IOException {
  List<String> navigationPropertyNames = new ArrayList<>(type.getNavigationPropertyNames());
  if (type.getBaseType() != null) {
    navigationPropertyNames.removeAll(type.getBaseType().getNavigationPropertyNames());
  }
  for (String navigationPropertyName : navigationPropertyNames) {
    EdmNavigationProperty navigationProperty = type.getNavigationProperty(navigationPropertyName);
    json.writeObjectFieldStart(navigationPropertyName);
    json.writeStringField(KIND, Kind.NavigationProperty.name());
    
    json.writeStringField(TYPE, getAliasedFullQualifiedName(navigationProperty.getType()));
    if (navigationProperty.isCollection()) {
      json.writeBooleanField(COLLECTION, navigationProperty.isCollection());
    }
    
    if (!navigationProperty.isNullable()) {
      json.writeBooleanField(NULLABLE, navigationProperty.isNullable());
    }

    if (navigationProperty.getPartner() != null) {
      EdmNavigationProperty partner = navigationProperty.getPartner();
      json.writeStringField(PARTNER, partner.getName());
    }

    if (navigationProperty.containsTarget()) {
      json.writeBooleanField(CONTAINS_TARGET, navigationProperty.containsTarget());
    }

    if (navigationProperty.getReferentialConstraints() != null) {
      for (EdmReferentialConstraint constraint : navigationProperty.getReferentialConstraints()) {
        json.writeObjectFieldStart(REFERENTIAL_CONSTRAINT);
        json.writeStringField(constraint.getPropertyName(), constraint.getReferencedPropertyName());
        for (EdmAnnotation annotation : constraint.getAnnotations()) {
          appendAnnotations(json, annotation, null);
        }
        json.writeEndObject();
      }
    }
    
    if (navigationProperty.getOnDelete() != null) {
      json.writeObjectFieldStart(ON_DELETE);
      json.writeStringField(ON_DELETE_PROPERTY, navigationProperty.getOnDelete().getAction());
      appendAnnotations(json, navigationProperty.getOnDelete(), null);
      json.writeEndObject();
    }

    appendAnnotations(json, navigationProperty, null);

    json.writeEndObject();
  }
}
 
Example 4
Source File: MetadataDocumentXmlSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void appendNavigationProperties(final XMLStreamWriter writer, final EdmStructuredType type)
    throws XMLStreamException {
  List<String> navigationPropertyNames = new ArrayList<>(type.getNavigationPropertyNames());
  if (type.getBaseType() != null) {
    navigationPropertyNames.removeAll(type.getBaseType().getNavigationPropertyNames());
  }
  for (String navigationPropertyName : navigationPropertyNames) {
    EdmNavigationProperty navigationProperty = type.getNavigationProperty(navigationPropertyName);

    writer.writeStartElement(XML_NAVIGATION_PROPERTY);
    writer.writeAttribute(XML_NAME, navigationPropertyName);
    writer.writeAttribute(XML_TYPE, getAliasedFullQualifiedName(navigationProperty.getType(), navigationProperty
        .isCollection()));
    if (!navigationProperty.isNullable()) {
      writer.writeAttribute(XML_NULLABLE, "" + navigationProperty.isNullable());
    }

    if (navigationProperty.getPartner() != null) {
      EdmNavigationProperty partner = navigationProperty.getPartner();
      writer.writeAttribute(XML_PARTNER, partner.getName());
    }

    if (navigationProperty.containsTarget()) {
      writer.writeAttribute(XML_CONTAINS_TARGET, "" + navigationProperty.containsTarget());
    }

    if (navigationProperty.getReferentialConstraints() != null) {
      for (EdmReferentialConstraint constraint : navigationProperty.getReferentialConstraints()) {
        writer.writeStartElement("ReferentialConstraint");
        writer.writeAttribute(XML_PROPERTY, constraint.getPropertyName());
        writer.writeAttribute("ReferencedProperty", constraint.getReferencedPropertyName());
        appendAnnotations(writer, constraint);
        writer.writeEndElement();
      }
    }
    
    if (navigationProperty.getOnDelete() != null) {
      writer.writeStartElement(XML_ON_DELETE);
      writer.writeAttribute(XML_ON_DELETE_PROPERTY, navigationProperty.getOnDelete().getAction());
      appendAnnotations(writer, navigationProperty.getOnDelete());
      writer.writeEndElement();
    }

    appendAnnotations(writer, navigationProperty);

    writer.writeEndElement();
  }
}
 
Example 5
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());
}