Java Code Examples for org.apache.olingo.commons.api.edm.EdmEntityType#getNavigationProperty()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmEntityType#getNavigationProperty() . 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: DataProvider.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void handleDeleteSingleNavigationProperties(final EdmEntitySet edmEntitySet, final Entity entity,
    final Entity changedEntity) throws DataProviderException {
  final EdmEntityType entityType = edmEntitySet.getEntityType();
  final List<String> navigationPropertyNames = entityType.getNavigationPropertyNames();

  for (final String navPropertyName : navigationPropertyNames) {
    final Link navigationLink = changedEntity.getNavigationLink(navPropertyName);
    final EdmNavigationProperty navigationProperty = entityType.getNavigationProperty(navPropertyName);
    if (!navigationProperty.isCollection() && navigationLink != null && navigationLink.getInlineEntity() == null) {

      // Check if partner is available
      if (navigationProperty.getPartner() != null && entity.getNavigationLink(navPropertyName) != null) {
        Entity partnerEntity = entity.getNavigationLink(navPropertyName).getInlineEntity();
        removeLink(navigationProperty.getPartner(), partnerEntity);
      }

      // Remove link
      removeLink(navigationProperty, entity);
    }
  }
}
 
Example 2
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void consumeExpandedNavigationProperties(final EdmEntityType edmEntityType, final ObjectNode node,
    final Entity entity, final ExpandTreeBuilder expandBuilder) throws DeserializerException {
  List<String> navigationPropertyNames = edmEntityType.getNavigationPropertyNames();
  for (String navigationPropertyName : navigationPropertyNames) {
    // read expanded navigation property
    JsonNode jsonNode = node.get(navigationPropertyName);
    if (jsonNode != null) {
      EdmNavigationProperty edmNavigationProperty = edmEntityType.getNavigationProperty(navigationPropertyName);
      checkNotNullOrValidNull(jsonNode, edmNavigationProperty);

      Link link = createLink(expandBuilder, navigationPropertyName, jsonNode, edmNavigationProperty);
      entity.getNavigationLinks().add(link);
      node.remove(navigationPropertyName);
    }
  }
}
 
Example 3
Source File: EdmAssistedJsonSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void links(final Linked linked, final EdmEntityType entityType, JsonGenerator json)
    throws IOException, SerializerException {

  for (final Link link : linked.getNavigationLinks()) {
    final String name = link.getTitle();
    for (final Annotation annotation : link.getAnnotations()) {
      valuable(json, annotation, name + '@' + annotation.getTerm(), null, null);
    }

    final EdmEntityType targetType =
        entityType == null || name == null || entityType.getNavigationProperty(name) == null ? null : entityType
            .getNavigationProperty(name).getType();
    if (link.getInlineEntity() != null) {
      json.writeFieldName(name);
      doSerialize(targetType, link.getInlineEntity(), null, null, json);
    } else if (link.getInlineEntitySet() != null) {
      json.writeArrayFieldStart(name);
      for (final Entity subEntry : link.getInlineEntitySet().getEntities()) {
        doSerialize(targetType, subEntry, null, null, json);
      }
      json.writeEndArray();
    }
  }
}
 
Example 4
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 5
Source File: ExpandSystemQueryOptionHandler.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity transformEntityGraphToTree(final Entity entity, final EdmBindingTarget edmEntitySet,
    final ExpandOption expand, final ExpandItem parentExpandItem) throws ODataApplicationException {
  final Entity newEntity = newEntity(entity);
  if (hasExpandItems(expand)) {
    final boolean expandAll = expandAll(expand);
    final Set<String> expanded = expandAll ? null : getExpandedPropertyNames(expand.getExpandItems());
    final EdmEntityType edmType = edmEntitySet.getEntityType();

    for (final Link link : entity.getNavigationLinks()) {
      final String propertyName = link.getTitle();

      if (expandAll || expanded.contains(propertyName)) {
        final EdmNavigationProperty edmNavigationProperty = edmType.getNavigationProperty(propertyName);
        final EdmBindingTarget edmBindingTarget = edmEntitySet.getRelatedBindingTarget(propertyName);
        final Link newLink = newLink(link);
        newEntity.getNavigationLinks().add(newLink);
        final ExpandItem expandItem = getInnerExpandItem(expand, propertyName);

        if (edmNavigationProperty.isCollection()) {
          newLink.setInlineEntitySet(transformEntitySetGraphToTree(link.getInlineEntitySet(),
              edmBindingTarget, expandItem.getExpandOption(), expandItem));
        } else {
          newLink.setInlineEntity(transformEntityGraphToTree(link.getInlineEntity(),
              edmBindingTarget,expandItem.getExpandOption(), expandItem));
        }
      }
    }

  }
  return newEntity;
}
 
