private void appendKey(final XMLStreamWriter writer, final EdmEntityType entityType)
      throws XMLStreamException {
    List<EdmKeyPropertyRef> keyPropertyRefs = entityType.getKeyPropertyRefs();
    if (keyPropertyRefs != null && !keyPropertyRefs.isEmpty()) {
      // Resolve Base Type key as it is shown in derived type
      EdmEntityType baseType = entityType.getBaseType();
      if (baseType != null
          && baseType.getKeyPropertyRefs() != null
          && !(baseType.getKeyPropertyRefs().isEmpty())) {
        return;
      }

      writer.writeStartElement(XML_KEY);
      for (EdmKeyPropertyRef keyRef : keyPropertyRefs) {
        writer.writeEmptyElement(XML_PROPERTY_REF);

        writer.writeAttribute(XML_NAME, keyRef.getName());

        if (keyRef.getAlias() != null) {
          writer.writeAttribute(XML_ALIAS, keyRef.getAlias());
        }
      }
      writer.writeEndElement();
    }
  }
 private UpdateResponse performDeepInsert(
     String rawURI,
     UriInfo uriInfo,
     EdmEntityType entityType,
     Entity entity,
     List<ExpandNode> expandNodes)
     throws SQLException, TeiidException {
   UpdateResponse response = performInsert(rawURI, uriInfo, entityType, entity);
   for (String navigationName : entityType.getNavigationPropertyNames()) {
     EdmNavigationProperty navProperty = entityType.getNavigationProperty(navigationName);
     Link navLink = entity.getNavigationLink(navigationName);
     if (navLink != null && navLink.getInlineEntity() != null) {
       ExpandNode node = new ExpandNode();
       node.navigationProperty = navProperty;
       expandNodes.add(node);
       performDeepInsert(
           rawURI, uriInfo, navProperty.getType(), navLink.getInlineEntity(), node.children);
     } else if (navLink != null
         && navLink.getInlineEntitySet() != null
         && !navLink.getInlineEntitySet().getEntities().isEmpty()) {
       ExpandNode node = new ExpandNode();
       node.navigationProperty = navProperty;
       expandNodes.add(node);
       for (Entity inlineEntity : navLink.getInlineEntitySet().getEntities()) {
         performDeepInsert(rawURI, uriInfo, navProperty.getType(), inlineEntity, node.children);
       }
     }
   }
   return response;
 }
  private void appendEntityTypes(
      final XMLStreamWriter writer, final List<EdmEntityType> entityTypes)
      throws XMLStreamException {
    for (EdmEntityType entityType : entityTypes) {
      writer.writeStartElement(XML_ENTITY_TYPE);
      writer.writeAttribute(XML_NAME, entityType.getName());

      if (entityType.hasStream()) {
        writer.writeAttribute(XML_HAS_STREAM, "" + entityType.hasStream());
      }

      if (entityType.getBaseType() != null) {
        writer.writeAttribute(
            XML_BASE_TYPE, getAliasedFullQualifiedName(entityType.getBaseType(), false));
      }

      if (entityType.isAbstract()) {
        writer.writeAttribute(ABSTRACT, TRUE);
      }

      appendKey(writer, entityType);

      appendProperties(writer, entityType);

      appendNavigationProperties(writer, entityType);

      writer.writeEndElement();
    }
  }
Exemple #4
0
  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;
  }
  @Override
  public void createEntity(DataRequest request, Entity entity, EntityResponse response)
      throws ODataLibraryException, ODataApplicationException {

    EdmEntityType entityType = request.getEntitySet().getEntityType();

    String txn;
    try {
      txn = getClient().startTransaction();
    } catch (SQLException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    }
    boolean success = false;

    try {
      List<ExpandNode> expands = new ArrayList<TeiidServiceHandler.ExpandNode>();
      UpdateResponse updateResponse =
          performDeepInsert(
              request.getODataRequest().getRawBaseUri(),
              request.getUriInfo(),
              entityType,
              entity,
              expands);

      if (updateResponse != null && updateResponse.getUpdateCount() == 1) {
        ODataSQLBuilder visitor =
            new ODataSQLBuilder(
                this.odata,
                getClient().getMetadataStore(),
                true,
                false,
                request.getODataRequest().getRawBaseUri(),
                this.serviceMetadata,
                this.nameGenerator);

        Query query =
            visitor.selectWithEntityKey(
                entityType, entity, updateResponse.getGeneratedKeys(), expands);
        LogManager.logDetail(
            LogConstants.CTX_ODATA,
            null,
            "created entity = ",
            entityType.getName(),
            " with key=",
            query.getCriteria().toString()); // $NON-NLS-1$ //$NON-NLS-2$

        EntityCollectionResponse result =
            new EntityCollectionResponse(
                request.getODataRequest().getRawBaseUri(), visitor.getContext());

        getClient().executeSQL(query, visitor.getParameters(), false, null, null, null, 1, result);

        if (!result.getEntities().isEmpty()) {
          entity = result.getEntities().get(0);
          String location =
              EntityResponse.buildLocation(
                  request.getODataRequest().getRawBaseUri(),
                  entity,
                  request.getEntitySet().getName(),
                  entityType);
          entity.setId(new URI(location));
        }
        response.writeCreatedEntity(request.getEntitySet(), entity);
      } else {
        response.writeNotModified();
      }
      getClient().commit(txn);
      success = true;
    } catch (SQLException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } catch (URISyntaxException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } catch (TeiidException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } finally {
      if (!success) {
        try {
          getClient().rollback(txn);
        } catch (SQLException e1) {
          // ignore
        }
      }
    }
  }