Java Code Examples for org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind#valueOf()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind#valueOf() . 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 5 votes vote down vote up
private void checkJsonTypeBasedOnPrimitiveType(final String propertyName, final EdmPrimitiveType edmPrimitiveType,
    final JsonNode jsonNode) throws DeserializerException {
  boolean valid = true;
  if (edmPrimitiveType.getKind() == EdmTypeKind.DEFINITION) {
    checkJsonTypeBasedOnPrimitiveType(propertyName,
        ((EdmTypeDefinition) edmPrimitiveType).getUnderlyingType(), jsonNode);
  } else if (edmPrimitiveType.getKind() == EdmTypeKind.ENUM) {
    // Enum values must be strings.
    valid = jsonNode.isTextual();
  } else {
    final String name = edmPrimitiveType.getName();
    EdmPrimitiveTypeKind primKind;
    try {
      primKind = EdmPrimitiveTypeKind.valueOf(name);
    } catch (final IllegalArgumentException e) {
      throw new DeserializerException("Unknown Primitive Type: " + name, e,
          DeserializerException.MessageKeys.UNKNOWN_PRIMITIVE_TYPE, name, propertyName);
    }
    valid = matchTextualCase(jsonNode, primKind)
        || matchNumberCase(jsonNode, primKind)
        || matchBooleanCase(jsonNode, primKind)
        || matchIEEENumberCase(jsonNode, primKind)
        || jsonNode.isObject() && name.startsWith("Geo");
  }
  if (!valid) {
    throw new DeserializerException(
        "Invalid json type: " + jsonNode.getNodeType() + " for " + edmPrimitiveType + " property: " + propertyName,
        DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, propertyName);
  }
}
 
Example 2
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmType getPrimitiveType(final FullQualifiedName fullQualifiedName) {
  if (EdmPrimitiveType.EDM_NAMESPACE.equals(fullQualifiedName.getNamespace())) {
    final EdmPrimitiveTypeKind primitiveTypeKind = EdmPrimitiveTypeKind.valueOf(fullQualifiedName.getName());
    return primitiveTypeKind == null ? null : odata.createPrimitiveTypeInstance(primitiveTypeKind);
  } else {
    return null;
  }
}
 
Example 3
Source File: ClientPrimitiveValueImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public BuilderImpl setType(final EdmType type) {
  EdmPrimitiveTypeKind primitiveTypeKind = null;
  if (type != null) {
    if (type.getKind() != EdmTypeKind.PRIMITIVE) {
      throw new IllegalArgumentException(String.format("Provided type %s is not primitive", type));
    }
    primitiveTypeKind = EdmPrimitiveTypeKind.valueOf(type.getName());
  }
  return setType(primitiveTypeKind);
}
 
Example 4
Source File: ODataUtils.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * This method creates access uri for the entity.
 *
 * @param baseURL      base URL
 * @param entity       entity
 * @param enitySetName entity Set Name
 * @param type         entity Type
 * @return Entity URI
 * @throws EdmPrimitiveTypeException
 */
public static String buildLocation(String baseURL, Entity entity, String enitySetName, EdmEntityType type)
        throws EdmPrimitiveTypeException {
    StringBuilder location = new StringBuilder();
    location.append(baseURL).append("/").append(enitySetName);
    int i = 0;
    boolean usename = type.getKeyPredicateNames().size() > 1;
    location.append("(");

    String value;
    for (Iterator var7 = type.getKeyPredicateNames().iterator(); var7.hasNext(); location.append(value)) {
        String key = (String) var7.next();
        if (i > 0) {
            location.append(",");
        }

        ++i;
        if (usename) {
            location.append(key).append("=");
        }

        String propertyType = entity.getProperty(key).getType();
        Object propertyValue = entity.getProperty(key).getValue();
        if (propertyType.startsWith("Edm.")) {
            propertyType = propertyType.substring(4);
        }

        EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(propertyType);
        EdmProperty property = type.getStructuralProperty(key);
        value = EdmPrimitiveTypeFactory.getInstance(kind)
                                       .valueToString(propertyValue, property.isNullable(), property.getMaxLength(),
                                                      property.getPrecision(), property.getScale(),
                                                      property.isUnicode());
        if (kind == EdmPrimitiveTypeKind.String) {
            value = EdmString.getInstance().toUriLiteral(Encoder.encode(value));
        }
    }

    location.append(")");
    return location.toString();
}
 
Example 5
Source File: EntityResponse.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static String buildLocation(String baseURL, Entity entity, String enitySetName, EdmEntityType type) 
    throws EdmPrimitiveTypeException {
  StringBuilder location = new StringBuilder();

  location.append(baseURL).append("/").append(enitySetName);
  
  int i = 0;
  boolean usename = type.getKeyPredicateNames().size() > 1;
  location.append("(");
  for (String key : type.getKeyPredicateNames()) {
    if (i > 0) {
      location.append(",");
    }
    i++;
    if (usename) {
      location.append(key).append("=");
    }
    
    EdmProperty property = (EdmProperty)type.getProperty(key);
    String propertyType = entity.getProperty(key).getType();
    Object propertyValue = entity.getProperty(key).getValue();
    
    if (propertyValue == null) {
      throw new EdmPrimitiveTypeException("The key value for property "+key+" is invalid; Key value cannot be null");
    }
    
    if(propertyType.startsWith("Edm.")) {
      propertyType = propertyType.substring(4);
    }
    EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(propertyType);
    String value =  EdmPrimitiveTypeFactory.getInstance(kind).valueToString(
        propertyValue, property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(), 
        property.isUnicode());
    if (kind == EdmPrimitiveTypeKind.String) {
        value = EdmString.getInstance().toUriLiteral(Encoder.encode(value));
    }
    location.append(value);
  }
  location.append(")");
  return location.toString();
}