Java Code Examples for org.apache.olingo.commons.api.edm.EdmEntityType#getProperty()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmEntityType#getProperty() . 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: 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 2
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void consumeEntityProperties(final EdmEntityType edmEntityType, final ObjectNode node,
    final Entity entity) throws DeserializerException {
  List<String> propertyNames = edmEntityType.getPropertyNames();
  for (String propertyName : propertyNames) {
    JsonNode jsonNode = node.get(propertyName);
    if (jsonNode != null) {
      EdmProperty edmProperty = (EdmProperty) edmEntityType.getProperty(propertyName);
      if (jsonNode.isNull() && !edmProperty.isNullable()) {
        throw new DeserializerException("Property: " + propertyName + " must not be null.",
            DeserializerException.MessageKeys.INVALID_NULL_PROPERTY, propertyName);
      }
      Property property = consumePropertyNode(edmProperty.getName(), edmProperty.getType(),
          edmProperty.isCollection(), edmProperty.isNullable(), edmProperty.getMaxLength(),
          edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), edmProperty.getMapping(),
          jsonNode);
      entity.addProperty(property);
      node.remove(propertyName);
    }
  }
}
 
Example 3
Source File: UriResourceImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void uriResourceNavigationPropertyImpl() {
  EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
  EdmNavigationProperty property = (EdmNavigationProperty) entityType.getProperty("NavPropertyETKeyNavMany");
  assertNotNull(property);

  UriResourceNavigationPropertyImpl impl = new UriResourceNavigationPropertyImpl(property);
  assertEquals(UriResourceKind.navigationProperty, impl.getKind());
  assertEquals(property, impl.getProperty());

  assertEquals("NavPropertyETKeyNavMany", impl.toString());
  assertEquals(property.getType(), impl.getType());

  assertTrue(impl.isCollection());
  impl.setKeyPredicates(Collections.singletonList(
      (UriParameter) new UriParameterImpl().setName("ParameterInt16")));
  assertFalse(impl.isCollection());
}
 
Example 4
Source File: UriResourceImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void uriResourceComplexPropertyImpl() {
  EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
  EdmProperty property = (EdmProperty) entityType.getProperty("PropertyCompNav");
  UriResourceComplexPropertyImpl impl = new UriResourceComplexPropertyImpl(property);
  assertEquals(UriResourceKind.complexProperty, impl.getKind());
  assertEquals(property, impl.getProperty());
  assertEquals(property.getName(), impl.toString());
  assertFalse(impl.isCollection());
  assertEquals(property.getType(), impl.getType());
  assertEquals(property.getType(), impl.getComplexType());
  impl.getComplexType();

  EdmComplexType complexTypeImplType = edm.getComplexType(ComplexTypeProvider.nameCTBasePrimCompNav);

  impl.setTypeFilter(complexTypeImplType);
  assertEquals(complexTypeImplType, impl.getTypeFilter());
  assertEquals(complexTypeImplType, impl.getComplexTypeFilter());
  impl.getComplexTypeFilter();

}
 
Example 5
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private ODataEntry getKeyPredicatesFromEntity(EdmEntityType entityType, Entity entity)
        throws ODataApplicationException {
    ODataEntry keyPredicates = new ODataEntry();
    for (String key : entityType.getKeyPredicateNames()) {
        EdmProperty propertyType = (EdmProperty) entityType.getProperty(key);
        keyPredicates.addValue(key, readPrimitiveValueInString(propertyType, entity.getProperty(key).getValue()));
    }
    return keyPredicates;
}
 
Example 6
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void readAnnotationOnAProperty() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmEntityType entity = edm.getEntityTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "I_DraftAdministrativeDataType"));
  EdmProperty property = (EdmProperty) entity.getProperty("DraftUUID");
  assertNotNull(property.getAnnotations());
  assertEquals(1, property.getAnnotations().size());
  assertEquals("UI.HeaderInfo", property.getAnnotations().get(0).getTerm().
      getFullQualifiedName().getFullQualifiedNameAsString());
}
 
Example 7
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity read(final EdmEntitySet edmEntitySet, final List<UriParameter> keys) throws DataProviderException {
  final EdmEntityType entityType = edmEntitySet.getEntityType();
  final EntityCollection entitySet = data.get(edmEntitySet.getName());
  if (entitySet == null) {
    return null;
  } else {
    try {
      for (final Entity entity : entitySet.getEntities()) {
        boolean found = true;
        for (final UriParameter key : keys) {
          final EdmProperty property = (EdmProperty) entityType.getProperty(key.getName());
          final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
          if (!type.valueToString(entity.getProperty(key.getName()).getValue(),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode())
              .equals(key.getText())) {
            found = false;
            break;
          }
        }
        if (found) {
          return entity;
        }
      }
      return null;
    } catch (final EdmPrimitiveTypeException e) {
      throw new DataProviderException("Wrong key!", e);
    }
  }
}
 
