org.apache.olingo.commons.api.edm.EdmNavigationProperty Java Examples

The following examples show how to use org.apache.olingo.commons.api.edm.EdmNavigationProperty. 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: MetadataTest.java    From olingo-odata4 with Apache License 2.0 7 votes vote down vote up
@Test
public void readAnnotationOnComplexType() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmComplexType complexType = edm.getComplexTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "CTPrim"));
  assertEquals(2, complexType.getAnnotations().size());
  assertEquals("HeaderInfo", complexType.getAnnotations().get(0).getTerm().getName());
  // Annotations on complex type property
  EdmProperty complexTypeProp = (EdmProperty) complexType.getProperty("PropertyInt16");
  assertEquals(1, complexTypeProp.getAnnotations().size());
  assertEquals("HeaderInfo", complexTypeProp.getAnnotations().get(0).getTerm().getName());
  // Annotations on complex type navigation property
  EdmNavigationProperty complexTypeNavProp = complexType.getNavigationProperty(
      "NavPropertyDraftAdministrativeDataType");
  assertEquals(1, complexTypeNavProp.getAnnotations().size());
  assertEquals("HeaderInfo", complexTypeNavProp.getAnnotations().get(0).getTerm().getName());
  
  FullQualifiedName termName = new FullQualifiedName("Integration", "Extractable");
  EdmTerm term = edm.getTerm(termName);
  EdmAnnotation annotation = complexType.getAnnotation(term, null);
  assertNotNull(annotation);
}
 
Example #2
Source File: DebugTabUriTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void expand() throws Exception {
  EdmNavigationProperty edmProperty = mock(EdmNavigationProperty.class);
  when(edmProperty.getName()).thenReturn("property");
  final DebugTabUri tab = new DebugTabUri(new UriInfoImpl().setKind(UriInfoKind.all)
      .setSystemQueryOption(new ExpandOptionImpl().addExpandItem(
          new ExpandItemImpl().setResourcePath(
              new UriInfoImpl().setKind(UriInfoKind.resource)
                  .addResourcePart(new UriResourceNavigationPropertyImpl(edmProperty)))
              .setSystemQueryOption(new LevelsOptionImpl().setValue(1)))));
  assertEquals("{\"kind\":\"all\",\"expand\":[{\"expandPath\":["
      + "{\"uriResourceKind\":\"navigationProperty\",\"segment\":\"property\",\"isCollection\":false}],"
      + "\"levels\":1}]}",
      createJson(tab));

  final String html = createHtml(tab);
  assertThat(html, allOf(
      startsWith("<h2>Kind</h2>\n"
          + "<p>all</p>\n"
          + "<h2>Expand Option</h2>\n"
          + "<ul>\n"
          + "<li class=\"json\">"),
      containsString("navigationProperty"), containsString("property"),
      containsString("isCollection"), containsString("false")));
  assertThat(html, allOf(containsString("levels"), endsWith("</li>\n</ul>\n")));
}
 
Example #3
Source File: EdmTypeValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch the correct navigation property from the remaining path
 * @param remainingPath
 * @param strNavProperty
 * @param sourceTypeHavingNavProp
 * @return EdmNavigationProperty
 */
private EdmNavigationProperty fetchNavigationProperty(String remainingPath,
    String strNavProperty, EdmStructuredType sourceTypeHavingNavProp) {
  String[] paths = remainingPath.split("/");
  for (String path : paths) {
    FullQualifiedName fqName = null;
    if (sourceTypeHavingNavProp instanceof EdmComplexType) {
      fqName = ((EdmComplexType)sourceTypeHavingNavProp).getProperty(path).getType().getFullQualifiedName();
    } else if (sourceTypeHavingNavProp instanceof EdmEntityType) {
      fqName = ((EdmEntityType)sourceTypeHavingNavProp).getProperty(path).getType().getFullQualifiedName();
    }
    if (fqName != null) {
      String namespace = aliasNamespaceMap.get(fqName.getNamespace());
      fqName = namespace != null ? new FullQualifiedName(namespace, fqName.getName()) : fqName;
    }
    
    sourceTypeHavingNavProp = edmEntityTypesMap.containsKey(fqName) ? 
        edmEntityTypesMap.get(fqName) : 
          edmComplexTypesMap.get(fqName);
  }
  return sourceTypeHavingNavProp.getNavigationProperty(strNavProperty);
}
 
