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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmNavigationProperty#isCollection() . 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: RequestValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private ValidationResult validateBinding(final Link navigationBinding, final EdmNavigationProperty edmProperty)
    throws DataProviderException {
  if (navigationBinding == null) {
    return ValidationResult.NOT_FOUND;
  }

  if (edmProperty.isCollection()) {
    if (navigationBinding.getBindingLinks().size() == 0) {
      return ValidationResult.EMPTY;
    }

    for (final String bindingLink : navigationBinding.getBindingLinks()) {
      validateLink(bindingLink);
    }
  } else {
    if (navigationBinding.getBindingLink() == null) {
      return ValidationResult.EMPTY;
    }

    validateLink(navigationBinding.getBindingLink());
  }

  return ValidationResult.FOUND;
}
 
Example 2
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 3
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void setLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
    final Entity targetEntity) {
  if (navigationProperty.isCollection()) {
    setLinks(srcEntity, navigationProperty.getName(), targetEntity);
  } else {
    setLink(srcEntity, navigationProperty.getName(), targetEntity);
  }
}
 
Example 4
Source File: EdmTypeConvertor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public PropertyMetadata visit(EdmNavigationProperty property) {
    this.nullable = property.isNullable();
    this.isCollection = property.isCollection();
    PropertyMetadata metaProperty = property(Object.class);
    metaProperty.setChildProperties(visit(property.getType()));
    return metaProperty;
}
 
Example 5
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity getRelatedEntity(Entity entity, UriResourceNavigation navigationResource) 
    throws ODataApplicationException {
  
  final EdmNavigationProperty edmNavigationProperty = navigationResource.getProperty();
  
  if(edmNavigationProperty.isCollection()) {
    return Util.findEntity(edmNavigationProperty.getType(), getRelatedEntityCollection(entity, navigationResource), 
       navigationResource.getKeyPredicates());
  } else {
    final Link link = entity.getNavigationLink(edmNavigationProperty.getName());
    return link == null ? null : link.getInlineEntity();
  }
}
 
Example 6
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity getRelatedEntity(Entity entity, UriResourceNavigation navigationResource) 
    throws ODataApplicationException {
  
  final EdmNavigationProperty edmNavigationProperty = navigationResource.getProperty();
  
  if(edmNavigationProperty.isCollection()) {
    return Util.findEntity(edmNavigationProperty.getType(), getRelatedEntityCollection(entity, navigationResource), 
       navigationResource.getKeyPredicates());
  } else {
    final Link link = entity.getNavigationLink(edmNavigationProperty.getName());
    return link == null ? null : link.getInlineEntity();
  }
}
 
Example 7
Source File: JsonDeltaSerializerWithNavigations.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 SelectOption innerSelect, final CountOption innerCount,
    final boolean writeOnlyCount, final boolean writeOnlyRef, final String name,
    final JsonGenerator json, boolean isFullRepresentation) throws IOException, SerializerException {

  if (property.isCollection()) {
    if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
      json.writeFieldName(property.getName());
      json.writeStartArray();
      json.writeEndArray();
    } else if (navigationLink != null && navigationLink.getInlineEntitySet() != null) {
      if (isFullRepresentation) {
        json.writeFieldName(property.getName());
      } else {
        json.writeFieldName(property.getName() + Constants.AT + Constants.DELTAVALUE);
      }
      writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand,
          innerSelect, writeOnlyRef, name, json, isFullRepresentation);
    }

  } else {
    if (isFullRepresentation) {
      json.writeFieldName(property.getName());
    } else {
      json.writeFieldName(property.getName()+ Constants.AT + Constants.DELTAVALUE);
    }
    if (navigationLink == null || navigationLink.getInlineEntity() == null) {
      json.writeNull();
    } else if (navigationLink != null && navigationLink.getInlineEntity() != null) {
      if (navigationLink.getInlineEntity() instanceof DeletedEntity) {
        writeDeletedEntity(navigationLink.getInlineEntity(), json);
      } else {
        writeAddedUpdatedEntity(metadata, property.getType(), navigationLink.getInlineEntity(),
            innerExpand, innerSelect, null, writeOnlyRef, name, json, isFullRepresentation);
      }
    }
  }
}
 
