Пример #1
0
 /*
  * 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));
             }
           }
         }
       }
     }
   }
 }
Пример #2
0
 private void addLinkToOLinks(List<OLink> olinks, Link link, RESTResource resource) {
   RequestContext requestContext = RequestContext.getRequestContext();
   assert (link != null);
   assert (link.getTransition() != null);
   Map<Transition, RESTResource> embeddedResources = resource.getEmbedded();
   String rel = link.getRel();
   String href = link.getHref();
   if (requestContext != null) {
     // Extract the transition fragment from the URI path
     href = link.getRelativeHref(getBaseUri(serviceDocument, uriInfo));
   }
   String title = link.getTitle();
   OLink olink = null;
   Transition linkTransition = link.getTransition();
   if (linkTransition != null) {
     if (embeddedResources != null
         && embeddedResources.get(linkTransition) != null
         && embeddedResources.get(linkTransition) instanceof EntityResource) {
       @SuppressWarnings("unchecked")
       EntityResource<OEntity> embeddedResource =
           (EntityResource<OEntity>) embeddedResources.get(linkTransition);
       // replace the OLink's on the embedded entity
       OEntity newEmbeddedEntity = processOEntity(embeddedResource);
       olink = OLinks.relatedEntityInline(rel, title, href, newEmbeddedEntity);
     } else if (embeddedResources != null
         && embeddedResources.get(linkTransition) != null
         && embeddedResources.get(linkTransition) instanceof CollectionResource) {
       @SuppressWarnings("unchecked")
       CollectionResource<OEntity> embeddedCollectionResource =
           (CollectionResource<OEntity>) embeddedResources.get(linkTransition);
       List<OEntity> entities = new ArrayList<OEntity>();
       for (EntityResource<OEntity> embeddedResource : embeddedCollectionResource.getEntities()) {
         // replace the OLink's on the embedded entity
         OEntity newEmbeddedEntity = processOEntity(embeddedResource);
         entities.add(newEmbeddedEntity);
       }
       olink = OLinks.relatedEntitiesInline(rel, title, href, entities);
     }
     if (olink == null) {
       if (linkTransition.getTarget() instanceof CollectionResourceState) {
         olink = OLinks.relatedEntities(rel, title, href);
       } else {
         olink = OLinks.relatedEntity(rel, title, href);
       }
     }
   }
   olinks.add(olink);
 }
  protected void writeEntry(
      StreamWriter writer,
      String entityName,
      Entity entity,
      Collection<Link> entityLinks,
      Map<Transition, RESTResource> embeddedResources,
      String baseUri,
      String absoluteId,
      String updated) {
    assert (entityName != null);

    // entity name could be different between entity resource and underlying entity
    // e.g., for Errors entity, entity resource would have the request entity name
    String entityMetadataName = entity != null ? entity.getName() : entityName;

    EntityMetadata entityMetadata = metadata.getEntityMetadata(entityMetadataName);
    String modelName = metadata.getModelName();
    writer.writeId(absoluteId);
    OAtomEntity oae = getAtomInfo(entity);

    writer.writeTitle(oae.getAtomEntityTitle());
    String summary = oae.getAtomEntitySummary();
    if (!summary.isEmpty()) {
      writer.writeSummary(summary);
    }

    LocalDateTime updatedTime = oae.getAtomEntityUpdated();
    if (updatedTime != null) {
      updated = InternalUtil.toString(updatedTime.toDateTime(DateTimeZone.UTC));
    }

    writer.writeUpdated(updated);
    writer.writeAuthor(oae.getAtomEntityAuthor());

    if (entityLinks != null) {
      for (Link link : entityLinks) {
        String type =
            (link.getTransition().getTarget() instanceof CollectionResourceState)
                ? atom_feed_content_type
                : atom_entry_content_type;
        String href = link.getRelativeHref(baseUri);
        String rel = link.getRel();
        writer.startLink(href, rel);

        if ("self".equals(link.getRel())) {
          ResourceState target = link.getTransition().getTarget();
          writer.writeAttribute("profile", target.getRel());
        }

        if (!"self".equals(link.getRel()) && !"edit".equals(link.getRel())) {
          writer.writeAttribute("type", type);
        }
        writer.writeAttribute("title", link.getTitle());
        if (embeddedResources != null && embeddedResources.get(link.getTransition()) != null) {
          String embeddedAbsoluteId = link.getHref();
          writeLinkInline(
              writer,
              metadata,
              link,
              embeddedResources.get(link.getTransition()),
              link.getHref(),
              baseUri,
              embeddedAbsoluteId,
              updated);
        }
        String linkId = link.getLinkId();
        if (linkId != null && linkId.length() > 0) {
          writer.writeAttribute("id", linkId);
        }
        writer.endLink();
      }
    }

    writer.writeCategory(modelName + Metadata.MODEL_SUFFIX + "." + entityName, scheme);
    writer.flush();

    writer.startContent(MediaType.APPLICATION_XML);

    writer.startElement(new QName(m, "properties", "m"));
    if (entity != null) {
      writeProperties(writer, entityMetadata, entity.getProperties(), modelName);
    }
    writer.endElement();

    writer.endContent();
  }