protected ClientValue getODataValue()

in lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataBinderImpl.java [827:952]


  protected ClientValue getODataValue(FullQualifiedName type,
      final Valuable valuable, final URI contextURL, final String metadataETag) {

    // fixes enum values treated as primitive when no type information is available
    if (client instanceof EdmEnabledODataClient && type != null) {
      final EdmEnumType edmType = ((EdmEnabledODataClient) client).getEdm(metadataETag).getEnumType(type);
      if (!valuable.isCollection() && valuable.isPrimitive() && edmType != null) {
        valuable.setValue(ValueType.ENUM, valuable.asPrimitive());
      }
    }

    ClientValue value = null;

    if (valuable.isCollection()) {
      value = client.getObjectFactory().newCollectionValue(type == null ? null : "Collection(" + type.toString() + ")");

      for (Object _value : valuable.asCollection()) {
        final Property fake = new Property();
        fake.setValue(valuable.getValueType().getBaseType(), _value);
        String typeName = null;
        if (_value instanceof ComplexValue) {
          typeName = ((ComplexValue) _value).getTypeName();
          type = typeName == null? type : new FullQualifiedName(typeName);
        }
        value.asCollection().add(getODataValue(type, fake, contextURL, metadataETag));
      }
    } else if (valuable.isEnum()) {
      value = client.getObjectFactory().newEnumValue(type == null ? null : type.toString(),
          valuable.isNull() ? null : valuable.asEnum().toString());
    } else if (valuable.isComplex()) {
      final ClientComplexValue lcValue =
          client.getObjectFactory().newComplexValue(type == null ? null : type.toString());

      EdmComplexType edmType = null;
      if (client instanceof EdmEnabledODataClient && type != null) {
        edmType = ((EdmEnabledODataClient) client).getEdm(metadataETag).getComplexType(type);
      }

      for (Property property : valuable.asComplex().getValue()) {
        EdmType edmPropertyType = null;
        if (edmType != null) {
          final EdmElement edmProp = edmType.getProperty(property.getName());
          if (edmProp != null) {
            edmPropertyType = edmProp.getType();
          }
        }
        lcValue.add(getODataProperty(edmPropertyType, property));
      }
      
      odataNavigationLinks(edmType, valuable.asComplex(), lcValue, metadataETag, contextURL);
      odataAnnotations(valuable.asComplex(), lcValue);
      
      value = lcValue;
    } else {
      if (valuable.isGeospatial()) {
        value = client.getObjectFactory().newPrimitiveValueBuilder().
            setValue(valuable.asGeospatial()).
            setType(type == null
                || EdmPrimitiveTypeKind.Geography.getFullQualifiedName().equals(type)
                || EdmPrimitiveTypeKind.Geometry.getFullQualifiedName().equals(type)
                ? valuable.asGeospatial().getEdmPrimitiveTypeKind()
                : EdmPrimitiveTypeKind.valueOfFQN(type.toString())).
            build();
      } else if (valuable.isPrimitive() || valuable.getValueType() == null) {
     // fixes non-string values treated as string when no type information is available at de-serialization level
        Edm edm = null;
        if (client instanceof EdmEnabledODataClient && type != null) {
          edm = ((EdmEnabledODataClient) client).getEdm(metadataETag);
        }
        if (edm != null && edm.getComplexType(type) != null) {
          ClientComplexValue cValue = client.getObjectFactory().newComplexValue(type.toString());
          value = cValue;
          } else {
            if (type != null && !EdmPrimitiveTypeKind.String.getFullQualifiedName().equals(type)
              && EdmPrimitiveType.EDM_NAMESPACE.equals(type.getNamespace())
              && valuable.asPrimitive() instanceof String) {
  
            final EdmPrimitiveType primitiveType =
                EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.valueOf(type.getName()));
            final Class<?> returnType = primitiveType.getDefaultType().isAssignableFrom(Calendar.class)
                ? Timestamp.class : primitiveType.getDefaultType();
            try {
              valuable.setValue(valuable.getValueType(),
                  primitiveType.valueOfString(valuable.asPrimitive().toString(),
                      null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null,
                      returnType));
            } catch (EdmPrimitiveTypeException e) {
              throw new IllegalArgumentException(e);
            }
          }
          value = client.getObjectFactory().newPrimitiveValueBuilder().
              setValue(valuable.asPrimitive()).
              setType(type == null || !EdmPrimitiveType.EDM_NAMESPACE.equals(type.getNamespace())
                  ? null
                  : EdmPrimitiveTypeKind.valueOfFQN(type.toString())).
              build();
          }
      } else if (valuable.isComplex()) {
        final ClientComplexValue cValue =
            client.getObjectFactory().newComplexValue(type == null ? null : type.toString());

        if (!valuable.isNull()) {
          EdmComplexType edmType = null;
          if (client instanceof EdmEnabledODataClient && type != null) {
            edmType = ((EdmEnabledODataClient) client).getEdm(metadataETag).getComplexType(type);
          }

          for (Property property : valuable.asComplex().getValue()) {
            EdmType edmPropertyType = null;
            if (edmType != null) {
              final EdmElement edmProp = edmType.getProperty(property.getName());
              if (edmProp != null) {
                edmPropertyType = edmProp.getType();
              }
            }

            cValue.add(getODataProperty(edmPropertyType, property));
          }
        }

        value = cValue;
      }
    }

    return value;
  }