Example 8
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void setLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
    final Entity targetEntity) {
  if (navigationProperty.isCollection()) {
    setLinks(srcEntity, navigationProperty.getName(), targetEntity);
  } else {
    setLink(srcEntity, navigationProperty.getName(), targetEntity);
  }
}
 
Example 9
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static List<UriParameter> parseNavigationKeyPredicate(UriTokenizer tokenizer,
    final EdmNavigationProperty navigationProperty,
    final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  if (tokenizer.next(TokenKind.OPEN)) {
    if (navigationProperty.isCollection()) {
      return parseKeyPredicate(tokenizer, navigationProperty.getType(), navigationProperty.getPartner(),
          edm, referringType, aliases);
    } else {
      throw new UriParserSemanticException("A key is not allowed on non-collection navigation properties.",
          UriParserSemanticException.MessageKeys.KEY_NOT_ALLOWED);
    }
  }
  return null;
}
 
Example 10
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity getRelatedEntity(Entity entity, UriResourceNavigation navigationResource) 
    throws ODataApplicationException {
  
  final EdmNavigationProperty edmNavigationProperty = navigationResource.getProperty();
  
  if(edmNavigationProperty.isCollection()) {
    return Util.findEntity(edmNavigationProperty.getType(), getRelatedEntityCollection(entity, navigationResource), 
       navigationResource.getKeyPredicates());
  } else {
    final Link link = entity.getNavigationLink(edmNavigationProperty.getName());
    return link == null ? null : link.getInlineEntity();
  }
}
 
Example 11
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Link createLink(final ExpandTreeBuilder expandBuilder, final String navigationPropertyName,
    final JsonNode jsonNode,
    final EdmNavigationProperty edmNavigationProperty) throws DeserializerException {
  Link link = new Link();
  link.setTitle(navigationPropertyName);
  final ExpandTreeBuilder childExpandBuilder = (expandBuilder != null) ? expandBuilder.expand(edmNavigationProperty)
      : null;
  EdmEntityType derivedEdmEntityType = (EdmEntityType) getDerivedType(
      edmNavigationProperty.getType(), jsonNode);
  if (jsonNode.isArray() && edmNavigationProperty.isCollection()) {
    link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
    EntityCollection inlineEntitySet = new EntityCollection();
    inlineEntitySet.getEntities().addAll(
        consumeEntitySetArray(derivedEdmEntityType, jsonNode, childExpandBuilder));
    link.setInlineEntitySet(inlineEntitySet);
  } else if (!jsonNode.isArray() && (!jsonNode.isValueNode() || jsonNode.isNull())
      && !edmNavigationProperty.isCollection()) {
    link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE);
    if (!jsonNode.isNull()) {
      Entity inlineEntity = consumeEntityNode(derivedEdmEntityType, (ObjectNode) jsonNode, childExpandBuilder);
      link.setInlineEntity(inlineEntity);
    }
  } else {
    throw new DeserializerException("Invalid value: " + jsonNode.getNodeType()
        + " for expanded navigation property: " + navigationPropertyName,
        MessageKeys.INVALID_VALUE_FOR_NAVIGATION_PROPERTY, navigationPropertyName);
  }
  return link;
}
 
Example 12
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Check if jsonNode is not null or if null but nullable or collection navigationProperty
 *
 * @param jsonNode related json node
 * @param edmNavigationProperty related navigation property
 * @throws DeserializerException if jsonNode is not null or if null but nullable or collection navigationProperty
 */
