Java Code Examples for org.apache.olingo.commons.api.edm.EdmType#getKind()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmType#getKind() . 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 matched entity list, where it uses in getEntity method to get the matched entity.
 *
 * @param entityType EdmEntityType
 * @param param      UriParameter
 * @param entityList List of entities
 * @return list of entities
 * @throws ODataApplicationException
 * @throws ODataServiceFault
 */
private List<Entity> getMatch(EdmEntityType entityType, UriParameter param, List<Entity> entityList)
        throws ODataApplicationException, ODataServiceFault {
    ArrayList<Entity> list = new ArrayList<>();
    for (Entity entity : entityList) {
        EdmProperty property = (EdmProperty) entityType.getProperty(param.getName());
        EdmType type = property.getType();
        if (type.getKind() == EdmTypeKind.PRIMITIVE) {
            Object match = readPrimitiveValue(property, param.getText());
            Property entityValue = entity.getProperty(param.getName());
            if (match != null) {
                if (match.equals(entityValue.asPrimitive())) {
                    list.add(entity);
                }
            } else {
                if (null == entityValue.asPrimitive()) {
                    list.add(entity);
                }
            }
        } else {
            throw new ODataServiceFault("Complex elements are not supported, couldn't compare complex objects.");
        }
    }
    return list;
}
 
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: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void checkEqualityTypes(final Expression left, final Expression right) throws UriParserException {
  checkNoCollection(left);
  checkNoCollection(right);

  final EdmType leftType = getType(left);
  final EdmType rightType = getType(right);
  if (leftType == null || rightType == null || leftType.equals(rightType)) {
    return;
  }

  // Numeric promotion for Edm.Byte and Edm.SByte
  if (isType(leftType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)
      && isType(rightType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)) {
    return;
  }

  if (leftType.getKind() != EdmTypeKind.PRIMITIVE
      || rightType.getKind() != EdmTypeKind.PRIMITIVE
      || !(((EdmPrimitiveType) leftType).isCompatible((EdmPrimitiveType) rightType)
      || ((EdmPrimitiveType) rightType).isCompatible((EdmPrimitiveType) leftType))) {
    throw new UriParserSemanticException("Incompatible types.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        leftType.getFullQualifiedName().getFullQualifiedNameAsString(),
        rightType.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}
 
Example 4
Source File: ApplyParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Compute parseComputeTrafo(EdmStructuredType referencedType)
    throws UriParserException, UriValidationException {
  ComputeImpl compute = new ComputeImpl();
  do {
    final Expression expression = new ExpressionParser(edm, odata)
        .parse(tokenizer, referencedType, crossjoinEntitySetNames, aliases);
    final EdmType expressionType = ExpressionParser.getType(expression);
    if (expressionType.getKind() != EdmTypeKind.PRIMITIVE) {
      throw new UriParserSemanticException("Compute expressions must return primitive values.",
          UriParserSemanticException.MessageKeys.ONLY_FOR_PRIMITIVE_TYPES, "compute");
    }
    final String alias = parseAsAlias(referencedType, true);
    ((DynamicStructuredType) referencedType).addProperty(createDynamicProperty(alias, expressionType));
    compute.addExpression(new ComputeExpressionImpl()
        .setExpression(expression)
        .setAlias(alias));
  } while (tokenizer.next(TokenKind.COMMA));
  ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
  return compute;
}
 
Example 5
Source File: TripPinDataModel.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private List<Entity> getMatch(UriParameter param, List<Entity> es)
    throws ODataApplicationException {
  ArrayList<Entity> list = new ArrayList<Entity>();
  for (Entity entity : es) {

    EdmEntityType entityType = this.metadata.getEdm().getEntityType(
        new FullQualifiedName(entity.getType()));

    EdmProperty property = (EdmProperty) entityType.getProperty(param.getName());
    EdmType type = property.getType();
    if (type.getKind() == EdmTypeKind.PRIMITIVE) {
      Object match = readPrimitiveValue(property, param.getText());
      Property entityValue = entity.getProperty(param.getName());
      if (match.equals(entityValue.asPrimitive())) {
        list.add(entity);
      }
    } else {
      throw new RuntimeException("Can not compare complex objects");
    }
  }
  return list;
}
 
Example 6
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 7
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void consumePropertySingleNode(final String name, final EdmType type,
    final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final boolean isUnicode, final EdmMapping mapping, final JsonNode jsonNode, final Property property)
    throws DeserializerException {
  switch (type.getKind()) {
  case PRIMITIVE:
  case DEFINITION:
  case ENUM:
    Object value = readPrimitiveValue(name, (EdmPrimitiveType) type,
        isNullable, maxLength, precision, scale, isUnicode, mapping, jsonNode);
    property.setValue(type.getKind() == EdmTypeKind.ENUM ? ValueType.ENUM : ValueType.PRIMITIVE,
        value);
    break;
  case COMPLEX:
    EdmType derivedType = getDerivedType((EdmComplexType) type,
        jsonNode);
    property.setType(derivedType.getFullQualifiedName()
        .getFullQualifiedNameAsString());

    value = readComplexNode(name, derivedType, isNullable, jsonNode);
    property.setValue(ValueType.COMPLEX, value);
    break;
  default:
    throw new DeserializerException("Invalid Type Kind for a property found: " + type.getKind(),
        DeserializerException.MessageKeys.INVALID_JSON_TYPE_FOR_PROPERTY, name);
  }
}
 
Example 8
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 9
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private boolean isEnumType(final Expression expression) throws UriParserException {
  final EdmType expressionType = getType(expression);
  return expressionType == null
      || expressionType.getKind() == EdmTypeKind.ENUM
      || isType(expressionType,
          EdmPrimitiveTypeKind.Int16, EdmPrimitiveTypeKind.Int32, EdmPrimitiveTypeKind.Int64,
          EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte);
}
 
Example 10
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static AliasQueryOption parseAliasValue(final String name, final EdmType type, final boolean isNullable,
    final boolean isCollection, final Edm edm, final EdmType referringType,
    final Map<String, AliasQueryOption> aliases) throws UriParserException, UriValidationException {
  final EdmTypeKind kind = type == null ? null : type.getKind();
  final AliasQueryOption alias = aliases.get(name);
  if (alias != null && alias.getText() != null) {
    UriTokenizer aliasTokenizer = new UriTokenizer(alias.getText());
    if (kind == null
        || !((isCollection || kind == EdmTypeKind.COMPLEX || kind == EdmTypeKind.ENTITY ?
        aliasTokenizer.next(TokenKind.jsonArrayOrObject) :
        nextPrimitiveTypeValue(aliasTokenizer, (EdmPrimitiveType) type, isNullable))
        && aliasTokenizer.next(TokenKind.EOF))) {
      // The alias value is not an allowed literal value, so parse it again as expression.
      aliasTokenizer = new UriTokenizer(alias.getText());
      // Don't pass on the current alias to avoid circular references.
      Map<String, AliasQueryOption> aliasesInner = new HashMap<>(aliases);
      aliasesInner.remove(name);
      final Expression expression = new ExpressionParser(edm, odata)
          .parse(aliasTokenizer, referringType, null, aliasesInner);
      final EdmType expressionType = ExpressionParser.getType(expression);
      if (aliasTokenizer.next(TokenKind.EOF)
          && (expressionType == null || type == null || expressionType.equals(type))) {
        ((AliasQueryOptionImpl) alias).setAliasValue(expression);
      } else {
        throw new UriParserSemanticException("Illegal value for alias '" + alias.getName() + "'.",
            UriParserSemanticException.MessageKeys.UNKNOWN_PART, alias.getText());
      }
    }
  }
  return alias;
}
 
Example 11
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static void validateFunctionParameterFacets(final EdmFunction function, 
    final List<UriParameter> parameters, Edm edm, Map<String, AliasQueryOption> aliases) 
        throws UriParserException, UriValidationException {
  for (UriParameter parameter : parameters) {
    EdmParameter edmParameter = function.getParameter(parameter.getName());
    final EdmType type = edmParameter.getType();
    final EdmTypeKind kind = type.getKind();
    if ((kind == EdmTypeKind.PRIMITIVE || kind == EdmTypeKind.DEFINITION || kind == EdmTypeKind.ENUM)
        && !edmParameter.isCollection()) {
      final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type;
      String text = null;
      try {
        text = parameter.getAlias() == null ?
            parameter.getText() :
              aliases.containsKey(parameter.getAlias()) ?
                  parseAliasValue(parameter.getAlias(),
                      edmParameter.getType(), edmParameter.isNullable(), edmParameter.isCollection(),
                      edm, type, aliases).getText() : null;
                      if (edmParameter.getMapping() == null) {
                        primitiveType.valueOfString(primitiveType.fromUriLiteral(text),
                            edmParameter.isNullable(), edmParameter.getMaxLength(), edmParameter.getPrecision(), 
                            edmParameter.getScale(), true, primitiveType.getDefaultType());
                      } else {
                        primitiveType.valueOfString(primitiveType.fromUriLiteral(text),
                            edmParameter.isNullable(), edmParameter.getMaxLength(), edmParameter.getPrecision(), 
                            edmParameter.getScale(), true, edmParameter.getMapping().getMappedJavaClass());
                      }
      } catch (final EdmPrimitiveTypeException e) {
        throw new UriValidationException(
            "Invalid value '" + text + "' for parameter " + parameter.getName(), e,
            UriValidationException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, parameter.getName());
      }
    }
  }
}
 
Example 12
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 13
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 14
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 15
Source File: PropertyResponse.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void writeProperty(EdmType edmType, Property property) throws SerializerException {
  assert (!isClosed());

  if (property == null) {
    writeNotFound(true);
    return;
  }

  if (property.getValue() == null) {
    writeNoContent(true);
    return;
  }
  
  if (ContentTypeHelper.isODataMetadataFull(this.responseContentType)) {
    ContextURL contextURL = (this.complexOptions != null)
        ? this.complexOptions.getContextURL()
        : this.primitiveOptions.getContextURL();
    EdmAction action = this.metadata.getEdm().getBoundActionWithBindingType(
        edmType.getFullQualifiedName(), this.collection);
    if (action != null) {
      property.getOperations().add(buildOperation(action, buildOperationTarget(contextURL)));
    }
    
    List<EdmFunction> functions = this.metadata.getEdm()
        .getBoundFunctionsWithBindingType(edmType.getFullQualifiedName(), this.collection);
    
    for (EdmFunction function:functions) {
      property.getOperations().add(buildOperation(function, buildOperationTarget(contextURL)));
    }
  }    

  if (edmType.getKind() == EdmTypeKind.PRIMITIVE) {
    writePrimitiveProperty((EdmPrimitiveType) edmType, property);
  } else {
    writeComplexProperty((EdmComplexType) edmType, property);
  }
}
 
Example 16
Source File: ClientPrimitiveValueImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public BuilderImpl setType(final EdmType type) {
  EdmPrimitiveTypeKind primitiveTypeKind = null;
  if (type != null) {
    if (type.getKind() != EdmTypeKind.PRIMITIVE) {
      throw new IllegalArgumentException(String.format("Provided type %s is not primitive", type));
    }
    primitiveTypeKind = EdmPrimitiveTypeKind.valueOf(type.getName());
  }
  return setType(primitiveTypeKind);
}