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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmProperty#getName() . 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 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 2
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 3
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 4
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 5
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 6
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 7
Source File: UriHelperImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public String buildKeyPredicate(final EdmEntityType edmEntityType, final Entity entity) throws SerializerException {
  StringBuilder result = new StringBuilder();
  final List<String> keyNames = edmEntityType.getKeyPredicateNames();
  boolean first = true;
  for (final String keyName : keyNames) {
    EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(keyName);
    if (first) {
      first = false;
    } else {
      result.append(',');
    }
    if (keyNames.size() > 1) {
      result.append(Encoder.encode(keyName)).append('=');
    }
    final EdmProperty edmProperty =  refType.getProperty();
    if (edmProperty == null) {
      throw new SerializerException("Property not found (possibly an alias): " + keyName,
          SerializerException.MessageKeys.MISSING_PROPERTY, keyName);
    }
    final EdmPrimitiveType type = (EdmPrimitiveType) edmProperty.getType();
    final Object propertyValue = findPropertyRefValue(entity, refType);
    try {
      final String value = type.toUriLiteral(
          type.valueToString(propertyValue,
              edmProperty.isNullable(), edmProperty.getMaxLength(),
              edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()));
      result.append(Encoder.encode(value));
    } catch (final EdmPrimitiveTypeException e) {
      throw new SerializerException("Wrong key value!", e,
          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE, edmProperty.getName(), 
          propertyValue != null ? propertyValue.toString(): null);
    }
  }
  return result.toString();
}
 
Example 8
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 9
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private List<Entity> applyOrderQueryOption(List<Entity> entityList, OrderByOption orderByOption) {

    if (orderByOption != null) {
      List<OrderByItem> orderItemList = orderByOption.getOrders();
      final OrderByItem orderByItem = orderItemList.get(0); // in our example we support only one
      Expression expression = orderByItem.getExpression();
      if (expression instanceof Member) {
        UriInfoResource resourcePath = ((Member) expression).getResourcePath();
        UriResource uriResource = resourcePath.getUriResourceParts().get(0);
        if (uriResource instanceof UriResourcePrimitiveProperty) {
          EdmProperty edmProperty = ((UriResourcePrimitiveProperty) uriResource).getProperty();
          final String sortPropertyName = edmProperty.getName();

          // do the sorting for the list of entities  
          Collections.sort(entityList, new Comparator<Entity>() {

            // we delegate the sorting to the native sorter of Integer and String
            public int compare(Entity entity1, Entity entity2) {
              int compareResult = 0;

              if (sortPropertyName.equals("ID")) {
                Integer integer1 = (Integer) entity1.getProperty(sortPropertyName).getValue();
                Integer integer2 = (Integer) entity2.getProperty(sortPropertyName).getValue();

                compareResult = integer1.compareTo(integer2);
              } else {
                String propertyValue1 = (String) entity1.getProperty(sortPropertyName).getValue();
                String propertyValue2 = (String) entity2.getProperty(sortPropertyName).getValue();

                compareResult = propertyValue1.compareTo(propertyValue2);
              }

              // if 'desc' is specified in the URI, change the order of the list 
              if (orderByItem.isDescending()) {
                return -compareResult; // just convert the result to negative value to change the order
              }

              return compareResult;
            }
          });
        }
      }
    }
    
    return entityList;
  }
 
