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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmProperty#isCollection() . 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: RequestValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void validatePropertyValue(final Property property, final EdmProperty edmProperty,
    final EdmBindingTarget edmBindingTarget, final List<String> path) throws DataProviderException {

  final ArrayList<String> newPath = new ArrayList<String>(path);
  newPath.add(edmProperty.getName());

  if (edmProperty.isCollection()) {
    if (edmProperty.getType() instanceof EdmComplexType && property != null) {
      for (final Object value : property.asCollection()) {
        validateComplexValue((ComplexValue) value,
            edmBindingTarget,
            (EdmComplexType) edmProperty.getType(),
            newPath);
      }
    }
  } else if (edmProperty.getType() instanceof EdmComplexType) {
    validateComplexValue((property == null) ? null : property.asComplex(),
        edmBindingTarget,
        (EdmComplexType) edmProperty.getType(),
        newPath);
  }
}
 
Example 2
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Property createProperty(final EdmProperty edmProperty, final String propertyName)
    throws DataProviderException {
  final EdmType type = edmProperty.getType();
  Property newProperty;
  if (edmProperty.isPrimitive()
      || type.getKind() == EdmTypeKind.ENUM || type.getKind() == EdmTypeKind.DEFINITION) {
    newProperty = edmProperty.isCollection() ?
        DataCreator.createPrimitiveCollection(propertyName) :
        DataCreator.createPrimitive(propertyName, null);
  } else {
    if (edmProperty.isCollection()) {
      @SuppressWarnings("unchecked")
      Property newProperty2 = DataCreator.createComplexCollection(propertyName, 
          edmProperty.getType().getFullQualifiedName().getFullQualifiedNameAsString());
      newProperty = newProperty2;
    } else {
      newProperty = DataCreator.createComplex(propertyName,
          edmProperty.getType().getFullQualifiedName().getFullQualifiedNameAsString());
      createProperties((EdmComplexType) type, newProperty.asComplex().getValue());
    }
  }
  return newProperty;
}
 
Example 3
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 4
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void updateProperty(final EdmProperty edmProperty, Property property, final Property newProperty,
    final boolean patch) throws DataProviderException {
  if(property == null){
    throw new DataProviderException("Cannot update type of the entity",
        HttpStatusCode.BAD_REQUEST);
  }
  final EdmType type = edmProperty.getType();
  if (edmProperty.isCollection()) {
    // Updating collection properties means replacing all entries with the given ones.
    property.asCollection().clear();

    if (newProperty != null) {
      if (type.getKind() == EdmTypeKind.COMPLEX) {
        // Create each complex value.
        for (final ComplexValue complexValue : (List<ComplexValue>) newProperty.asCollection()) {
          ((List<ComplexValue>) property.asCollection()).add(createComplexValue(edmProperty, complexValue, patch));
        }
      } else {
        // Primitive type
        ((List<Object>) property.asCollection()).addAll(newProperty.asCollection());
      }
    }
  } else if (type.getKind() == EdmTypeKind.COMPLEX) {
    for (final String propertyName : ((EdmComplexType) type).getPropertyNames()) {
      final List<Property> newProperties = newProperty == null || newProperty.asComplex() == null ? null :
          newProperty.asComplex().getValue();
      updateProperty(((EdmComplexType) type).getStructuralProperty(propertyName),
          findProperty(propertyName, property.asComplex().getValue()),
          newProperties == null ? null : findProperty(propertyName, newProperties),
          patch);
    }
  } else {
    if (newProperty != null || !patch) {
      final Object value = newProperty == null ? null : newProperty.getValue();
      updatePropertyValue(property, value);
    }
  }
}
 
Example 5
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeProperty(final ServiceMetadata metadata,
    final EdmProperty edmProperty, final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws IOException, SerializerException {
  boolean isStreamProperty = isStreamProperty(edmProperty);
  writePropertyType(edmProperty, json);
  if (!isStreamProperty) {
    json.writeFieldName(edmProperty.getName());
  }
  if (property == null || property.isNull()) {
    if (edmProperty.isNullable() == Boolean.FALSE && !isStreamProperty) {
      throw new SerializerException("Non-nullable property not present!",
          SerializerException.MessageKeys.MISSING_PROPERTY, edmProperty.getName());
    } else {
      if (!isStreamProperty) {
        if (edmProperty.isCollection()) {
          json.writeStartArray();
          json.writeEndArray();
        } else {
          json.writeNull();
        }
      }
    }
  } else {
    writePropertyValue(metadata, edmProperty, property, selectedPaths, json, 
        expandedPaths, linked, expand);
  }
}
 
Example 6
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePropertyType(final EdmProperty edmProperty, JsonGenerator json)
    throws SerializerException, IOException {
  if (!isODataMetadataFull) {
    return;
  }
  String typeName = edmProperty.getName() + constants.getType();
  final EdmType type = edmProperty.getType();
  if (type.getKind() == EdmTypeKind.ENUM || type.getKind() == EdmTypeKind.DEFINITION) {
    if (edmProperty.isCollection()) {
      json.writeStringField(typeName, 
          "#Collection(" + type.getFullQualifiedName().getFullQualifiedNameAsString() + ")");
    } else {
      json.writeStringField(typeName, "#" + type.getFullQualifiedName().getFullQualifiedNameAsString());
    }
  } else if (edmProperty.isPrimitive()) {
    if (edmProperty.isCollection()) {
      json.writeStringField(typeName, "#Collection(" + type.getFullQualifiedName().getName() + ")");
    } else {
      // exclude the properties that can be heuristically determined
      if (type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean) &&
          type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Double) &&
          type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.String)) {
        json.writeStringField(typeName, "#" + type.getFullQualifiedName().getName());                  
      }
    }
  } else if (type.getKind() == EdmTypeKind.COMPLEX) {
    // non-collection case written in writeComplex method directly.
    if (edmProperty.isCollection()) {
      json.writeStringField(typeName, 
          "#Collection(" + type.getFullQualifiedName().getFullQualifiedNameAsString() + ")");
    }
  } else {
    throw new SerializerException("Property type not yet supported!",
        SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
  }    
}
 