Example #4
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void readAnnotationOnASingleton() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmEntityContainer container = edm.getEntityContainer();
  EdmSingleton singleton = container.getSingleton("SINav");
  assertEquals(2, singleton.getAnnotations().size());
  FullQualifiedName termName = new FullQualifiedName("UI", "HeaderInfo");
  EdmTerm term = edm.getTerm(termName);
  EdmAnnotation annotation = singleton.getAnnotation(term, null);
  assertNotNull(annotation);

  EdmEntityType singletonET = singleton.getEntityType();
  EdmProperty singlComplexProp = (EdmProperty) singletonET.getProperty("ComplexProperty");
  EdmComplexType singlCompType = (EdmComplexType) singlComplexProp.getTypeWithAnnotations();
  EdmNavigationProperty singlNavProp = (EdmNavigationProperty) singlCompType.getNavigationProperty(
      "NavPropertyDraftAdministrativeDataType");
  assertEquals(2, singlNavProp.getAnnotations().size());
  assertEquals("AdditionalInfo", singlNavProp.getAnnotations().get(0).getTerm().getName());
}
 
Example #5
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 #6
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method return the entity collection which are able to navigate from the parent entity (source) using uri navigation properties.
 * <p/>
 * In this method we check the parent entities primary keys and return the entity according to the values.
 * we use ODataDataHandler, navigation properties to get particular foreign keys.
 *
 * @param metadata     Service Metadata
 * @param parentEntity parentEntity
 * @param navigation   UriResourceNavigation
 * @return EntityCollection
 * @throws ODataServiceFault
 */
private EntityCollection getNavigableEntitySet(ServiceMetadata metadata, Entity parentEntity,
                                               EdmNavigationProperty navigation, String url)
        throws ODataServiceFault, ODataApplicationException {
    EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType()));
    String linkName = navigation.getName();
    List<Property> properties = new ArrayList<>();
    Map<String, EdmProperty> propertyMap = new HashMap<>();
    for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(type.getName())
                                               .getNavigationKeys(linkName)) {
        Property property = parentEntity.getProperty(keys.getPrimaryKey());
        if (property != null && !property.isNull()) {
            propertyMap.put(keys.getForeignKey(), (EdmProperty) type.getProperty(property.getName()));
            property.setName(keys.getForeignKey());
            properties.add(property);
        }
    }
    if(!properties.isEmpty()) {
        return createEntityCollectionFromDataEntryList(linkName, dataHandler
                .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), url);
    }
    return null;
}
 
Example #7
Source File: ApplyParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private UriResource parsePathSegment(final EdmElement property) throws UriParserException {
  if (property == null
      || !(property.getType().getKind() == EdmTypeKind.COMPLEX
      || property instanceof EdmNavigationProperty)) {
    // Could be a customAggregate or $count.
    return null;
  }
  if (tokenizer.next(TokenKind.SLASH)) {
    final EdmStructuredType typeCast = ParserHelper.parseTypeCast(tokenizer, edm,
        (EdmStructuredType) property.getType());
    if (typeCast != null) {
      ParserHelper.requireNext(tokenizer, TokenKind.SLASH);
    }
    return property.getType().getKind() == EdmTypeKind.COMPLEX ?
        new UriResourceComplexPropertyImpl((EdmProperty) property).setTypeFilter(typeCast) :
        new UriResourceNavigationPropertyImpl((EdmNavigationProperty) property).setCollectionTypeFilter(typeCast);
  } else {
    return null;
  }
}
 
