Java Code Examples for org.apache.olingo.commons.api.edm.EdmPrimitiveType#valueToString()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmPrimitiveType#valueToString() . 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: ActionData.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected static Property primitiveCollectionBoundAction(final String name, final Map<String, Parameter> parameters,
    final Map<String, EntityCollection> data, 
    EdmEntitySet edmEntitySet, List<UriParameter> keyList, final OData oData) throws DataProviderException {
  List<Object> collectionValues = new ArrayList<Object>();
  if ("BAETTwoPrimRTCollString".equals(name)) {
    EdmPrimitiveType strType = oData.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String);
    try {
      String strValue1 = strType.valueToString("ABC", false, 100, null, null, false);
      collectionValues.add(strValue1);
      String strValue2 = strType.valueToString("XYZ", false, 100, null, null, false);
      collectionValues.add(strValue2);
    } catch (EdmPrimitiveTypeException e) {
      throw new DataProviderException("EdmPrimitiveTypeException", HttpStatusCode.BAD_REQUEST, e);
    }
    return new Property(null, name, ValueType.COLLECTION_PRIMITIVE, collectionValues);
  }
  throw new DataProviderException("Action " + name + " is not yet implemented.",
      HttpStatusCode.NOT_IMPLEMENTED);
}
 
Example 2
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns the object which is the value of the property.
 *
 * @param edmProperty EdmProperty
 * @param value       String value
 * @return Object
 * @throws ODataApplicationException
 */
private String readPrimitiveValueInString(EdmProperty edmProperty, Object value) throws ODataApplicationException {
    if (value == null) {
        return null;
    }
    try {
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
        return edmPrimitiveType.valueToString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                              edmProperty.getPrecision(), edmProperty.getScale(),
                                              edmProperty.isUnicode());
    } catch (EdmPrimitiveTypeException e) {
        throw new ODataApplicationException("Invalid value: " + value + " for property: " + edmProperty.getName(),
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                            Locale.getDefault());
    }
}
 
Example 3
Source File: EdmEnumTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueToString(final EdmPrimitiveType instance, final Object value,
    final Boolean isNullable, final String message) {
  try {
    instance.valueToString(value, isNullable, null, null, null, null);
    fail("Expected exception not thrown");
  } catch (final EdmPrimitiveTypeException e) {
    assertNotNull(e.getLocalizedMessage());
    assertThat(e.getLocalizedMessage(), containsString(message));
  }
}
 
Example 4
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writePrimitiveValue(final EdmPrimitiveType type, final Object primitiveValue,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final String xml10InvalidCharReplacement,
    final XMLStreamWriter writer) throws EdmPrimitiveTypeException, XMLStreamException {
  final String value = type.valueToString(primitiveValue,
      isNullable, maxLength, precision, scale, isUnicode);
  if (value == null) {
    writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_NULL, "true");
  } else {
    // XML 1.0 does not handle certain unicode characters, they need to be replaced
    writer.writeCharacters(replaceInvalidCharacters(type, value,
        isUnicode, xml10InvalidCharReplacement));
  }
}
 
Example 5
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 6
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 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;
    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: 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 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 = 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 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 = 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 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 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 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;
    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 (matches) {
      // if the given key value is found in the current entity, continue with the next key
      continue;
    } else {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  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: EdmAssistedJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void primitiveValue(final JsonGenerator json, final EdmPrimitiveType valueType, final String typeName,
    final EdmProperty edmProperty, final Object value) throws IOException, SerializerException {

  EdmPrimitiveType type = valueType;
  if (type == null) {
    final EdmPrimitiveTypeKind kind =
        typeName == null ? EdmTypeInfo.determineTypeKind(value) : new EdmTypeInfo.Builder().setTypeExpression(
            typeName).build().getPrimitiveTypeKind();
    type = kind == null ? null : EdmPrimitiveTypeFactory.getInstance(kind);
  }

  if (value == null) {
    json.writeNull();
  } else if (type == null) {
    throw new SerializerException("The primitive type could not be determined.",
        MessageKeys.INCONSISTENT_PROPERTY_TYPE, "");
  } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean)) {
    json.writeBoolean((Boolean) value);
  } else {
    String serialized = null;
    try {
      serialized = type.valueToString(value,
          edmProperty == null ? null : edmProperty.isNullable(),
          edmProperty == null ? null : edmProperty.getMaxLength(),
          edmProperty == null ? Constants.DEFAULT_PRECISION : edmProperty.getPrecision(),
          edmProperty == null ? Constants.DEFAULT_SCALE : edmProperty.getScale(),
          edmProperty == null ? null : edmProperty.isUnicode());
    } catch (final EdmPrimitiveTypeException e) {
      final String name = edmProperty == null ? "" : edmProperty.getName();
      throw new SerializerException("Wrong value for property '" + name + "'!", e,
          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE, name, value.toString());
    }
    if (isIEEE754Compatible &&
        (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int64)
            || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Decimal))
        || type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Byte)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.SByte)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Single)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Double)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int16)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int32)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int64)
            && type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Decimal)) {
      json.writeString(serialized);
    } else {
      json.writeNumber(serialized);
    }
  }
}
 
Example 18
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void writePrimitiveValue(final String name, final EdmPrimitiveType type, final Object primitiveValue,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final JsonGenerator json) throws EdmPrimitiveTypeException, IOException {
  final String value = type.valueToString(primitiveValue,
      isNullable, maxLength, precision, scale, isUnicode);
  if (value == null) {
    json.writeNull();
  } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean)) {
    json.writeBoolean(Boolean.parseBoolean(value));
  } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Byte)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Double)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int16)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int32)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.SByte)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Single)
      || (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Decimal)
      || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int64))
      && !isIEEE754Compatible) {
    json.writeNumber(value);
  } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Stream)) {
    if (primitiveValue instanceof Link) {
      Link stream = (Link)primitiveValue;
      if (!isODataMetadataNone) {
        if (stream.getMediaETag() != null) {
          json.writeStringField(name+constants.getMediaEtag(), stream.getMediaETag());
        }
        if (stream.getType() != null) {
          json.writeStringField(name+constants.getMediaContentType(), stream.getType());
        }
      }
      if (isODataMetadataFull) {
        if (stream.getRel() != null && stream.getRel().equals(Constants.NS_MEDIA_READ_LINK_REL)) {
          json.writeStringField(name+constants.getMediaReadLink(), stream.getHref());
        }
        if (stream.getRel() == null || stream.getRel().equals(Constants.NS_MEDIA_EDIT_LINK_REL)) {
          json.writeStringField(name+constants.getMediaEditLink(), stream.getHref());
        }
      }
    }
  } else {
    json.writeString(value);
  }
}
 
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;
}