Example 6
Source File: EdmTypeValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * This looks for the last fully qualified identifier to fetch the navigation property
 * e.g if navigation property path is Microsoft.Exchange.Services.OData.Model.ItemAttachment/Item 
 * then it fetches the entity ItemAttachment and fetches the navigation property Item
 * if navigation property path is EntityType/ComplexType/OData.Model.DerivedComplexType/Item
 * then it fetches the complex type DerivedComplexType and fetches the navigation property Item
 * @param navBindingPath
 * @param sourceEntityType 
 * @return EdmNavigationProperty
 */
private EdmNavigationProperty findLastQualifiedNameHavingNavigationProperty(String navBindingPath, 
    EdmEntityType sourceEntityType) {
  String[] paths = navBindingPath.split("/");
  String lastFullQualifiedName = "";
  for (String path : paths) {
    if (path.contains(".")) {
      lastFullQualifiedName = path;
    }
  }
  String strNavProperty = paths[paths.length - 1];
  String remainingPath = navBindingPath.substring(navBindingPath.indexOf(lastFullQualifiedName) 
      + lastFullQualifiedName.length() + (lastFullQualifiedName.length() == 0 ? 0 : 1), 
      navBindingPath.lastIndexOf(strNavProperty));
  if (remainingPath.length() > 0) {
    remainingPath = remainingPath.substring(0, remainingPath.length() - 1);
  }
  EdmNavigationProperty navProperty = null;
  EdmEntityType sourceEntityTypeHavingNavProp = lastFullQualifiedName.length() == 0 ? sourceEntityType : 
    (edmEntityTypesMap.containsKey(new FullQualifiedName(lastFullQualifiedName)) ? 
      edmEntityTypesMap.get(new FullQualifiedName(lastFullQualifiedName)) : 
        edmEntityTypesMap.get(fetchCorrectNamespaceFromAlias(new FullQualifiedName(lastFullQualifiedName))));
  if (sourceEntityTypeHavingNavProp == null) {
    EdmComplexType sourceComplexTypeHavingNavProp = 
        edmComplexTypesMap.containsKey(new FullQualifiedName(lastFullQualifiedName)) ?
        edmComplexTypesMap.get(new FullQualifiedName(lastFullQualifiedName)) : 
          edmComplexTypesMap.get(fetchCorrectNamespaceFromAlias(new FullQualifiedName(lastFullQualifiedName)));
    if (sourceComplexTypeHavingNavProp == null) {
      throw new RuntimeException("The fully Qualified type " + lastFullQualifiedName + 
          " mentioned in navigation binding path not found ");
    }
    navProperty = remainingPath.length() > 0 ? fetchNavigationProperty(remainingPath, strNavProperty, 
        sourceComplexTypeHavingNavProp) : sourceComplexTypeHavingNavProp.getNavigationProperty(strNavProperty);
  } else {
    navProperty = remainingPath.length() > 0 ? fetchNavigationProperty(remainingPath, strNavProperty, 
        sourceEntityTypeHavingNavProp) : sourceEntityTypeHavingNavProp.getNavigationProperty(strNavProperty);
  }
  return navProperty;
}
 
Example 7
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void readAnnotationOnAnEntityType() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmEntityType entity = edm.getEntityTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "SEPMRA_C_CountryVHType"));
  assertEquals(1, entity.getAnnotations().size());
  assertNotNull(entity.getAnnotations().get(0).getTerm());
  assertEquals("HeaderInfo", entity.getAnnotations().get(0).getTerm().getName());
  assertNotNull(entity.getAnnotations().get(0).getExpression());
  
  EdmEntityType entity1 = edm.getEntityTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "SEPMRA_C_SalesOrderCustCntctVHType"));
  EdmAnnotation annotation = entity1.getAnnotations().get(0);
  assertNotNull(annotation);
  assertEquals(6, entity1.getAnnotations().size());
  assertEquals("FieldGroup", annotation.getTerm().getName());
  assertEquals("ContactPerson", annotation.getQualifier());
  EdmExpression expression = annotation.getExpression();
  assertNotNull(expression);
  assertTrue(expression.isDynamic());
  EdmRecord record = expression.asDynamic().asRecord();
  assertNotNull(record);
  assertEquals(2, record.asRecord().getPropertyValues().size());
  List<EdmPropertyValue> propertyValues = record.asRecord().getPropertyValues();
  assertEquals("Data", propertyValues.get(0).getProperty());
  assertTrue(propertyValues.get(0).getValue().isDynamic());
  List<EdmExpression> items = propertyValues.get(0).getValue().asDynamic().asCollection().getItems();
  assertEquals(4, items.size());
  assertEquals("Label", propertyValues.get(1).getProperty());
  assertEquals("Contact Person", propertyValues.get(1).getValue().asConstant().asPrimitive());
  
  assertEquals(2, entity1.getNavigationProperty("to_Customer").getAnnotations().size());
  EdmNavigationProperty navProperty = entity1.getNavigationProperty("to_Customer");
  assertEquals("ThingPerspective", navProperty.
      getAnnotations().get(0).getTerm().getName());
}
 
