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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmPrimitiveType#valueOfString() . 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: 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 Object readPrimitiveValue(EdmProperty edmProperty, String value) throws ODataApplicationException {
    if (value == null) {
        return null;
    }
    try {
        if (value.startsWith("'") && value.endsWith("'")) {
            value = value.substring(1, value.length() - 1);
        }
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
        Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmPrimitiveType);
        return edmPrimitiveType.valueOfString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                              edmProperty.getPrecision(), edmProperty.getScale(),
                                              edmProperty.isUnicode(), javaClass);
    } catch (EdmPrimitiveTypeException e) {
        throw new ODataApplicationException("Invalid value: " + value + " for property: " + edmProperty.getName(),
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                            Locale.getDefault());
    }
}
 
Example 2
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Object readPrimitiveValue(final String name, final EdmPrimitiveType type,
    final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final boolean isUnicode, final EdmMapping mapping, final JsonNode jsonNode) throws DeserializerException {
  if (isValidNull(name, isNullable, jsonNode)) {
    return null;
  }
  final boolean isGeoType = type.getName().startsWith("Geo");
  if (!isGeoType) {
    checkForValueNode(name, jsonNode);
  }
  checkJsonTypeBasedOnPrimitiveType(name, type, jsonNode);
  try {
    if (isGeoType) {
      return readPrimitiveGeoValue(name, type, (ObjectNode) jsonNode);
    }
    return type.valueOfString(jsonNode.asText(),
        isNullable, maxLength, precision, scale, isUnicode,
        getJavaClassForPrimitiveType(mapping, type));
  } catch (final EdmPrimitiveTypeException e) {
    throw new DeserializerException(
        "Invalid value: " + jsonNode.asText() + " for property: " + name, e,
        DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, name);
  }
}
 
Example 3
Source File: TripPinDataModel.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static Object readPrimitiveValue(EdmProperty edmProperty, String value)
    throws ODataApplicationException {
  if (value == null) {
    return null;
  }
  try {
    if (value.startsWith("'") && value.endsWith("'")) {
      value = value.substring(1,value.length()-1);
    }
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
    Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmPrimitiveType);
    return edmPrimitiveType.valueOfString(value, edmProperty.isNullable(),
        edmProperty.getMaxLength(), edmProperty.getPrecision(), edmProperty.getScale(),
        edmProperty.isUnicode(), javaClass);
  } catch (EdmPrimitiveTypeException e) {
    throw new ODataApplicationException("Invalid value: " + value + " for property: "
        + edmProperty.getName(), 500, Locale.getDefault());
  }
}
 
Example 4
Source File: VisitorOperand.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
protected Object castTo(final String value, final EdmPrimitiveType type) throws EdmPrimitiveTypeException {
    final EdmProperty edmProperty = getEdmProperty();
    if (edmProperty != null) {
        return type.valueOfString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                  edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(),
                                  getDefaultType(type));
    } else {
        return type.valueOfString(value, null, null, null, null, null, getDefaultType(type));
    }
}
 
Example 5
Source File: VisitorOperand.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected Object castTo(final String value, final EdmPrimitiveType type) throws EdmPrimitiveTypeException {
  final EdmProperty edmProperty = getEdmProperty();

  if (edmProperty != null) {
    return type.valueOfString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
        edmProperty.getPrecision(), edmProperty.getScale(),
        edmProperty.isUnicode(), getDefaultType(type));
  } else {
    return type.valueOfString(value, null, null, null, null, null, getDefaultType(type));
  }
}
 
Example 6
Source File: PrimitiveTypeBaseTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueOfString(final EdmPrimitiveType instance,
    final String value, final Boolean isNullable, final Integer maxLength, final Integer precision,
    final Integer scale, final Boolean isUnicode, final Class<?> returnType,
    final String message) {

  try {
    instance.valueOfString(value, isNullable, maxLength, precision, scale, isUnicode, returnType);
    fail("Expected exception not thrown");
  } catch (final EdmPrimitiveTypeException e) {
    assertNotNull(e.getLocalizedMessage());
    assertThat(e.getLocalizedMessage(), containsString(message));
  }
}
 