Example 10
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 11
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
    UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

  // 1. Retrieve info from URI
  // 1.1. retrieve the info about the requested entity set
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  // Note: only in our example we can rely that the first segment is the EntitySet
  UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
  EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
  // the key for the entity
  List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

  // 1.2. retrieve the requested (Edm) property
  // the last segment is the Property
  UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1);
  EdmProperty edmProperty = uriProperty.getProperty();
  String edmPropertyName = edmProperty.getName();
  // in our example, we know we have only primitive types in our model
  EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();

  // 2. retrieve data from backend
  // 2.1. retrieve the entity data, for which the property has to be read
  Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
  if (entity == null) { // Bad request
    throw new ODataApplicationException("Entity not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 2.2. retrieve the property data from the entity
  Property property = entity.getProperty(edmPropertyName);
  if (property == null) {
    throw new ODataApplicationException("Property not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 3. serialize
  Object value = property.getValue();
  if (value != null) {
    // 3.1. configure the serializer
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
    PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
    // 3.2. serialize
    SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
    InputStream propertyStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(propertyStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  } else {
    // in case there's no value for the property, we can skip the serialization
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
}
 
Example 12
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 13
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1);
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 14
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());
  }
}
 
Example 15
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 16
Source File: ProductsEntityCollectionProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private static void applyOrderby(UriInfo uriInfo, EntityCollection entityCollection) {
    List<Entity> entityList = entityCollection.getEntities();

    OrderByOption orderByOption = uriInfo.getOrderByOption();
    if (orderByOption == null) {
        return;
    }

    List<OrderByItem> orderItemList = orderByOption.getOrders();
    OrderByItem orderByItem = orderItemList.get(0); // in our example we support only one
    Expression expression = orderByItem.getExpression();
    if (! (expression instanceof Member)) {
        return;
    }

    UriInfoResource resourcePath = ((Member)expression).getResourcePath();
    UriResource uriResource = resourcePath.getUriResourceParts().get(0);
    if (! (uriResource instanceof UriResourcePrimitiveProperty)) {
        return;
    }

    EdmProperty edmProperty = ((UriResourcePrimitiveProperty)uriResource).getProperty();
    String sortPropertyName = edmProperty.getName();

    // do the sorting for the list of entities
    Collections.sort(entityList, new Comparator<Entity>() {

        // we delegate the sorting to the native sorter of Integer and String
        @Override
        public int compare(Entity entity1, Entity entity2) {
            int compareResult = 0;

            if (sortPropertyName.equals("ID")) {
                Integer integer1 = (Integer)entity1.getProperty(sortPropertyName).getValue();
                Integer integer2 = (Integer)entity2.getProperty(sortPropertyName).getValue();
                compareResult = integer1.compareTo(integer2);
            } else {
                String propertyValue1 = (String)entity1.getProperty(sortPropertyName).getValue();
                String propertyValue2 = (String)entity2.getProperty(sortPropertyName).getValue();
                compareResult = propertyValue1.compareTo(propertyValue2);
            }

            // if 'desc' is specified in the URI, change the order of the list
            if (orderByItem.isDescending()) {
                return -compareResult; // just convert the result to negative value to change the order
            }

            return compareResult;
        }
    });
}
 
Example 17
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
    UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

  // 1. Retrieve info from URI
  // 1.1. retrieve the info about the requested entity set
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  // Note: only in our example we can rely that the first segment is the EntitySet
  UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
  EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
  // the key for the entity
  List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

  // 1.2. retrieve the requested (Edm) property
  // the last segment is the Property
  UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1);
  EdmProperty edmProperty = uriProperty.getProperty();
  String edmPropertyName = edmProperty.getName();
  // in our example, we know we have only primitive types in our model
  EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();

  // 2. retrieve data from backend
  // 2.1. retrieve the entity data, for which the property has to be read
  Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
  if (entity == null) { // Bad request
    throw new ODataApplicationException("Entity not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 2.2. retrieve the property data from the entity
  Property property = entity.getProperty(edmPropertyName);
  if (property == null) {
    throw new ODataApplicationException("Property not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 3. serialize
  Object value = property.getValue();
  if (value != null) {
    // 3.1. configure the serializer
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
    PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
    // 3.2. serialize
    SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
    InputStream propertyStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(propertyStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  } else {
    // in case there's no value for the property, we can skip the serialization
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
}
 
Example 18
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 19
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, 
    ContentType responseFormat) throws ODataApplicationException, SerializerException {

	// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// in our example, the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
	EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
	List<Entity> entityList = entityCollection.getEntities();
	
	// 3rd apply $orderby
	OrderByOption orderByOption = uriInfo.getOrderByOption();
	if (orderByOption != null) {
		List<OrderByItem> orderItemList = orderByOption.getOrders();
		final OrderByItem orderByItem = orderItemList.get(0); // in our example we support only one
		Expression expression = orderByItem.getExpression();
		if(expression instanceof Member){
			UriInfoResource resourcePath = ((Member)expression).getResourcePath();
			UriResource uriResource = resourcePath.getUriResourceParts().get(0);
			if (uriResource instanceof UriResourcePrimitiveProperty) {
				EdmProperty edmProperty = ((UriResourcePrimitiveProperty)uriResource).getProperty();
				final String sortPropertyName = edmProperty.getName();

				// do the sorting for the list of entities  
				Collections.sort(entityList, new Comparator<Entity>() {

					// we delegate the sorting to the native sorter of Integer and String
					public int compare(Entity entity1, Entity entity2) {
						int compareResult = 0;

						if(sortPropertyName.equals("ID")){
							Integer integer1 = (Integer) entity1.getProperty(sortPropertyName).getValue();
							Integer integer2 = (Integer) entity2.getProperty(sortPropertyName).getValue();
							
							compareResult = integer1.compareTo(integer2);
						}else{
							String propertyValue1 = (String) entity1.getProperty(sortPropertyName).getValue();
							String propertyValue2 = (String) entity2.getProperty(sortPropertyName).getValue();
							
							compareResult = propertyValue1.compareTo(propertyValue2);
						}

						// if 'desc' is specified in the URI, change the order of the list 
						if(orderByItem.isDescending()){
							return - compareResult; // just convert the result to negative value to change the order
						}
						
						return compareResult;
					}
				});
			}
		}
	}
	
	
	// 4th: create a serializer based on the requested format (json)
	ODataSerializer serializer = odata.createSerializer(responseFormat);

	// and serialize the content: transform from the EntitySet object to InputStream
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();
	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

	final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
	EntityCollectionSerializerOptions opts =
			EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
   SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, 
                                                                   entityCollection, opts);
	InputStream serializedContent = serializerResult.getContent();

	// 5th: configure the response object: set the body, headers and status code
	response.setContent(serializedContent);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 20
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,UriInfo uriInfo, ContentType responseFormat) 
    throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); 
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), 
		                                    Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}