Java Code Examples for org.apache.olingo.commons.api.edm.EdmKeyPropertyRef#getProperty()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmKeyPropertyRef#getProperty() . 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: EdmEntityTypeImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void keyBehaviour() {
  List<String> keyPredicateNames = baseType.getKeyPredicateNames();
  assertEquals(1, keyPredicateNames.size());
  assertEquals("Id", keyPredicateNames.get(0));

  EdmKeyPropertyRef keyPropertyRef = baseType.getKeyPropertyRef("Id");
  assertNotNull(keyPropertyRef);
  assertEquals("Id", keyPropertyRef.getName());
  assertNull(keyPropertyRef.getAlias());

  EdmProperty keyProperty = keyPropertyRef.getProperty();
  assertNotNull(keyProperty);
  assertEquals(baseType.getProperty("Id"), keyProperty);

  List<EdmKeyPropertyRef> keyPropertyRefs = baseType.getKeyPropertyRefs();
  assertNotNull(keyPropertyRefs);
  assertEquals(1, keyPropertyRefs.size());
  assertEquals("Id", keyPropertyRefs.get(0).getName());
}
 
Example 2
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void aliasForPropertyInComplexPropertyOneLevel() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("comp/Id").setAlias("alias");
  EdmEntityType etMock = mock(EdmEntityType.class);
  EdmProperty keyPropertyMock = mock(EdmProperty.class);
  EdmProperty compMock = mock(EdmProperty.class);
  EdmComplexType compTypeMock = mock(EdmComplexType.class);
  when(compTypeMock.getStructuralProperty("Id")).thenReturn(keyPropertyMock);
  when(compMock.getType()).thenReturn(compTypeMock);
  when(etMock.getStructuralProperty("comp")).thenReturn(compMock);
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(etMock, providerRef);
  assertEquals("alias", ref.getAlias());

  EdmProperty property = ref.getProperty();
  assertNotNull(property);
  assertTrue(property == keyPropertyMock);
}
 
Example 3
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void aliasForPropertyInComplexPropertyTwoLevels() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("comp/comp2/Id").setAlias("alias");
  EdmEntityType etMock = mock(EdmEntityType.class);
  EdmProperty keyPropertyMock = mock(EdmProperty.class);
  EdmProperty compMock = mock(EdmProperty.class);
  EdmComplexType compTypeMock = mock(EdmComplexType.class);
  EdmProperty comp2Mock = mock(EdmProperty.class);
  EdmComplexType comp2TypeMock = mock(EdmComplexType.class);
  when(comp2TypeMock.getStructuralProperty("Id")).thenReturn(keyPropertyMock);
  when(comp2Mock.getType()).thenReturn(comp2TypeMock);
  when(compTypeMock.getStructuralProperty("comp2")).thenReturn(comp2Mock);
  when(compMock.getType()).thenReturn(compTypeMock);
  when(etMock.getStructuralProperty("comp")).thenReturn(compMock);
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(etMock, providerRef);

  EdmProperty property = ref.getProperty();
  assertNotNull(property);
  assertTrue(property == keyPropertyMock);
}
 
Example 4
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private static UriParameter keyValuePair(UriTokenizer tokenizer,
    final String keyPredicateName, final EdmEntityType edmEntityType,
    final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  final EdmKeyPropertyRef keyPropertyRef = edmEntityType.getKeyPropertyRef(keyPredicateName);
  final EdmProperty edmProperty = keyPropertyRef == null ? null : keyPropertyRef.getProperty();
  if (edmProperty == null) {
    throw new UriValidationException(keyPredicateName + " is not a valid key property name.",
        UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, keyPredicateName);
  }
  ParserHelper.requireNext(tokenizer, TokenKind.EQ);
  if (tokenizer.next(TokenKind.COMMA) || tokenizer.next(TokenKind.CLOSE) || tokenizer.next(TokenKind.EOF)) {
    throw new UriParserSyntaxException("Key value expected.", UriParserSyntaxException.MessageKeys.SYNTAX);
  }
  if (nextPrimitiveTypeValue(tokenizer, (EdmPrimitiveType) edmProperty.getType(), edmProperty.isNullable())) {
    return createUriParameter(edmProperty, keyPredicateName, tokenizer.getText(), edm, referringType, aliases);
  } else {
    throw new UriParserSemanticException(keyPredicateName + " has not a valid  key value.",
        UriParserSemanticException.MessageKeys.INVALID_KEY_VALUE, keyPredicateName);
  }
}
 
