Java Code Examples for org.apache.olingo.commons.api.edm.EdmProperty#isNullable()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmProperty#isNullable() . 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: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void consumeEntityProperties(final EdmEntityType edmEntityType, final ObjectNode node,
    final Entity entity) throws DeserializerException {
  List<String> propertyNames = edmEntityType.getPropertyNames();
  for (String propertyName : propertyNames) {
    JsonNode jsonNode = node.get(propertyName);
    if (jsonNode != null) {
      EdmProperty edmProperty = (EdmProperty) edmEntityType.getProperty(propertyName);
      if (jsonNode.isNull() && !edmProperty.isNullable()) {
        throw new DeserializerException("Property: " + propertyName + " must not be null.",
            DeserializerException.MessageKeys.INVALID_NULL_PROPERTY, propertyName);
      }
      Property property = consumePropertyNode(edmProperty.getName(), edmProperty.getType(),
          edmProperty.isCollection(), edmProperty.isNullable(), edmProperty.getMaxLength(),
          edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), edmProperty.getMapping(),
          jsonNode);
      entity.addProperty(property);
      node.remove(propertyName);
    }
  }
}
 
Example 2
Source File: UriValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void validatePropertyOperations(final UriInfo uriInfo, final HttpMethod method)
    throws UriValidationException {
  final List<UriResource> parts = uriInfo.getUriResourceParts();
  final UriResource last = !parts.isEmpty() ? parts.get(parts.size() - 1) : null;
  final UriResource previous = parts.size() > 1 ? parts.get(parts.size() - 2) : null;
  if (last != null
      && (last.getKind() == UriResourceKind.primitiveProperty
      || last.getKind() == UriResourceKind.complexProperty
      || (last.getKind() == UriResourceKind.value
          && previous != null && previous.getKind() == UriResourceKind.primitiveProperty))) {
    final EdmProperty property = ((UriResourceProperty)
        (last.getKind() == UriResourceKind.value ? previous : last)).getProperty();
    if (method == HttpMethod.PATCH && property.isCollection()) {
      throw new UriValidationException("Attempt to patch collection property.",
          UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString());
    }
    if (method == HttpMethod.DELETE && !property.isNullable()) {
      throw new UriValidationException("Attempt to delete non-nullable property.",
          UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString());
    }
  }
}
 
Example 3
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private ComplexValue createComplexValue(final EdmProperty edmProperty, final ComplexValue complexValue,
    final boolean patch) throws DataProviderException {
  final ComplexValue result = new ComplexValue();
  EdmComplexType edmType = (EdmComplexType) edmProperty.getType();
  final List<Property> givenProperties = complexValue.getValue();
  if(complexValue.getTypeName()!=null){
    EdmComplexType derivedType = edm.getComplexType(new FullQualifiedName(complexValue.getTypeName()));
    if(derivedType.getBaseType()!=null && edmType.getFullQualifiedName().getFullQualifiedNameAsString()
        .equals(derivedType.getBaseType().getFullQualifiedName().getFullQualifiedNameAsString())){
    edmType = derivedType;
    }
  }
  // Create ALL properties, even if no value is given. Check if null is allowed
  for (final String propertyName : edmType.getPropertyNames()) {
    final EdmProperty innerEdmProperty = (EdmProperty) edmType.getProperty(propertyName);
    final Property currentProperty = findProperty(propertyName, givenProperties);
    final Property newProperty = createProperty(innerEdmProperty, propertyName);
    result.getValue().add(newProperty);

    if (currentProperty != null) {
      updateProperty(innerEdmProperty, newProperty, currentProperty, patch);
    } else {
      if (innerEdmProperty.isNullable()) {
        // Check complex properties ... may be null is not allowed
        if (edmProperty.getType().getKind() == EdmTypeKind.COMPLEX) {
          updateProperty(innerEdmProperty, newProperty, null, patch);
        }
      }
    }
  }
  result.setTypeName(edmType.getFullQualifiedName().getFullQualifiedNameAsString());
  return result;
}
 