Example 7
Source File: EdmEnumTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueOfString(final EdmPrimitiveType instance, final String value,
    final Boolean isNullable, final Class<?> returnType, final String message) {
  try {
    instance.valueOfString(value, isNullable, null, null, null, null, returnType);
    fail("Expected exception not thrown");
  } catch (final EdmPrimitiveTypeException e) {
    assertNotNull(e.getLocalizedMessage());
    assertThat(e.getLocalizedMessage(), containsString(message));
  }
}
 
Example 8
Source File: ODataXmlDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Object primitive(final XMLEventReader reader, final StartElement start,
    final EdmType type, final boolean isNullable, final Integer maxLength, final Integer precision,
    final Integer scale, final boolean isUnicode) throws XMLStreamException, EdmPrimitiveTypeException,
    DeserializerException {

  Object value = null;

  boolean foundEndProperty = false;
  while (reader.hasNext() && !foundEndProperty) {
    final XMLEvent event = reader.nextEvent();

    if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
      if (type instanceof AbstractGeospatialType<?>) {
        throw new DeserializerException("geo types support not implemented",
            DeserializerException.MessageKeys.NOT_IMPLEMENTED);
      }
      final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type;
      final String stringValue = event.asCharacters().getData();
      value = primitiveType.valueOfString(stringValue,
          isNullable,
          maxLength,
          precision,
          scale,
          isUnicode,
          primitiveType.getDefaultType());
    }

    if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
      foundEndProperty = true;
    }
  }
  return value;
}
 
Example 9
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static void validateFunctionParameterFacets(final EdmFunction function, 
    final List<UriParameter> parameters, Edm edm, Map<String, AliasQueryOption> aliases) 
        throws UriParserException, UriValidationException {
  for (UriParameter parameter : parameters) {
    EdmParameter edmParameter = function.getParameter(parameter.getName());
    final EdmType type = edmParameter.getType();
    final EdmTypeKind kind = type.getKind();
    if ((kind == EdmTypeKind.PRIMITIVE || kind == EdmTypeKind.DEFINITION || kind == EdmTypeKind.ENUM)
        && !edmParameter.isCollection()) {
      final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type;
      String text = null;
      try {
        text = parameter.getAlias() == null ?
            parameter.getText() :
              aliases.containsKey(parameter.getAlias()) ?
                  parseAliasValue(parameter.getAlias(),
                      edmParameter.getType(), edmParameter.isNullable(), edmParameter.isCollection(),
                      edm, type, aliases).getText() : null;
                      if (edmParameter.getMapping() == null) {
                        primitiveType.valueOfString(primitiveType.fromUriLiteral(text),
                            edmParameter.isNullable(), edmParameter.getMaxLength(), edmParameter.getPrecision(), 
                            edmParameter.getScale(), true, primitiveType.getDefaultType());
                      } else {
                        primitiveType.valueOfString(primitiveType.fromUriLiteral(text),
                            edmParameter.isNullable(), edmParameter.getMaxLength(), edmParameter.getPrecision(), 
                            edmParameter.getScale(), true, edmParameter.getMapping().getMappedJavaClass());
                      }
      } catch (final EdmPrimitiveTypeException e) {
        throw new UriValidationException(
            "Invalid value '" + text + "' for parameter " + parameter.getName(), e,
            UriValidationException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, parameter.getName());
      }
    }
  }
}
 
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: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected Object valueOf(final String value, final EdmPrimitiveTypeKind kind) throws EdmPrimitiveTypeException {
  final EdmPrimitiveType type = OData.newInstance().createPrimitiveTypeInstance(kind);
  return type.valueOfString(value, true, null, null, null, true, type.getDefaultType());
}