Example #8
Source File: AbstractEdmStructuredType.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public Map<String, EdmNavigationProperty> getNavigationProperties() {
  if (navigationProperties == null) {
    final Map<String, EdmNavigationProperty> localNavigationProperties =
        new LinkedHashMap<String, EdmNavigationProperty>();
    final List<CsdlNavigationProperty> structuredTypeNavigationProperties =
        providerStructuredType.getNavigationProperties();

    if (structuredTypeNavigationProperties != null) {
      for (CsdlNavigationProperty navigationProperty : structuredTypeNavigationProperties) {
        localNavigationProperties.put(navigationProperty.getName(),
            new EdmNavigationPropertyImpl(edm, navigationProperty));
      }
    }

    navigationProperties = Collections.unmodifiableMap(localNavigationProperties);
  }
  return navigationProperties;
}
 
Example #9
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 #10
Source File: EdmNavigationPropertyImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void navigationPropertyWithPartner() 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());

  List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>();
  navigationProperties.add(new CsdlNavigationProperty().setName("partnerName").setType(entityTypeName));
  entityTypeProvider.setNavigationProperties(navigationProperties);
  when(provider.getEntityType(entityTypeName)).thenReturn(entityTypeProvider);
  CsdlNavigationProperty propertyProvider = new CsdlNavigationProperty();
  propertyProvider.setType(entityTypeName);
  propertyProvider.setNullable(false);
  propertyProvider.setPartner("partnerName");
  EdmNavigationProperty property = new EdmNavigationPropertyImpl(edm, propertyProvider);
  EdmNavigationProperty partner = property.getPartner();
  assertNotNull(partner);

  // Caching
  assertTrue(partner == property.getPartner());
}
 
Example #11
Source File: EdmNavigationPropertyImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = EdmException.class)
public void navigationPropertyWithNonexistentPartner() 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());

  List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>();
  navigationProperties.add(new CsdlNavigationProperty().setName("partnerName").setType(entityTypeName));
  entityTypeProvider.setNavigationProperties(navigationProperties);
  when(provider.getEntityType(entityTypeName)).thenReturn(entityTypeProvider);
  CsdlNavigationProperty propertyProvider = new CsdlNavigationProperty();
  propertyProvider.setType(entityTypeName);
  propertyProvider.setNullable(false);
  propertyProvider.setPartner("wrong");
  EdmNavigationProperty property = new EdmNavigationPropertyImpl(edm, propertyProvider);
  property.getPartner();
}
 
Example #12
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 #13
Source File: ExpandSelectMock.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private static UriInfoResource mockResourceOnDerivedEntityAndComplexTypes(
    final String name, final EdmType derivedEntityType, final EdmType derivedComplexType, 
    final String pathSegment) {
  EdmStructuredType type = (EdmStructuredType) derivedEntityType;
  List<UriResource> elements = new ArrayList<UriResource>();
  mockComplexPropertyWithTypeFilter(name, derivedComplexType, type, elements);
  
  final EdmElement edmElement1 = ((EdmStructuredType) derivedComplexType).getProperty(pathSegment);
  UriResourceNavigation element1 = Mockito.mock(UriResourceNavigation.class);
  Mockito.when(element1.getProperty()).thenReturn((EdmNavigationProperty) edmElement1);
  elements.add(element1);
  
  UriInfoResource resource = Mockito.mock(UriInfoResource.class);
  Mockito.when(resource.getUriResourceParts()).thenReturn(elements);
  return resource;
}
 
Example #14
Source File: UriResourceImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void uriResourceNavigationPropertyImpl() {
  EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
  EdmNavigationProperty property = (EdmNavigationProperty) entityType.getProperty("NavPropertyETKeyNavMany");
  assertNotNull(property);

  UriResourceNavigationPropertyImpl impl = new UriResourceNavigationPropertyImpl(property);
  assertEquals(UriResourceKind.navigationProperty, impl.getKind());
  assertEquals(property, impl.getProperty());

  assertEquals("NavPropertyETKeyNavMany", impl.toString());
  assertEquals(property.getType(), impl.getType());

  assertTrue(impl.isCollection());
  impl.setKeyPredicates(Collections.singletonList(
      (UriParameter) new UriParameterImpl().setName("ParameterInt16")));
  assertFalse(impl.isCollection());
}
 