Example 4
Source File: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void deleteProperty(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final boolean isValue) throws ODataLibraryException, ODataApplicationException {
  final UriInfoResource resource = uriInfo.asUriInfoResource();
  validatePath(resource);
  getEdmEntitySet(uriInfo); // including checks

  Entity entity = readEntity(uriInfo);
  odata.createETagHelper().checkChangePreconditions(entity.getETag(),
      request.getHeaders(HttpHeader.IF_MATCH),
      request.getHeaders(HttpHeader.IF_NONE_MATCH));

  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final int trailing = isValue ? 1 : 0;
  final List<String> path = getPropertyPath(resourceParts, trailing);

  Property property = getPropertyData(entity, path);

  final EdmProperty edmProperty = ((UriResourceProperty) resourceParts.get(resourceParts.size() - trailing - 1))
      .getProperty();

  if (edmProperty.isNullable()) {
    property.setValue(property.getValueType(), edmProperty.isCollection() ? Collections.emptyList() : null);
    dataProvider.updateETag(entity);
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    if (entity.getETag() != null) {
      response.setHeader(HttpHeader.ETAG, entity.getETag());
    }
  } else {
    throw new ODataApplicationException("Not nullable.", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
  }
}
 
Example 5
Source File: PrimitiveSerializerOptions.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/** Sets all facets from an EDM property. */
public Builder facetsFrom(final EdmProperty property) {
  options.isNullable = property.isNullable();
  options.maxLength = property.getMaxLength();
  options.precision = property.getPrecision();
  options.scale = property.getScale();
  options.isUnicode = property.isUnicode();
  return this;
}
 
Example 6
Source File: PrimitiveValueSerializerOptions.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/** Sets all facets from an EDM property. */
public Builder facetsFrom(final EdmProperty property) {
  options.isNullable = property.isNullable();
  options.maxLength = property.getMaxLength();
  options.precision = property.getPrecision();
  options.scale = property.getScale();
  options.isUnicode = property.isUnicode();
  return this;
}
 
Example 7
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 8
Source File: EdmTypeConvertor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
public PropertyMetadata visit(EdmProperty property) {
    this.nullable = property.isNullable();
    this.isCollection = property.isCollection();
    return visit(property.getType());
}
 
Example 9
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
              .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 10
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity rt_entity,
    List<UriParameter> keyParams) {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // note: below line doesn't consider: keyProp can be part of a complexType in V4
    // in such case, it would be required to access it via getKeyPropertyRef()
    // but since this isn't the case in our model, we ignore it in our implementation
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    // Edm: we need this info for the comparison below
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    // if(EdmType instanceof EdmPrimitiveType) // do we need this?
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in FWK
    Object valueObject = rt_entity.getProperty(keyName).getValue();
    // TODO if the property is a complex type

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable,
          maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      return false; // TODO proper Exception handling
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    // if any of the key properties is not found in the entity, we don't need to search further
    if (!matches) {
      return false;
    }
    // if the given key value is found in the current entity, continue with the next key
  }

  return true;
}
 
Example 11
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
              .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 12
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean
    entityMatchesAllKeys(EdmEntityType edmEntityType, Entity rt_entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    // if(EdmType instanceof EdmPrimitiveType) // do we need this?
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = rt_entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using type.valueToString
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value",
				HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 13
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 14
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 15
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity rt_entity,
    List<UriParameter> keyParams) {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // note: below line doesn't consider: keyProp can be part of a complexType in V4
    // in such case, it would be required to access it via getKeyPropertyRef()
    // but since this isn't the case in our model, we ignore it in our implementation
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    // Edm: we need this info for the comparison below
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    // if(EdmType instanceof EdmPrimitiveType) // do we need this?
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in FWK
    Object valueObject = rt_entity.getProperty(keyName).getValue();
    // TODO if the property is a complex type

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable,
          maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      return false; // TODO proper Exception handling
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    // if any of the key properties is not found in the entity, we don't need to search further
    if (!matches) {
      return false;
    }
    // if the given key value is found in the current entity, continue with the next key
  }

  return true;
}
 