Example 8
Source File: EdmTypeValidator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param sourceEntityType
 * @param targetEntityType
 * @param navProperty
 */
private void validateReferentialConstraint(EdmEntityType sourceEntityType, EdmEntityType targetEntityType,
    EdmNavigationProperty navProperty) {
  if (!navProperty.getReferentialConstraints().isEmpty()) {
    String propertyName = navProperty.getReferentialConstraints().get(0).getPropertyName();
    if (sourceEntityType.getProperty(propertyName) == null) {
      throw new RuntimeException("Property name "+ propertyName + " not part of the source entity.");
    }
    String referencedPropertyName = navProperty.getReferentialConstraints().get(0).getReferencedPropertyName();
    if (targetEntityType.getProperty(referencedPropertyName) == null) {
      throw new RuntimeException("Property name " + referencedPropertyName + " not part of the target entity.");
    }
  }
}
 
Example 9
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method updates entity in tables where the request doesn't specify the odata e-tag.
 *
 * @param edmEntityType Entity type
 * @param entity        Entity
 * @param keys          Keys
 * @param merge         Merge
 * @throws DataServiceFault
 * @throws ODataApplicationException
 */
private void updateEntity(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keys, boolean merge)
        throws DataServiceFault, ODataApplicationException {
    ODataEntry entry = new ODataEntry();
    for (UriParameter key : keys) {
        String value = key.getText();
        if (value.startsWith("'") && value.endsWith("'")) {
            value = value.substring(1, value.length() - 1);
        }
        entry.addValue(key.getName(), value);
    }
    for (String property : edmEntityType.getPropertyNames()) {
        Property updateProperty = entity.getProperty(property);
        if (isKey(edmEntityType, property)) {
            continue;
        }
        // the request payload might not consider ALL properties, so it can be null
        if (updateProperty == null) {
            // if a property has NOT been added to the request payload
            // depending on the HttpMethod, our behavior is different
            if (merge) {
                // as of the OData spec, in case of PATCH, the existing property is not touched
                continue;
            } else {
                // as of the OData spec, in case of PUT, the existing property is set to null (or to default value)
                entry.addValue(property, null);
                continue;
            }
        }
        EdmProperty propertyType = (EdmProperty) edmEntityType.getProperty(property);
        entry.addValue(property, readPrimitiveValueInString(propertyType, updateProperty.getValue()));
    }
    this.dataHandler.updateEntityInTable(edmEntityType.getName(), entry);
}
 
Example 10
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
   * This method updates the entity to the table by invoking ODataDataHandler updateEntityInTable method.
   *
   * @param edmEntityType  EdmEntityType
   * @param entity         entity with changes
   * @param existingEntity existing entity
   * @param merge          PUT/PATCH
   * @throws ODataApplicationException
   * @throws DataServiceFault
   * @see ODataDataHandler#updateEntityInTableTransactional(String, ODataEntry, ODataEntry)
   */
  private boolean updateEntityWithETagMatched(EdmEntityType edmEntityType, Entity entity, Entity existingEntity,
                                              boolean merge) throws ODataApplicationException, DataServiceFault {
/* loop over all properties and replace the values with the values of the given payload
   Note: ignoring ComplexType, as we don't have it in wso2dss oData model */
      List<Property> oldProperties = existingEntity.getProperties();
      ODataEntry newProperties = new ODataEntry();
      Map<String, EdmProperty> propertyMap = new HashMap<>();
      for (String property : edmEntityType.getPropertyNames()) {
          Property updateProperty = entity.getProperty(property);
          EdmProperty propertyType = (EdmProperty) edmEntityType.getProperty(property);
          if (isKey(edmEntityType, property)) {
              propertyMap.put(property, (EdmProperty) edmEntityType.getProperty(property));
              continue;
          }
          // the request payload might not consider ALL properties, so it can be null
          if (updateProperty == null) {
              // if a property has NOT been added to the request payload
              // depending on the HttpMethod, our behavior is different
              if (merge) {
                  // as of the OData spec, in case of PATCH, the existing property is not touched
                  propertyMap.put(property, (EdmProperty) edmEntityType.getProperty(property));
                  continue;
              } else {
                  // as of the OData spec, in case of PUT, the existing property is set to null (or to default value)
                  propertyMap.put(property, (EdmProperty) edmEntityType.getProperty(property));
                  newProperties.addValue(property, null);
                  continue;
              }
          }
          propertyMap.put(property, (EdmProperty) edmEntityType.getProperty(property));
          newProperties.addValue(property, readPrimitiveValueInString(propertyType, updateProperty.getValue()));
      }
      return this.dataHandler.updateEntityInTableTransactional(edmEntityType.getName(),
                                                               wrapPropertiesToDataEntry(edmEntityType, oldProperties,
                                                                                         propertyMap), newProperties);
  }
 