Example #15
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 #16
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 #17
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 #18
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param propertyName
 * @param property
 * @return
 */
private EdmComplexType mockComplexType(final String propertyName, EdmNavigationProperty property) {
  EdmComplexType complexType = Mockito.mock(EdmComplexType.class);
  Mockito.when(complexType.getPropertyNames()).thenReturn(Collections.singletonList(propertyName));
  Mockito.when(complexType.getProperty(propertyName)).thenReturn(property);
  return complexType;
}
 
Example #19
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNavigationPropertyPathExp() throws Exception {
  final String entitySetName = "ESName";
  final String keyPropertyName = "a";
  final String complexPropertyName = "comp";
  final String propertyName = "navProp";
  EdmProperty keyProperty = mockProperty(keyPropertyName, 
      OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String));
  EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty);
  
  EdmEntityType targetEntityType = mockEntityType(keyPropertyName, keyPropertyRef);
  Mockito.when(targetEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.TargetET"));
  EdmNavigationProperty navProperty = mockNavigationProperty(propertyName, targetEntityType);
  
  EdmComplexType complexType = mockComplexType(propertyName, navProperty);
  EdmProperty complexProperty = mockProperty(complexPropertyName, complexType);
  
  EdmEntityType startEntityType = mockEntityType(keyPropertyName, keyPropertyRef);
  Mockito.when(startEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.StartET"));
  Mockito.when(startEntityType.getPropertyNames()).thenReturn(
      Arrays.asList(keyPropertyName, complexPropertyName));
  Mockito.when(startEntityType.getProperty(keyPropertyName)).thenReturn(keyProperty);
  Mockito.when(startEntityType.getProperty(complexPropertyName)).thenReturn(complexProperty);
  EdmEntitySet entitySet = mockEntitySet(entitySetName, startEntityType);
  EdmEntityContainer container = mockContainer(entitySetName, entitySet);
  Edm mockedEdm = Mockito.mock(Edm.class);
  Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container);
  
  UriTokenizer tokenizer = new UriTokenizer("comp/navProp");
  final Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
      startEntityType, null, null);
  assertNotNull(expression);
  assertEquals("[comp, navProp]", expression.toString());
}
 
Example #20
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param propertyName
 * @param entityType
 * @return
 */
private EdmNavigationProperty mockNavigationProperty(final String propertyName, EdmEntityType entityType) {
  EdmNavigationProperty navProperty = Mockito.mock(EdmNavigationProperty.class);
  Mockito.when(navProperty.getName()).thenReturn(propertyName);
  Mockito.when(navProperty.getType()).thenReturn(entityType);
  return navProperty;
}
 
Example #21
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 #22
Source File: ApplyParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the path prefix and a following OData identifier as one path, deviating from the ABNF.
 * @param uriInfo object to be filled with path segments
 * @return a parsed but not used OData identifier */
