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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmEntityType#getKeyPropertyRef() . 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: 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 2
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 3
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 4
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);
  }
}