Example 5
Source File: ActionData.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param keyList
 * @param edmEntitySet
 * @param values
 * @param propertyNames
 * @throws DataProviderException
 */
private static void setBindingPropertyKeyNameAndValue(List<UriParameter> keyList, EdmEntitySet edmEntitySet,
    List<Object> values, List<String> propertyNames) throws DataProviderException {
  for (final UriParameter key : keyList) {
    EdmKeyPropertyRef refType = edmEntitySet.getEntityType().getKeyPropertyRef(key.getName());
    final EdmProperty property = refType.getProperty();
    final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
    try {
      values.add(type.fromUriLiteral(key.getText()));
    } catch (EdmPrimitiveTypeException e) {
      throw new DataProviderException("Wrong key!", HttpStatusCode.BAD_REQUEST, e);
    }
    propertyNames.add(key.getName());
  }
}
 
Example 6
Source File: EdmEntityTypeImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void complexKeyWithAlias() {
  List<String> keyPredicateNames = typeWithComplexKey.getKeyPredicateNames();
  assertEquals(2, keyPredicateNames.size());
  assertEquals("Id", keyPredicateNames.get(0));
  assertEquals("alias", keyPredicateNames.get(1));

  EdmKeyPropertyRef keyPropertyRef = typeWithComplexKey.getKeyPropertyRef("Id");
  assertNotNull(keyPropertyRef);
  assertEquals("Id", keyPropertyRef.getName());
  assertNull(keyPropertyRef.getAlias());
  EdmProperty keyProperty = keyPropertyRef.getProperty();
  assertNotNull(keyProperty);
  assertEquals(typeWithComplexKey.getProperty("Id"), keyProperty);

  keyPropertyRef = typeWithComplexKey.getKeyPropertyRef("alias");
  assertNotNull(keyPropertyRef);
  assertEquals("Comp/ComplexPropName", keyPropertyRef.getName());
  assertEquals("alias", keyPropertyRef.getAlias());

  keyProperty = keyPropertyRef.getProperty();
  assertNotNull(keyProperty);
  EdmElement complexProperty = typeWithComplexKey.getProperty("Comp");
  EdmComplexType complexType = (EdmComplexType) complexProperty.getType();
  assertNotNull(complexType);
  assertEquals(complexType.getProperty("ComplexPropName"), keyProperty);
}
 
Example 7
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void noAlias() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("Id");
  EdmEntityType etMock = mock(EdmEntityType.class);
  EdmProperty keyPropertyMock = mock(EdmProperty.class);
  when(etMock.getStructuralProperty("Id")).thenReturn(keyPropertyMock);
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(etMock, providerRef);
  assertEquals("Id", ref.getName());
  assertNull(ref.getAlias());

  EdmProperty property = ref.getProperty();
  assertNotNull(property);
  assertTrue(property == keyPropertyMock);
  assertTrue(property == ref.getProperty());
}
 
Example 8
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private static UriParameter simpleKey(UriTokenizer tokenizer, final EdmKeyPropertyRef edmKeyPropertyRef,
    final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  final EdmProperty edmProperty = edmKeyPropertyRef == null ? null : edmKeyPropertyRef.getProperty();
  if (nextPrimitiveTypeValue(tokenizer,
      edmProperty == null ? null : (EdmPrimitiveType) edmProperty.getType(),
      edmProperty == null ? false : edmProperty.isNullable())) {
    final String literalValue = tokenizer.getText();
    ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
    return createUriParameter(edmProperty, edmKeyPropertyRef.getName(), literalValue, edm, referringType, aliases);
  } else {
    return null;
  }
}
 