private String parsePathPrefix(UriInfoImpl uriInfo, final EdmStructuredType referencedType)
    throws UriParserException {
  final EdmStructuredType typeCast = ParserHelper.parseTypeCast(tokenizer, edm, referencedType);
  if (typeCast != null) {
    uriInfo.addResourcePart(new UriResourceStartingTypeFilterImpl(typeCast, true));
    ParserHelper.requireNext(tokenizer, TokenKind.SLASH);
  }
  EdmStructuredType type = typeCast == null ? referencedType : typeCast;
  while (tokenizer.next(TokenKind.ODataIdentifier)) {
    final String name = tokenizer.getText();
    final EdmElement property = type.getProperty(name);
    final UriResource segment = parsePathSegment(property);
    if (segment == null) {
      if (property == null) {
        return name;
      } else {
        uriInfo.addResourcePart(
            property instanceof EdmNavigationProperty ?
                new UriResourceNavigationPropertyImpl((EdmNavigationProperty) property) :
                property.getType().getKind() == EdmTypeKind.COMPLEX ?
                    new UriResourceComplexPropertyImpl((EdmProperty) property) :
                    new UriResourcePrimitivePropertyImpl((EdmProperty) property));
        return null;
      }
    } else {
      uriInfo.addResourcePart(segment);
    }
    type = (EdmStructuredType) ParserHelper.getTypeInformation((UriResourcePartTyped) segment);
  }
  return null;
}
 
Example #23
Source File: PreconditionsValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmBindingTarget getEntitySetFromNavigation(final EdmBindingTarget lastFoundEntitySetOrSingleton,
    final UriResourceNavigation uriResourceNavigation) {
  if (lastFoundEntitySetOrSingleton != null && !uriResourceNavigation.isCollection()) {
    EdmNavigationProperty navProp = uriResourceNavigation.getProperty();
    return lastFoundEntitySetOrSingleton.getRelatedBindingTarget(navProp.getName());
  }
  return null;
}
 
Example #24
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 #25
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 #26
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void createLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
    final Entity destEntity) {
  setLink(navigationProperty, srcEntity, destEntity);

  final EdmNavigationProperty partnerNavigationProperty = navigationProperty.getPartner();
  if (partnerNavigationProperty != null) {
    setLink(partnerNavigationProperty, destEntity, srcEntity);
  }
}
 
Example #27
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 #28
Source File: ExpandSelectMock.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param navProperty
 * @param elements
 * @param property
 */
private static void mockNavPropertyOnEdmType(final String navProperty, List<UriResource> elements,
    final EdmProperty property) {
  final EdmElement edmElement1 = ((EdmStructuredType) property.getType()).getProperty(navProperty);
  UriResourceNavigation element1 = Mockito.mock(UriResourceNavigation.class);
  Mockito.when(element1.getProperty()).thenReturn((EdmNavigationProperty) edmElement1);
  elements.add(element1);
}
 
Example #29
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method return the entity which is able to navigate from the parent entity (source) using uri navigation properties.
 * <p/>
 * In this method we check the parent entities foreign keys and return the entity according to the values.
 * we use ODataDataHandler, navigation properties to get particular foreign keys.
 *
 * @param metadata     Service Metadata
 * @param parentEntity Entity (Source)
 * @param navigation   UriResourceNavigation (Destination)
 * @return Entity (Destination)
 * @throws ODataApplicationException
 * @throws ODataServiceFault
 * @see ODataDataHandler#getNavigationProperties()
 */
private Entity getNavigableEntity(ServiceMetadata metadata, Entity parentEntity, EdmNavigationProperty navigation,
                                  String baseUrl) throws ODataApplicationException, ODataServiceFault {
    EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType()));
    String linkName = navigation.getName();
    List<Property> properties = new ArrayList<>();
    Map<String, EdmProperty> propertyMap = new HashMap<>();
    for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(linkName)
                                               .getNavigationKeys(type.getName())) {
        Property property = parentEntity.getProperty(keys.getForeignKey());
        if (property != null && !property.isNull()) {
            propertyMap.put(keys.getPrimaryKey(), (EdmProperty) type.getProperty(property.getName()));
            property.setName(keys.getPrimaryKey());
            properties.add(property);
        }
    }
    EntityCollection results = null;
    if (!properties.isEmpty()) {
        results = createEntityCollectionFromDataEntryList(linkName, dataHandler
                .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), baseUrl);
    }
    if (results != null && !results.getEntities().isEmpty()) {
        return results.getEntities().get(0);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reference is not found.");
        }
        return null;
    }
}
 
Example #30
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());
}