Example 8
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void consumeDeltaJsonNodeFields(EdmEntityType edmEntityType, ObjectNode node,
    Entity entity, ExpandTreeBuilder expandBuilder) 
    throws DeserializerException {
  if (constants instanceof Constantsv01) {
    List<String> navigationPropertyNames = edmEntityType.getNavigationPropertyNames();
    for (String navigationPropertyName : navigationPropertyNames) {
      // read expanded navigation property for delta
      String delta = navigationPropertyName + Constants.AT + Constants.DELTAVALUE;
      JsonNode jsonNode = node.get(delta);
      EdmNavigationProperty edmNavigationProperty = edmEntityType.getNavigationProperty(navigationPropertyName);
      if (jsonNode != null && jsonNode.isArray() && edmNavigationProperty.isCollection()) {
        checkNotNullOrValidNull(jsonNode, edmNavigationProperty);
        Link link = new Link();
        link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
        link.setTitle(navigationPropertyName);
        Delta deltaValue = new Delta();
        for (JsonNode arrayElement : jsonNode) {
          String removed = Constants.AT + Constants.REMOVED;
          if (arrayElement.get(removed) != null) {
            //if @removed is present create a DeletedEntity Object
            JsonNode reasonNode = arrayElement.get(removed);
            DeletedEntity deletedEntity = new DeletedEntity();
            Reason reason = null;
            if (reasonNode.get(REASON) != null) {
              if(reasonNode.get(REASON).asText().equals(Reason.changed.name())){
                reason = Reason.changed;
              }else if(reasonNode.get(REASON).asText().equals(Reason.deleted.name())){
                reason = Reason.deleted;
              }
            }else{
              throw new DeserializerException("DeletedEntity reason is null.",
                  SerializerException.MessageKeys.MISSING_DELTA_PROPERTY, Constants.REASON);
            }
            deletedEntity.setReason(reason);
            try {
              deletedEntity.setId(new URI(arrayElement.get(constants.getId()).asText()));
            } catch (URISyntaxException e) {
              throw new DeserializerException("Could not set Id for deleted Entity", e,
                  DeserializerException.MessageKeys.UNKNOWN_CONTENT);
            }
            deltaValue.getDeletedEntities().add(deletedEntity);
          } else {
            //For @id and properties create normal entity
            Entity inlineEntity = consumeEntityNode(edmEntityType, (ObjectNode) arrayElement, expandBuilder);
            deltaValue.getEntities().add(inlineEntity);
          }
        }
        link.setInlineEntitySet(deltaValue);
        entity.getNavigationLinks().add(link);
        node.remove(navigationPropertyName);
      }
    }
  }

}
 
Example 9
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private Link consumeBindingLink(final String key, final JsonNode jsonNode, final EdmEntityType edmEntityType)
    throws DeserializerException {
  String[] splitKey = key.split(ODATA_ANNOTATION_MARKER);
  String navigationPropertyName = splitKey[0];
  EdmNavigationProperty edmNavigationProperty = edmEntityType.getNavigationProperty(navigationPropertyName);
  if (edmNavigationProperty == null) {
    throw new DeserializerException("Invalid navigationPropertyName: " + navigationPropertyName,
        DeserializerException.MessageKeys.NAVIGATION_PROPERTY_NOT_FOUND, navigationPropertyName);
  }
  Link bindingLink = new Link();
  bindingLink.setTitle(navigationPropertyName);

  if (edmNavigationProperty.isCollection()) {
    assertIsNullNode(key, jsonNode);
    if (!jsonNode.isArray()) {
      throw new DeserializerException("Binding annotation: " + key + " must be an array.",
          DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, key);
    }
    List<String> bindingLinkStrings = new ArrayList<>();
    for (JsonNode arrayValue : jsonNode) {
      assertIsNullNode(key, arrayValue);
      if (!arrayValue.isTextual()) {
        throw new DeserializerException("Binding annotation: " + key + " must have string valued array.",
            DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, key);
      }
      bindingLinkStrings.add(arrayValue.asText());
    }
    bindingLink.setType(Constants.ENTITY_COLLECTION_BINDING_LINK_TYPE);
    bindingLink.setBindingLinks(bindingLinkStrings);
  } else {
    if (!jsonNode.isValueNode()) {
      throw new DeserializerException("Binding annotation: " + key + " must be a string value.",
          DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, key);
    }
    if (edmNavigationProperty.isNullable() && jsonNode.isNull()) {
      bindingLink.setBindingLink(null);
    } else {
      assertIsNullNode(key, jsonNode);
      bindingLink.setBindingLink(jsonNode.asText());        
    }
    bindingLink.setType(Constants.ENTITY_BINDING_LINK_TYPE);
  }
  return bindingLink;
}