Example 16
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 17
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 18
Source File: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void updateProperty(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final ContentType requestFormat, final ContentType responseFormat, final RepresentationType representationType)
    throws ODataApplicationException, ODataLibraryException {
  final UriInfoResource resource = uriInfo.asUriInfoResource();
  validatePath(resource);
  final EdmEntitySet edmEntitySet = getEdmEntitySet(resource);

  Entity entity = readEntity(uriInfo);
  odata.createETagHelper().checkChangePreconditions(entity.getETag(),
      request.getHeaders(HttpHeader.IF_MATCH),
      request.getHeaders(HttpHeader.IF_NONE_MATCH));

  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final int trailing = representationType == RepresentationType.VALUE ? 1 : 0;
  final List<String> path = getPropertyPath(resourceParts, trailing);
  final EdmProperty edmProperty = ((UriResourceProperty) resourceParts.get(resourceParts.size() - trailing - 1))
      .getProperty();

  Property property = getPropertyData(entity, path);

  if (representationType == RepresentationType.VALUE || 
  		edmProperty.getType().getFullQualifiedName()
  		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
    final FixedFormatDeserializer deserializer = odata.createFixedFormatDeserializer();
    final Object value = edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) 
  		  || edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Stream) ?
        deserializer.binary(request.getBody()) :
        deserializer.primitiveValue(request.getBody(), edmProperty);
    dataProvider.updatePropertyValue(property, value);
  } else {
    final Property changedProperty = odata.createDeserializer(requestFormat)
        .property(request.getBody(), edmProperty).getProperty();
    if (changedProperty.isNull() && !edmProperty.isNullable()) {
      throw new ODataApplicationException("Not nullable.", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
    dataProvider.updateProperty(edmProperty, property, changedProperty, request.getMethod() == HttpMethod.PATCH);
  }

  dataProvider.updateETag(entity);

  final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
  if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    if (representationType == RepresentationType.VALUE || 
      		edmProperty.getType().getFullQualifiedName()
        		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
      response.setContent(
          serializePrimitiveValue(property, edmProperty, (EdmPrimitiveType) edmProperty.getType(), null));
    } else {
      final SerializerResult result = serializeProperty(entity, edmEntitySet, path, property, edmProperty,
          edmProperty.getType(), null, representationType, responseFormat, null, null);
      response.setContent(result.getContent());
    }
    if (edmProperty.getType().getFullQualifiedName()
      		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
    	  response.setHeader(HttpHeader.CONTENT_TYPE, requestFormat.toContentTypeString());
      } else {
    	  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
      }
  } else {
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
  if (returnPreference != null) {
    response.setHeader(HttpHeader.PREFERENCE_APPLIED,
        PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
  }
  if (entity.getETag() != null) {
    response.setHeader(HttpHeader.ETAG, entity.getETag());
  }
}
 
Example 19
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
              .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 20
Source File: Util.java    From syndesis with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
    throws ODataApplicationException {

    // loop over all keys
    for (final UriParameter key : keyParams) {
        // key
        String keyName = key.getName();
        String keyText = key.getText();

        // Edm: we need this info for the comparison below
        EdmProperty edmKeyProperty = (EdmProperty)edmEntityType.getProperty(keyName);
        Boolean isNullable = edmKeyProperty.isNullable();
        Integer maxLength = edmKeyProperty.getMaxLength();
        Integer precision = edmKeyProperty.getPrecision();
        Boolean isUnicode = edmKeyProperty.isUnicode();
        Integer scale = edmKeyProperty.getScale();
        // get the EdmType in order to compare
        EdmType edmType = edmKeyProperty.getType();
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType)edmType;

        // Runtime data: the value of the current entity
        // don't need to check for null, this is done in olingo library
        Object valueObject = entity.getProperty(keyName).getValue();

        // now need to compare the valueObject with the keyText String
        // this is done using the type.valueToString
        String valueAsString;
        try {
            valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
        } catch (EdmPrimitiveTypeException e) {
            throw new ODataApplicationException("Failed to retrieve String value",
                                                HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH, e);
        }

        if (valueAsString == null) {
            return false;
        }

        boolean matches = valueAsString.equals(keyText);
        if (!matches) {
            // if any of the key properties is not found in the entity, we don't need to search further
            return false;
        }
    }

    return true;
}