예제 #1
0
 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;
 }
예제 #2
0
 public static void applySearchSystemQueryOption(
     final SearchOption searchOption, EntityCollection entitySet)
     throws ODataApplicationException {
   if (searchOption != null) {
     SearchExpression se = searchOption.getSearchExpression();
     Iterator<Entity> it = entitySet.getEntities().iterator();
     while (it.hasNext()) {
       boolean keep = false;
       Entity entity = it.next();
       ListIterator<Property> properties = entity.getProperties().listIterator();
       while (properties.hasNext() && !keep) {
         keep = isTrue(se, properties.next());
       }
       if (!keep) {
         it.remove();
       }
     }
   }
 }
예제 #3
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;
  }
예제 #4
0
 @Override
 public void readPrimitiveValue(
     ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType format)
     throws ODataApplicationException, SerializerException {
   // First we have to figure out which entity set the requested entity is in
   final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
   // Next we fetch the requested entity from the database
   final Entity entity;
   try {
     entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
   } catch (DataProviderException e) {
     throw new ODataApplicationException(e.getMessage(), 500, Locale.ENGLISH);
   }
   if (entity == null) {
     // If no entity was found for the given key we throw an exception.
     throw new ODataApplicationException(
         "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
   } else {
     // Next we get the property value from the entity and pass the value to serialization
     UriResourceProperty uriProperty =
         (UriResourceProperty)
             uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
     EdmProperty edmProperty = uriProperty.getProperty();
     Property property = entity.getProperty(edmProperty.getName());
     if (property == null) {
       throw new ODataApplicationException(
           "No property found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
     } else {
       if (property.getValue() == null) {
         response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
       } else {
         String value = String.valueOf(property.getValue());
         ByteArrayInputStream serializerContent =
             new ByteArrayInputStream(value.getBytes(Charset.forName("UTF-8")));
         response.setContent(serializerContent);
         response.setStatusCode(HttpStatusCode.OK.getStatusCode());
         response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.TEXT_PLAIN.toContentTypeString());
       }
     }
   }
 }
예제 #5
0
  private void readProperty(
      ODataResponse response, UriInfo uriInfo, ContentType contentType, boolean complex)
      throws ODataApplicationException, SerializerException {
    // To read a property we have to first get the entity out of the entity set
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
    Entity entity;
    try {
      entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
    } catch (DataProviderException e) {
      throw new ODataApplicationException(
          e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
    }

    if (entity == null) {
      // If no entity was found for the given key we throw an exception.
      throw new ODataApplicationException(
          "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
    } else {
      // Next we get the property value from the entity and pass the value to serialization
      UriResourceProperty uriProperty =
          (UriResourceProperty)
              uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
      EdmProperty edmProperty = uriProperty.getProperty();
      Property property = entity.getProperty(edmProperty.getName());
      if (property == null) {
        throw new ODataApplicationException(
            "No property found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
      } else {
        if (property.getValue() == null) {
          response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
        } else {
          ODataSerializer serializer = odata.createSerializer(contentType);
          final ContextURL contextURL =
              isODataMetadataNone(contentType)
                  ? null
                  : getContextUrl(edmEntitySet, true, null, null, edmProperty.getName());
          InputStream serializerContent =
              complex
                  ? serializer
                      .complex(
                          edm,
                          (EdmComplexType) edmProperty.getType(),
                          property,
                          ComplexSerializerOptions.with().contextURL(contextURL).build())
                      .getContent()
                  : serializer
                      .primitive(
                          edm,
                          (EdmPrimitiveType) edmProperty.getType(),
                          property,
                          PrimitiveSerializerOptions.with()
                              .contextURL(contextURL)
                              .scale(edmProperty.getScale())
                              .nullable(edmProperty.isNullable())
                              .precision(edmProperty.getPrecision())
                              .maxLength(edmProperty.getMaxLength())
                              .unicode(edmProperty.isUnicode())
                              .build())
                      .getContent();
          response.setContent(serializerContent);
          response.setStatusCode(HttpStatusCode.OK.getStatusCode());
          response.setHeader(HttpHeader.CONTENT_TYPE, contentType.toContentTypeString());
        }
      }
    }
  }
예제 #6
0
  @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
        }
      }
    }
  }