org.apache.olingo.server.api.uri.queryoption.expression.Member Java Examples

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.expression.Member. 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: ExpressionVisitorImpl.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public VisitorOperand visitMember(Member member) throws ExpressionVisitException, ODataApplicationException {
    final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
    int size = uriResourceParts.size();
    if (uriResourceParts.get(0) instanceof UriResourceProperty) {
        EdmProperty currentEdmProperty = ((UriResourceProperty) uriResourceParts.get(0)).getProperty();
        Property currentProperty = entity.getProperty(currentEdmProperty.getName());
        return new TypedOperand(currentProperty.getValue(), currentEdmProperty.getType(), currentEdmProperty);
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAll) {
        return throwNotImplemented();
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAny) {
        return throwNotImplemented();
    } else {
        return throwNotImplemented();
    }
}
 
Example #2
Source File: FilterTreeToText.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public String visitMember(final Member member) throws ExpressionVisitException, ODataApplicationException {
  String ret = "";

  for (UriResource item : member.getResourcePath().getUriResourceParts()) {
    String tmp = "";
    if (item instanceof UriResourceLambdaAll) {
      UriResourceLambdaAll all = (UriResourceLambdaAll) item;
      tmp = visitLambdaExpression("ALL", all.getLambdaVariable(), all.getExpression());
    } else if (item instanceof UriResourceLambdaAny) {
      UriResourceLambdaAny any = (UriResourceLambdaAny) item;
      tmp = visitLambdaExpression("ANY", any.getLambdaVariable(), any.getExpression());
    } else if (item instanceof UriResourcePartTyped) {
      UriResourcePartTyped typed = (UriResourcePartTyped) item;
      tmp = typed.toString(true);
    }

    if (ret.length() > 0) {
      ret += "/";
    }
    ret += tmp;

  }
  return "<" + ret + ">";
}
 
Example #3
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public FilterValidator isType(final FullQualifiedName fullName) {
  EdmType actualType = null;

  if (curExpression instanceof Member) {
    actualType = ((Member) curExpression).getType();
  } else if (curExpression instanceof TypeLiteral) {
    actualType = ((TypeLiteral) curExpression).getType();
  } else if (curExpression instanceof Literal) {
    actualType = ((Literal) curExpression).getType();
  } else if (curExpression instanceof Enumeration) {
    actualType = ((Enumeration) curExpression).getType();
  } else if (curExpression instanceof Unary) {
    actualType = ((UnaryImpl) curExpression).getType();
  } else if (curExpression instanceof Binary) {
    actualType = ((BinaryImpl) curExpression).getType();
  } else if (curExpression instanceof Method) {
    actualType = ((MethodImpl) curExpression).getType();
  }

  assertNotNull("Current expression not typed", actualType);
  assertEquals(fullName, actualType.getFullQualifiedName());
  return this;
}
 