private void checkNotNullOrValidNull(final JsonNode jsonNode,
    final EdmNavigationProperty edmNavigationProperty) throws DeserializerException {
  boolean isNullable = edmNavigationProperty.isNullable();
  if ((jsonNode.isNull() && !isNullable) || (jsonNode.isNull() && edmNavigationProperty.isCollection())) {
    throw new DeserializerException("Property: " + edmNavigationProperty.getName() + " must not be null.",
        MessageKeys.INVALID_NULL_PROPERTY, edmNavigationProperty.getName());
  }
}
 
Example 13
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void setLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
    final Entity targetEntity) {
  if (navigationProperty.isCollection()) {
    setLinks(srcEntity, navigationProperty.getName(), targetEntity);
  } else {
    setLink(srcEntity, navigationProperty.getName(), targetEntity);
  }
}
 
Example 14
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 15
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void deleteReference(final Entity entity, final EdmNavigationProperty navigationProperty,
    final String entityId, final String rawServiceRoot) throws DataProviderException {

  if (navigationProperty.isCollection()) {
    final Entity targetEntity = getEntityByReference(entityId, rawServiceRoot);
    final Link navigationLink = entity.getNavigationLink(navigationProperty.getName());

    if (navigationLink != null && navigationLink.getInlineEntitySet() != null
        && navigationLink.getInlineEntitySet().getEntities().contains(targetEntity)) {

      // Remove partner single-valued navigation property
      if (navigationProperty.getPartner() != null) {
        final EdmNavigationProperty edmPartnerNavigationProperty = navigationProperty.getPartner();
        if (!edmPartnerNavigationProperty.isCollection() && !edmPartnerNavigationProperty.isNullable()) {
          throw new DataProviderException("Navigation property must not be null", HttpStatusCode.BAD_REQUEST);
        } else if (!edmPartnerNavigationProperty.isCollection()) {
          removeLink(edmPartnerNavigationProperty, targetEntity);
        } else if (edmPartnerNavigationProperty.isCollection()
            && edmPartnerNavigationProperty.getPartner() != null) {
          // Bidirectional referential constraint
          final Link partnerNavigationLink = targetEntity.getNavigationLink(edmPartnerNavigationProperty.getName());
          if (partnerNavigationLink != null && partnerNavigationLink.getInlineEntitySet() != null) {
            partnerNavigationLink.getInlineEntitySet().getEntities().remove(entity);
          }
        }
      }

      // Remove target entity from collection-valued navigation property
      navigationLink.getInlineEntitySet().getEntities().remove(targetEntity);
    } else {
      throw new DataProviderException("Entity not found", HttpStatusCode.NOT_FOUND);
    }
  } else {
    if (navigationProperty.isNullable()) {
      removeLink(navigationProperty, entity);
    } else {
      throw new DataProviderException("Navigation property must not be null", HttpStatusCode.BAD_REQUEST);
    }
  }
}
 
Example 16
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void setLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
    final Entity targetEntity) {
  if (navigationProperty.isCollection()) {
    DataCreator.setLinks(srcEntity, navigationProperty.getName(), targetEntity);
  } else {
    DataCreator.setLink(srcEntity, navigationProperty.getName(), targetEntity);
  }
}
 
Example 17
Source File: RequestValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private ValidationResult validateNavigationLink(final Link navigationLink, final EdmNavigationProperty edmProperty,
    final EdmBindingTarget edmBindingTarget) throws DataProviderException {
  if (navigationLink == null) {
    return ValidationResult.NOT_FOUND;
  }

  if (edmProperty.isCollection()) {
    final EntityCollection inlineEntitySet = navigationLink.getInlineEntitySet();
    if (inlineEntitySet != null) {
      if (!isInsert && inlineEntitySet.getEntities().size() > 0) {
        throw new DataProvider.DataProviderException("Deep update is not allowed", HttpStatusCode.BAD_REQUEST);
      } else {
        for (final Entity entity : navigationLink.getInlineEntitySet().getEntities()) {
          validate(edmBindingTarget, entity);
        }
      }
    }
  } else {
    final Entity inlineEntity = navigationLink.getInlineEntity();
    if (!isInsert && inlineEntity != null) {
      throw new DataProvider.DataProviderException("Deep update is not allowed", HttpStatusCode.BAD_REQUEST);
    } else if (inlineEntity != null) {
      validate(edmBindingTarget, navigationLink.getInlineEntity());
    }
  }

  return ValidationResult.FOUND;
}
 