Example 7
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePropertyValue(final ServiceMetadata metadata, final EdmProperty edmProperty,
    final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  final EdmType type = edmProperty.getType();
  try {
    if (edmProperty.isPrimitive()
        || type.getKind() == EdmTypeKind.ENUM || type.getKind() == EdmTypeKind.DEFINITION) {
      if (edmProperty.isCollection()) {
        writePrimitiveCollection((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      } else {
        writePrimitive((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      }
    } else if (property.isComplex()) {
      if (edmProperty.isCollection()) {
        writeComplexCollection(metadata, (EdmComplexType) type, property, selectedPaths, json);
      } else {
        writeComplex(metadata, (EdmComplexType) type, property, selectedPaths, json);
      }
    } else {
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
    }
  } catch (final EdmPrimitiveTypeException e) {
    throw new SerializerException("Wrong value for property!", e,
        SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
        edmProperty.getName(), property.getValue().toString());
  }
}
 
Example 8
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePropertyValue(final ServiceMetadata metadata, final EdmProperty edmProperty,
    final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  final EdmType type = edmProperty.getType();
  try {
    if (edmProperty.isPrimitive()
        || type.getKind() == EdmTypeKind.ENUM || type.getKind() == EdmTypeKind.DEFINITION) {
      if (edmProperty.isCollection()) {
        writePrimitiveCollection((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      } else {
        writePrimitive((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      }
    } else if (property.isComplex()) {
      if (edmProperty.isCollection()) {
        writeComplexCollection(metadata, (EdmComplexType) type, property, selectedPaths, json);
      } else {
        writeComplex(metadata, (EdmComplexType) type, property, selectedPaths, json);
      }
    } else {
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
    }
  } catch (final EdmPrimitiveTypeException e) {
    throw new SerializerException("Wrong value for property!", e,
        SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
        edmProperty.getName(), property.getValue().toString());
  }
}
 
Example 9
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 10
Source File: MetadataDocumentJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void appendProperties(final JsonGenerator json, 
    final EdmStructuredType type) throws SerializerException, IOException {
  List<String> propertyNames = new ArrayList<>(type.getPropertyNames());
  if (type.getBaseType() != null) {
    propertyNames.removeAll(type.getBaseType().getPropertyNames());
  }
  for (String propertyName : propertyNames) {
    EdmProperty property = type.getStructuralProperty(propertyName);
    json.writeObjectFieldStart(propertyName);
    String fqnString;
    if (property.isPrimitive()) {
      fqnString = getFullQualifiedName(property.getType());
    } else {
      fqnString = getAliasedFullQualifiedName(property.getType());
    }
    json.writeStringField(TYPE, fqnString);
    if (property.isCollection()) {
      json.writeBooleanField(COLLECTION, property.isCollection());
    }

    // Facets
    if (!property.isNullable()) {
      json.writeBooleanField(NULLABLE, property.isNullable());
    }

    if (!property.isUnicode()) {
      json.writeBooleanField(UNICODE, property.isUnicode());
    }

    if (property.getDefaultValue() != null) {
      json.writeStringField(DEFAULT_VALUE, property.getDefaultValue());
    }

    if (property.getMaxLength() != null) {
      json.writeNumberField(MAX_LENGTH, property.getMaxLength());
    }

    if (property.getPrecision() != null) {
      json.writeNumberField(PRECISION, property.getPrecision());
    }

    if (property.getScale() != null) {
      json.writeNumberField(SCALE, property.getScale());
    }
    
    if (property.getSrid() != null) {
        json.writeStringField(SRID, "" + property.getSrid());
    }

    appendAnnotations(json, property, null);
    json.writeEndObject();
  }
}
 
Example 11
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void writePropertyValue(final ServiceMetadata metadata,
    final EdmProperty edmProperty, final Property property,
    final Set<List<String>> selectedPaths,
    final String xml10InvalidCharReplacement, final XMLStreamWriter writer, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws XMLStreamException, SerializerException {
  try {
    if (edmProperty.isPrimitive()
        || edmProperty.getType().getKind() == EdmTypeKind.ENUM
        || edmProperty.getType().getKind() == EdmTypeKind.DEFINITION) {
      if (edmProperty.isCollection()) {
        writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_TYPE,
            edmProperty.isPrimitive() ?
                "#Collection(" + edmProperty.getType().getName() + ")" :
                collectionType(edmProperty.getType()));
        writePrimitiveCollection((EdmPrimitiveType) edmProperty.getType(), property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(),
            xml10InvalidCharReplacement,writer);
      } else {
        writePrimitive((EdmPrimitiveType) edmProperty.getType(), property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(),
            xml10InvalidCharReplacement, writer);
      }
    } else if (property.isComplex()) {
      if (edmProperty.isCollection()) {
        writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_TYPE, collectionType(edmProperty.getType()));
        writeComplexCollection(metadata, (EdmComplexType) edmProperty.getType(), property, selectedPaths, 
            xml10InvalidCharReplacement, writer, expandedPaths, linked, expand);
      } else {
          writeComplex(metadata, edmProperty, property, selectedPaths, 
              xml10InvalidCharReplacement, writer, expandedPaths, linked, expand);
      }
    } else {
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
    }
  } catch (final EdmPrimitiveTypeException e) {
    throw new SerializerException("Wrong value for property!", e,
        SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
        edmProperty.getName(), property.getValue().toString());
  }
}