Example #4
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public Object visitMember(final Member member) throws ExpressionVisitException, ODataApplicationException {
  // To keeps things simple, this tutorial allows only primitive properties.
  // We have faith that the java type of Edm.Int32 is Integer
  
  final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
  
  // Make sure that the resource path of the property contains only a single segment and a primitive property
  // has been addressed. We can be sure, that the property exists because the UriParser checks if the
  // property has been defined in service metadata document.
  
  if(uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourcePrimitiveProperty) {
    UriResourcePrimitiveProperty uriResourceProperty = (UriResourcePrimitiveProperty) uriResourceParts.get(0);
    return currentEntity.getProperty(uriResourceProperty.getProperty().getName()).getValue();
  } else {
    // The OData specification allows in addition complex properties and navigation properties 
    // with a target cardinality 0..1 or 1.
    // This means any combination can occur e.g. Supplier/Address/City
    //  -> Navigation properties  Supplier 
    //  -> Complex Property       Address
    //  -> Primitive Property     City
    // For such cases the resource path returns a list of UriResourceParts
    throw new ODataApplicationException("Only primitive properties are implemented in filter expressions", 
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }
}
 
Example #5
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public Object visitMember(final Member member) throws ExpressionVisitException, ODataApplicationException {
  // To keeps things simple, this tutorial allows only primitive properties.
  // We have faith that the java type of Edm.Int32 is Integer
  
  final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
  
  // Make sure that the resource path of the property contains only a single segment and a primitive property
  // has been addressed. We can be sure, that the property exists because the UriParser checks if the
  // property has been defined in service metadata document.
  
  if(uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourcePrimitiveProperty) {
    UriResourcePrimitiveProperty uriResourceProperty = (UriResourcePrimitiveProperty) uriResourceParts.get(0);
    return currentEntity.getProperty(uriResourceProperty.getProperty().getName()).getValue();
  } else {
    // The OData specification allows in addition complex properties and navigation properties 
    // with a target cardinality 0..1 or 1.
    // This means any combination can occur e.g. Supplier/Address/City
    //  -> Navigation properties  Supplier 
    //  -> Complex Property       Address
    //  -> Primitive Property     City
    // For such cases the resource path returns a list of UriResourceParts
    throw new ODataApplicationException("Only primitive properties are implemented in filter expressions", 
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }
}
 
Example #6
Source File: FilterExpressionVisitor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitMember(final Member member) throws ExpressionVisitException, ODataApplicationException {
    // To keeps things simple, this tutorial allows only primitive
    // properties.
    // We have faith that the java type of Edm.Int32 is Integer

    final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();

    // Make sure that the resource path of the property contains only a
    // single segment and a primitive property
    // has been addressed. We can be sure, that the property exists because
    // the UriParser checks if the
    // property has been defined in service metadata document.

    if (uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourcePrimitiveProperty) {
        UriResourcePrimitiveProperty uriResourceProperty = (UriResourcePrimitiveProperty) uriResourceParts.get(0);
        return currentEntity.getProperty(uriResourceProperty.getProperty().getName()).getValue();
    }

    // The OData specification allows in addition complex properties and
    // navigation properties
    // with a target cardinality 0..1 or 1.
    // This means any combination can occur e.g. Supplier/Address/City
    // -> Navigation properties Supplier
    // -> Complex Property Address
    // -> Primitive Property City
    // For such cases the resource path returns a list of UriResourceParts
    throw new ODataApplicationException("Only primitive properties are implemented in filter expressions",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
 
Example #7
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public ResourceValidator goPath() {
  isMember();
  Member member = (Member) curExpression;

  return new ResourceValidator()
      .setEdm(edm)
      .setUriInfoPath(member.getResourcePath())
      .setUpValidator(this);
}
 
Example #8
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public FilterValidator isMemberStartType(final FullQualifiedName fullName) {
  isMember();
  Member member = (Member) curExpression;
  EdmType actualType = member.getStartTypeFilter();
  assertEquals(fullName, actualType.getFullQualifiedName());
  return this;
}
 
Example #9
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static EdmType getType(final Expression expression) throws UriParserException {
  EdmType type;
  if (expression instanceof Literal) {
    type = ((Literal) expression).getType();
  } else if (expression instanceof TypeLiteral) {
    type = ((TypeLiteral) expression).getType();
  } else if (expression instanceof Enumeration) {
    type = ((Enumeration) expression).getType();
  } else if (expression instanceof Member) {
    type = ((Member) expression).getType();
  } else if (expression instanceof Unary) {
    type = ((UnaryImpl) expression).getType();
  } else if (expression instanceof Binary) {
    type = ((BinaryImpl) expression).getType();
  } else if (expression instanceof Method) {
    type = ((MethodImpl) expression).getType();
  } else if (expression instanceof Alias) {
    final AliasQueryOption alias = ((AliasImpl) expression).getAlias();
    type = alias == null || alias.getValue() == null ? null : getType(alias.getValue());
  } else if (expression instanceof LambdaRef) {
    throw new UriParserSemanticException("Type determination not implemented.",
        UriParserSemanticException.MessageKeys.NOT_IMPLEMENTED, expression.toString());
  } else {
    throw new UriParserSemanticException("Unknown expression type.",
        UriParserSemanticException.MessageKeys.NOT_IMPLEMENTED, expression.toString());
  }
  if (type != null && type.getKind() == EdmTypeKind.DEFINITION) {
    type = ((EdmTypeDefinition) type).getUnderlyingType();
  }
  return type;
}
 
Example #10
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 #11
Source File: ODataFesParser.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
default T visitMember(Member member)
        throws ExpressionVisitException, ODataApplicationException {
    return visitMember(member.getResourcePath());
}
 
Example #12
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public FilterValidator isMember() {
  assertTrue("Current expression not a member", curExpression instanceof Member);
  return this;
}
 
Example #13
Source File: ExpressionJsonVisitor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public JsonNode visitMember(final Member member)
    throws ExpressionVisitException, ODataApplicationException {
  final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
  final UriResource lastSegment = uriResourceParts.get(uriResourceParts.size() - 1);
  ObjectNode result = nodeFactory.objectNode()
      .put(NODE_TYPE_NAME, MEMBER_NAME)
      .put(TYPE_NAME, getType(lastSegment));
  putType(result, TYPE_FILTER_NAME, member.getStartTypeFilter());
  ArrayNode segments = result.putArray(RESOURCE_SEGMENTS_NAME);
  for (final UriResource segment : uriResourceParts) {
    if (segment instanceof UriResourceLambdaAll) {
      final UriResourceLambdaAll all = (UriResourceLambdaAll) segment;
      segments.add(visitLambdaExpression(ALL_NAME, all.getLambdaVariable(), all.getExpression()));
    } else if (segment instanceof UriResourceLambdaAny) {
      final UriResourceLambdaAny any = (UriResourceLambdaAny) segment;
      segments.add(visitLambdaExpression(ANY_NAME, any.getLambdaVariable(), any.getExpression()));
    } else if (segment instanceof UriResourcePartTyped) {
      ObjectNode node = nodeFactory.objectNode()
          .put(NODE_TYPE_NAME, segment.getKind().toString())
          .put(NAME_NAME, segment.toString())
          .put(TYPE_NAME, getType(segment));
      if (segment instanceof UriResourceEntitySet) {
        putParameters(node, KEYS_NAME, ((UriResourceEntitySet) segment).getKeyPredicates());
        putType(node, TYPE_FILTER_ON_COLLECTION_NAME, ((UriResourceEntitySet) segment).getTypeFilterOnCollection());
        putType(node, TYPE_FILTER_ON_ENTRY_NAME, ((UriResourceEntitySet) segment).getTypeFilterOnEntry());
      } else if (segment instanceof UriResourceNavigation) {
        putParameters(node, KEYS_NAME, ((UriResourceNavigation) segment).getKeyPredicates());
        putType(node, TYPE_FILTER_ON_COLLECTION_NAME, ((UriResourceNavigation) segment).getTypeFilterOnCollection());
        putType(node, TYPE_FILTER_ON_ENTRY_NAME, ((UriResourceNavigation) segment).getTypeFilterOnEntry());
      } else if (segment instanceof UriResourceFunction) {
        putParameters(node, PARAMETERS_NAME, ((UriResourceFunction) segment).getParameters());
        putParameters(node, KEYS_NAME, ((UriResourceFunction) segment).getKeyPredicates());
        putType(node, TYPE_FILTER_ON_COLLECTION_NAME, ((UriResourceFunction) segment).getTypeFilterOnCollection());
        putType(node, TYPE_FILTER_ON_ENTRY_NAME, ((UriResourceFunction) segment).getTypeFilterOnEntry());
      } else if (segment instanceof UriResourceIt) {
        putType(node, TYPE_FILTER_ON_COLLECTION_NAME, ((UriResourceIt) segment).getTypeFilterOnCollection());
        putType(node, TYPE_FILTER_ON_ENTRY_NAME, ((UriResourceIt) segment).getTypeFilterOnEntry());
      } else if (segment instanceof UriResourceSingleton) {
        putType(node, TYPE_FILTER_NAME, ((UriResourceSingleton) segment).getEntityTypeFilter());
      } else if (segment instanceof UriResourceComplexProperty) {
        putType(node, TYPE_FILTER_NAME, ((UriResourceComplexProperty) segment).getComplexTypeFilter());
      }
      segments.add(node);
    } else {
      segments.add(nodeFactory.objectNode()
          .put(NODE_TYPE_NAME, segment.getKind().toString())
          .put(NAME_NAME, segment.toString())
          .putNull(TYPE_NAME));
    }
  }
  return result;
}
 
Example #14
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void checkNoCollection(final Expression expression) throws UriParserException {
  if (expression instanceof Member  && ((Member) expression).isCollection()) {
    throw new UriParserSemanticException("Collection not allowed.",
        UriParserSemanticException.MessageKeys.COLLECTION_NOT_ALLOWED);
  }
}
 
Example #15
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 #16
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;
  }