Example 18
Source File: ExpandParser.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private ExpandItem parseItem(UriTokenizer tokenizer, final EdmStructuredType referencedType)
    throws UriParserException, UriValidationException {
  ExpandItemImpl item = new ExpandItemImpl();
  if (tokenizer.next(TokenKind.STAR)) {
    item.setIsStar(true);
    if (tokenizer.next(TokenKind.SLASH)) {
      ParserHelper.requireNext(tokenizer, TokenKind.REF);
      item.setIsRef(true);
    } else if (tokenizer.next(TokenKind.OPEN)) {
      ParserHelper.requireNext(tokenizer, TokenKind.LEVELS);
      ParserHelper.requireNext(tokenizer, TokenKind.EQ);
      item.setSystemQueryOption((SystemQueryOption) parseLevels(tokenizer));
      ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
    }

  } else {
    final EdmStructuredType typeCast = ParserHelper.parseTypeCast(tokenizer, edm, referencedType);
    if (typeCast != null) {
      item.setTypeFilter(typeCast);
      ParserHelper.requireNext(tokenizer, TokenKind.SLASH);
    }

    UriInfoImpl resource = parseExpandPath(tokenizer, edm, referencedType, item);

    UriResourcePartTyped lastPart = (UriResourcePartTyped) resource.getLastResourcePart();

    boolean hasSlash = false;
    EdmStructuredType typeCastSuffix = null;
    if (tokenizer.next(TokenKind.SLASH)) {
      hasSlash = true;
      if (lastPart instanceof UriResourceNavigation) {
        UriResourceNavigationPropertyImpl navigationResource = (UriResourceNavigationPropertyImpl) lastPart;
        final EdmNavigationProperty navigationProperty = navigationResource.getProperty();
        typeCastSuffix = ParserHelper.parseTypeCast(tokenizer, edm, navigationProperty.getType());
        if (typeCastSuffix != null) {
          if (navigationProperty.isCollection()) {
            navigationResource.setCollectionTypeFilter(typeCastSuffix);
          } else {
            navigationResource.setEntryTypeFilter(typeCastSuffix);
          }
          hasSlash = false;
        }
      }
    }
    //For handling $expand for Stream property in v 4.01
      if(lastPart instanceof UriResourcePrimitivePropertyImpl){     
        item.setResourcePath(resource);
      }else{
      final EdmStructuredType newReferencedType = typeCastSuffix != null ? typeCastSuffix 
        : (EdmStructuredType) lastPart.getType();
      final boolean newReferencedIsCollection = lastPart.isCollection();
      if (hasSlash || tokenizer.next(TokenKind.SLASH)) {
        if (tokenizer.next(TokenKind.REF)) {
          resource.addResourcePart(new UriResourceRefImpl());
          item.setIsRef(true);
          parseOptions(tokenizer, newReferencedType, newReferencedIsCollection, item, true, false);
        } else {
          ParserHelper.requireNext(tokenizer, TokenKind.COUNT);
          resource.addResourcePart(new UriResourceCountImpl());
          item.setCountPath(true);
          parseOptions(tokenizer, newReferencedType, newReferencedIsCollection, item, false, true);
        }
      } else {
        parseOptions(tokenizer, newReferencedType, newReferencedIsCollection, item, false, false);
      }

      item.setResourcePath(resource);
    }
   }

  return item;
}
 
Example 19
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 20
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);
    }
  }
}