Example 11
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 12
Source File: ExpandSystemQueryOptionHandler.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void applyExpandOptionToEntity(final Entity entity, final EdmBindingTarget edmBindingTarget,
    final ExpandOption expandOption, final UriInfoResource uriInfo, final Edm edm) throws ODataApplicationException {

  final EdmEntityType entityType = edmBindingTarget.getEntityType();

  for (ExpandItem item : expandOption.getExpandItems()) {
    if(item.getLevelsOption() != null) {
      throw new ODataApplicationException("$levels is not implemented", 
          HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
    }
    
    List<EdmNavigationProperty> navigationProperties = new ArrayList<EdmNavigationProperty>();
    if(item.isStar()) {
      List<EdmNavigationPropertyBinding> bindings = edmBindingTarget.getNavigationPropertyBindings();
      for (EdmNavigationPropertyBinding binding : bindings) {
        EdmElement property = entityType.getProperty(binding.getPath());
        if(property instanceof EdmNavigationProperty) {
          navigationProperties.add((EdmNavigationProperty) property);
        }
      }
    } else {
      final List<UriResource> uriResourceParts = item.getResourcePath().getUriResourceParts();
      if (uriResourceParts.get(0) instanceof UriResourceNavigation) {
        navigationProperties.add(((UriResourceNavigation) uriResourceParts.get(0)).getProperty());
      }
    }

    for (EdmNavigationProperty navigationProperty: navigationProperties) {
      final String navPropertyName = navigationProperty.getName();
      final EdmBindingTarget targetEdmEntitySet = edmBindingTarget.getRelatedBindingTarget(navPropertyName);

      final Link link = entity.getNavigationLink(navPropertyName);
      if (link != null && entityType.getNavigationProperty(navPropertyName).isCollection()) {
        applyOptionsToEntityCollection(link.getInlineEntitySet(),
            targetEdmEntitySet,
            item.getFilterOption(),
            item.getOrderByOption(),
            item.getCountOption(),
            item.getSkipOption(),
            item.getTopOption(),
            item.getExpandOption(),
            uriInfo, edm);
      }
    }
  }
}
 
Example 13
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 14
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 15
Source File: EntityResponse.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static String buildLocation(String baseURL, Entity entity, String enitySetName, EdmEntityType type) 
    throws EdmPrimitiveTypeException {
  StringBuilder location = new StringBuilder();

  location.append(baseURL).append("/").append(enitySetName);
  
  int i = 0;
  boolean usename = type.getKeyPredicateNames().size() > 1;
  location.append("(");
  for (String key : type.getKeyPredicateNames()) {
    if (i > 0) {
      location.append(",");
    }
    i++;
    if (usename) {
      location.append(key).append("=");
    }
    
    EdmProperty property = (EdmProperty)type.getProperty(key);
    String propertyType = entity.getProperty(key).getType();
    Object propertyValue = entity.getProperty(key).getValue();
    
    if (propertyValue == null) {
      throw new EdmPrimitiveTypeException("The key value for property "+key+" is invalid; Key value cannot be null");
    }
    
    if(propertyType.startsWith("Edm.")) {
      propertyType = propertyType.substring(4);
    }
    EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(propertyType);
    String value =  EdmPrimitiveTypeFactory.getInstance(kind).valueToString(
        propertyValue, property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(), 
        property.isUnicode());
    if (kind == EdmPrimitiveTypeKind.String) {
        value = EdmString.getInstance().toUriLiteral(Encoder.encode(value));
    }
    location.append(value);
  }
  location.append(")");
  return location.toString();
}
 
Example 16
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 17
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
        throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
              .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}
 
Example 18
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity rt_entity,
    List<UriParameter> keyParams) {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();

    // note: below line doesn't consider: keyProp can be part of a complexType in V4
    // in such case, it would be required to access it via getKeyPropertyRef()
    // but since this isn't the case in our model, we ignore it in our implementation
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    // Edm: we need this info for the comparison below
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    // if(EdmType instanceof EdmPrimitiveType) // do we need this?
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in FWK
    Object valueObject = rt_entity.getProperty(keyName).getValue();
    // TODO if the property is a complex type

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable,
          maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      return false; // TODO proper Exception handling
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    // if any of the key properties is not found in the entity, we don't need to search further
    if (!matches) {
      return false;
    }
    // if the given key value is found in the current entity, continue with the next key
  }

  return true;
}
 