Example 9
Source File: UriHelperImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public String buildKeyPredicate(final EdmEntityType edmEntityType, final Entity entity) throws SerializerException {
  StringBuilder result = new StringBuilder();
  final List<String> keyNames = edmEntityType.getKeyPredicateNames();
  boolean first = true;
  for (final String keyName : keyNames) {
    EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(keyName);
    if (first) {
      first = false;
    } else {
      result.append(',');
    }
    if (keyNames.size() > 1) {
      result.append(Encoder.encode(keyName)).append('=');
    }
    final EdmProperty edmProperty =  refType.getProperty();
    if (edmProperty == null) {
      throw new SerializerException("Property not found (possibly an alias): " + keyName,
          SerializerException.MessageKeys.MISSING_PROPERTY, keyName);
    }
    final EdmPrimitiveType type = (EdmPrimitiveType) edmProperty.getType();
    final Object propertyValue = findPropertyRefValue(entity, refType);
    try {
      final String value = type.toUriLiteral(
          type.valueToString(propertyValue,
              edmProperty.isNullable(), edmProperty.getMaxLength(),
              edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()));
      result.append(Encoder.encode(value));
    } catch (final EdmPrimitiveTypeException e) {
      throw new SerializerException("Wrong key value!", e,
          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE, edmProperty.getName(), 
          propertyValue != null ? propertyValue.toString(): null);
    }
  }
  return result.toString();
}
 
Example 10
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public Entity read(final EdmEntityType edmEntityType, final EntityCollection entitySet,
    final List<UriParameter> keys) throws DataProviderException {
  try {
    for (final Entity entity : entitySet.getEntities()) {
      boolean found = true;
      for (final UriParameter key : keys) {
        EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(key.getName());
        Object value =  findPropertyRefValue(entity, refType);
        
        final EdmProperty property = refType.getProperty();
        final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
        
        if (key.getExpression() != null && !(key.getExpression() instanceof Literal)) {
          throw new DataProviderException("Expression in key value is not supported yet!",
              HttpStatusCode.NOT_IMPLEMENTED);
        }
        Object keyValue = null;
        final String text = key.getAlias() == null ? key.getText() : ((Literal) key.getExpression()).getText();
        if (Calendar.class.isAssignableFrom(value.getClass())) {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), Calendar.class);
        } else {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), value.getClass());
        }
        if (!value.equals(keyValue)) {
          found = false;
          break;
        }
      }
      if (found) {
        return entity;
      }
    }
    return null;
  } catch (final EdmPrimitiveTypeException e) {
    throw new DataProviderException("Wrong key!", HttpStatusCode.BAD_REQUEST, e);
  }
}
 
Example 11
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public Entity readDataFromEntity(final EdmEntityType edmEntityType,
    final List<UriParameter> keys) throws DataProviderException {
  EntityCollection coll = data.get(edmEntityType.getName());
  List<Entity> entities = coll.getEntities();
  try {
    for (final Entity entity : entities) {
      boolean found = true;
      for (final UriParameter key : keys) {
        EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(key.getName());
        Object value =  findPropertyRefValue(entity, refType);
        
        final EdmProperty property = refType.getProperty();
        final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
        
        if (key.getExpression() != null && !(key.getExpression() instanceof Literal)) {
          throw new DataProviderException("Expression in key value is not supported yet!",
              HttpStatusCode.NOT_IMPLEMENTED);
        }
        final String text = key.getAlias() == null ? key.getText() : ((Literal) key.getExpression()).getText();
        Object keyValue = null;
        if (Calendar.class.isAssignableFrom(value.getClass())) {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), Calendar.class);
        } else {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), value.getClass());
        }
        if (!value.equals(keyValue)) {
          found = false;
          break;
        }
      }
      if (found) {
        return entity;
      }
    }
    return null;
  } catch (final EdmPrimitiveTypeException e) {
    throw new DataProviderException("Wrong key!", HttpStatusCode.BAD_REQUEST, e);
  }
}
 
Example 12
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test(expected = EdmException.class)
public void oneKeyNoAliasButInvalidProperty() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("Id");
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(mock(EdmEntityType.class), providerRef);
  ref.getProperty();
}
 
Example 13
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test(expected = EdmException.class)
public void aliasButNoPath() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("Id").setAlias("alias");
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(mock(EdmEntityType.class), providerRef);
  ref.getProperty();
}
 
Example 14
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test(expected = EdmException.class)
public void aliasButEmptyPath() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("").setAlias("alias");
  EdmKeyPropertyRef ref = new EdmKeyPropertyRefImpl(mock(EdmEntityType.class), providerRef);
  ref.getProperty();
}