Exemple #1
0
  protected void deleteEntityAndTest(ODataConsumer consumer, String customerID) {

    OEntity customer = consumer.getEntity("Customers", customerID).execute();
    Assert.assertNotNull(customer);
    Assert.assertEquals(customerID, customer.getEntityKey().asSingleValue());

    Assert.assertNotNull(
        consumer
            .getEntities("Customers")
            .execute()
            .firstOrNull(OPredicates.entityPropertyValueEquals("CustomerID", customerID)));

    consumer.deleteEntity("Customers", customer.getEntityKey()).execute();

    Assert.assertNull(
        consumer
            .getEntities("Customers")
            .execute()
            .firstOrNull(OPredicates.entityPropertyValueEquals("CustomerID", customerID)));
  }
 private OEntity processOEntity(EntityResource<OEntity> entityResource) {
   List<OLink> embeddedLinks = formOLinks(entityResource);
   OEntity embeddedEntity = entityResource.getEntity();
   // replace the OLink's on the embedded entity
   OEntity newOEntity =
       OEntities.create(
           embeddedEntity.getEntitySet(),
           embeddedEntity.getEntityKey(),
           embeddedEntity.getProperties(),
           embeddedLinks);
   return newOEntity;
 }
 /*
  * Using the supplied EntityResource, add the embedded resources
  * from the OEntity embedded resources.  NB - only an OEntity can
  * carry OLinks.
  */
 public void addExpandedLinks(EntityResource<OEntity> entityResource) {
   RequestContext requestContext = RequestContext.getRequestContext();
   Collection<Link> links = entityResource.getLinks();
   if (links != null) {
     OEntity oentity = entityResource.getEntity();
     List<OLink> olinks = oentity.getLinks();
     for (OLink olink : olinks) {
       if (olink.isInline()) {
         String relid = InternalUtil.getEntityRelId(oentity);
         String href = relid + "/" + olink.getTitle();
         for (Link link : links) {
           String linkHref = link.getHref();
           if (requestContext != null) {
             // Extract the transition fragment from the URI path
             linkHref = link.getRelativeHref(getBaseUri(serviceDocument, uriInfo));
           }
           if (href.equals(linkHref)) {
             if (entityResource.getEmbedded() == null) {
               entityResource.setEmbedded(new HashMap<Transition, RESTResource>());
             }
             if (olink.isCollection()) {
               List<OEntity> oentities = olink.getRelatedEntities();
               Collection<EntityResource<OEntity>> entityResources =
                   new ArrayList<EntityResource<OEntity>>();
               for (OEntity oe : oentities) {
                 entityResources.add(new EntityResource<OEntity>(oe));
               }
               entityResource
                   .getEmbedded()
                   .put(link.getTransition(), new CollectionResource<OEntity>(entityResources));
             } else {
               // replace the OLink's on the current entity
               OEntity inlineOentity = olink.getRelatedEntity();
               List<OLink> inlineResourceOlinks =
                   formOLinks(new EntityResource<OEntity>(inlineOentity));
               OEntity newInlineOentity =
                   OEntities.create(
                       inlineOentity.getEntitySet(),
                       inlineOentity.getEntityKey(),
                       inlineOentity.getProperties(),
                       inlineResourceOlinks);
               entityResource
                   .getEmbedded()
                   .put(link.getTransition(), new EntityResource<OEntity>(newInlineOentity));
             }
           }
         }
       }
     }
   }
 }
  /**
   * Writes a Atom (OData) representation of {@link EntityResource} to the output stream.
   *
   * @precondition supplied {@link EntityResource} is non null
   * @precondition {@link EntityResource#getEntity()} returns a valid OEntity, this provider only
   *     supports serialising OEntities
   * @postcondition non null Atom (OData) XML document written to OutputStream
   * @invariant valid OutputStream
   */
  @SuppressWarnings("unchecked")
  @Override
  public void writeTo(
      RESTResource resource,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders,
      OutputStream entityStream)
      throws IOException, WebApplicationException {
    assert (resource != null);
    assert (uriInfo != null);
    // Set response headers
    if (httpHeaders != null) {
      httpHeaders.putSingle(
          HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML); // Workaround for
      // https://issues.apache.org/jira/browse/WINK-374
    }

    try {
      RESTResource restResource = processLinks((RESTResource) resource);
      Collection<Link> processedLinks = restResource.getLinks();
      if (ResourceTypeHelper.isType(type, genericType, EntityResource.class, OEntity.class)) {
        EntityResource<OEntity> entityResource = (EntityResource<OEntity>) resource;
        OEntity tempEntity = entityResource.getEntity();
        EdmEntitySet entitySet = getEdmEntitySet(entityResource.getEntityName());
        List<OLink> olinks = formOLinks(entityResource);
        // Write entry
        // create OEntity with our EdmEntitySet see issue
        // https://github.com/aphethean/IRIS/issues/20
        OEntity oentity =
            OEntities.create(
                entitySet, tempEntity.getEntityKey(), tempEntity.getProperties(), null);
        entryWriter.write(
            uriInfo,
            new OutputStreamWriter(entityStream, UTF_8),
            Responses.entity(oentity),
            entitySet,
            olinks);
      } else if (ResourceTypeHelper.isType(type, genericType, EntityResource.class, Entity.class)) {
        EntityResource<Entity> entityResource = (EntityResource<Entity>) resource;
        // Write entry
        Entity entity = entityResource.getEntity();
        String entityName = entityResource.getEntityName();
        // Write Entity object with Abdera implementation
        entityEntryWriter.write(
            uriInfo,
            new OutputStreamWriter(entityStream, UTF_8),
            entityName,
            entity,
            processedLinks,
            entityResource.getEmbedded());
      } else if (ResourceTypeHelper.isType(type, genericType, EntityResource.class)) {
        EntityResource<Object> entityResource = (EntityResource<Object>) resource;
        // Links and entity properties
        Object entity = entityResource.getEntity();
        String entityName = entityResource.getEntityName();
        EntityProperties props = new EntityProperties();
        if (entity != null) {
          Map<String, Object> objProps =
              (transformer != null ? transformer : new BeanTransformer()).transform(entity);
          if (objProps != null) {
            for (String propName : objProps.keySet()) {
              props.setProperty(new EntityProperty(propName, objProps.get(propName)));
            }
          }
        }
        entityEntryWriter.write(
            uriInfo,
            new OutputStreamWriter(entityStream, UTF_8),
            entityName,
            new Entity(entityName, props),
            processedLinks,
            entityResource.getEmbedded());
      } else if (ResourceTypeHelper.isType(
          type, genericType, CollectionResource.class, OEntity.class)) {
        CollectionResource<OEntity> collectionResource = ((CollectionResource<OEntity>) resource);
        EdmEntitySet entitySet = getEdmEntitySet(collectionResource.getEntityName());
        List<EntityResource<OEntity>> collectionEntities =
            (List<EntityResource<OEntity>>) collectionResource.getEntities();
        List<OEntity> entities = new ArrayList<OEntity>();
        Map<OEntity, Collection<Link>> linkId = new HashMap<OEntity, Collection<Link>>();
        for (EntityResource<OEntity> collectionEntity : collectionEntities) {
          // create OEntity with our EdmEntitySet see issue
          // https://github.com/aphethean/IRIS/issues/20
          OEntity tempEntity = collectionEntity.getEntity();
          List<OLink> olinks = formOLinks(collectionEntity);
          Collection<Link> links = collectionEntity.getLinks();
          OEntity entity =
              OEntities.create(
                  entitySet,
                  null,
                  tempEntity.getEntityKey(),
                  tempEntity.getEntityTag(),
                  tempEntity.getProperties(),
                  olinks);
          entities.add(entity);
          linkId.put(entity, links);
        }
        // TODO implement collection properties and get transient values for inlinecount and
        // skiptoken
        Integer inlineCount = null;
        String skipToken = null;
        feedWriter.write(
            uriInfo,
            new OutputStreamWriter(entityStream, UTF_8),
            processedLinks,
            Responses.entities(entities, entitySet, inlineCount, skipToken),
            metadata.getModelName(),
            linkId);
      } else if (ResourceTypeHelper.isType(
          type, genericType, CollectionResource.class, Entity.class)) {
        CollectionResource<Entity> collectionResource = ((CollectionResource<Entity>) resource);

        // TODO implement collection properties and get transient values for inlinecount and
        // skiptoken
        Integer inlineCount = null;
        String skipToken = null;
        // Write feed
        AtomEntityFeedFormatWriter entityFeedWriter =
            new AtomEntityFeedFormatWriter(serviceDocument, metadata);
        entityFeedWriter.write(
            uriInfo,
            new OutputStreamWriter(entityStream, UTF_8),
            collectionResource,
            inlineCount,
            skipToken,
            metadata.getModelName());
      } else {
        logger.error(
            "Accepted object for writing in isWriteable, but type not supported in writeTo method");
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
      }
    } catch (ODataProducerException e) {
      logger.error("An error occurred while writing " + mediaType + " resource representation", e);
    }
  }