Example 19
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void readAnnotationOnEntitySet() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmEntityContainer container = edm.getEntityContainer();
  EdmEntitySet entitySet = container.getEntitySet("I_DraftAdministrativeData");
  assertEquals(1, entitySet.getAnnotations().size());
  assertEquals("HeaderInfo", entitySet.getAnnotations().get(0).getTerm().getName());

  EdmEntityType entityType50 = edm.getEntityTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "I_DraftAdministrativeDataType"));
  assertEquals(1, ((EdmProperty) entityType50.getProperty("DraftUUID")).getAnnotations().size());
  assertEquals("UI.HeaderInfo", ((EdmProperty) entityType50.getProperty("DraftUUID")).getAnnotations().get(0)
      .getTerm().getFullQualifiedName().getFullQualifiedNameAsString());

  // Annotations on properties of entity type included in EntitySet
  EdmEntityType entityType3 = entitySet.getEntityTypeWithAnnotations();
  assertEquals(3, ((EdmProperty) entityType3.getProperty("DraftUUID")).getAnnotations().size());
  assertEquals("AdditionalInfo", ((EdmProperty) entityType3.getProperty("DraftUUID"))
      .getAnnotations().get(0).getTerm().getName());
  assertEquals("HeaderInfo", ((EdmProperty) entityType3.getProperty("DraftUUID"))
      .getAnnotations().get(1).getTerm().getName());

  // Annotations on navigation properties of entity type included in EntitySet
  EdmEntitySet entitySet1 = container.getEntitySet("SEPMRA_C_SalesOrderCustCntctVH");
  EdmEntityType entityType5 = entitySet1.getEntityTypeWithAnnotations();
  assertEquals(2, ((EdmNavigationProperty) entityType5.getNavigationProperty("to_Customer"))
      .getAnnotations().size());
  assertEquals("AdditionalInfo", ((EdmNavigationProperty) entityType5
      .getNavigationProperty("to_Customer"))
          .getAnnotations().get(0).getTerm().getName());
  assertEquals("HeaderInfo", ((EdmNavigationProperty) entityType5
      .getNavigationProperty("to_Customer"))
          .getAnnotations().get(1).getTerm().getName());

 EdmComplexType complexType = edm.getComplexTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "CTPrim"));
  EdmProperty complexTypeProp = (EdmProperty) complexType.getProperty("PropertyInt16");
  assertEquals(1, complexTypeProp.getAnnotations().size());
  assertEquals("HeaderInfo", complexTypeProp.getAnnotations().get(0).getTerm().getName());

  // Annotations on properties of complex properties of entity type included in EntitySet
  EdmProperty complexProp = (EdmProperty) entityType3.getProperty("ComplexProperty");
  EdmComplexType compType = (EdmComplexType) complexProp.getTypeWithAnnotations();
  EdmProperty prop = (EdmProperty) compType.getProperty("PropertyInt16");
  assertEquals(2, prop.getAnnotations().size());
  assertEquals("AdditionalInfo", prop.getAnnotations().get(0).getTerm().getName());

  // Annotations on navigation properties of complex properties of entity type included in EntitySet
  EdmNavigationProperty navProp = (EdmNavigationProperty) compType
      .getProperty("NavPropertyDraftAdministrativeDataType");
  assertEquals(2, navProp.getAnnotations().size());
  assertEquals("AdditionalInfo", navProp.getAnnotations().get(0).getTerm().getName());
}
 
Example 20
Source File: Util.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams) 
    throws ODataApplicationException {

  // loop over all keys
  for (final UriParameter key : keyParams) {
    // key
    String keyName = key.getName();
    String keyText = key.getText();
    
    // Edm: we need this info for the comparison below
    EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
    Boolean isNullable = edmKeyProperty.isNullable();
    Integer maxLength = edmKeyProperty.getMaxLength();
    Integer precision = edmKeyProperty.getPrecision();
    Boolean isUnicode = edmKeyProperty.isUnicode();
    Integer scale = edmKeyProperty.getScale();
    // get the EdmType in order to compare
    EdmType edmType = edmKeyProperty.getType();
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

    // Runtime data: the value of the current entity
    // don't need to check for null, this is done in olingo library
    Object valueObject = entity.getProperty(keyName).getValue();

    // now need to compare the valueObject with the keyText String
    // this is done using the type.valueToString //
    String valueAsString = null;
    try {
      valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
          .getStatusCode(), Locale.ENGLISH, e);
    }

    if (valueAsString == null) {
      return false;
    }

    boolean matches = valueAsString.equals(keyText);
    if (!matches) {
      // if any of the key properties is not found in the entity, we don't need to search further
      return false;
    }
  }

  return true;
}