protected void writeLinkInline(
      XMLWriter2 writer,
      OLink linkToInline,
      String href,
      String baseUri,
      String updated,
      boolean isResponse) {

    writer.startElement(new QName2(m, "inline", "m"));
    if (linkToInline instanceof ORelatedEntitiesLinkInline) {
      ORelatedEntitiesLinkInline relLink = ((ORelatedEntitiesLinkInline) linkToInline);
      List<OEntity> entities = relLink.getRelatedEntities();

      if (entities != null && !entities.isEmpty()) {
        writer.startElement(new QName2("feed"));
        writeElement(writer, "title", linkToInline.getTitle(), "type", "text");

        writeElement(writer, "id", baseUri + href);
        writeElement(writer, "updated", updated);
        writeElement(
            writer, "link", null, "rel", "self", "title", linkToInline.getTitle(), "href", href);

        for (OEntity entity : ((ORelatedEntitiesLinkInline) linkToInline).getRelatedEntities()) {
          writer.startElement("entry");
          writeEntry(
              writer,
              entity,
              entity.getProperties(),
              entity.getLinks(),
              entity.getEntitySet().name,
              baseUri,
              updated,
              entity.getEntitySet(),
              isResponse);

          writer.endElement("entry");
        }
        writer.endElement("feed");
      }
    } else if (linkToInline instanceof ORelatedEntityLinkInline) {
      OEntity entity = ((ORelatedEntityLinkInline) linkToInline).getRelatedEntity();
      if (entity != null) {
        writer.startElement("entry");
        writeEntry(
            writer,
            entity,
            entity.getProperties(),
            entity.getLinks(),
            entity.getEntitySet().name,
            baseUri,
            updated,
            entity.getEntitySet(),
            isResponse);

        writer.endElement("entry");
      }
    } else throw new RuntimeException("Unknown OLink type " + linkToInline.getClass());
    writer.endElement("inline");
  }
 /*
  * 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));
             }
           }
         }
       }
     }
   }
 }
Beispiel #3
0
  protected String writeEntry(
      XMLWriter2 writer,
      OEntity oe,
      List<OProperty<?>> entityProperties,
      List<OLink> entityLinks,
      String baseUri,
      String updated,
      EdmEntitySet ees,
      boolean isResponse) {

    String relid = null;
    String absid = null;
    if (isResponse) {
      relid = InternalUtil.getEntityRelId(oe);
      absid = baseUri + relid;
      writeElement(writer, "id", absid);
    }

    OAtomEntity oae = getAtomInfo(oe);

    writeElement(writer, "title", oae.getAtomEntityTitle(), "type", "text");
    String summary = oae.getAtomEntitySummary();
    if (summary != null) {
      writeElement(writer, "summary", summary, "type", "text");
    }

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

    writer.startElement("author");
    writeElement(writer, "name", oae.getAtomEntityAuthor());
    writer.endElement("author");

    if (isResponse) {
      writeElement(
          writer, "link", null, "rel", "edit", "title", ees.getType().getName(), "href", relid);
    }

    if (entityLinks != null) {
      if (isResponse) {
        // the producer has populated the link collection, we just what he gave us.
        for (OLink link : entityLinks) {
          String rel = related + link.getTitle();
          String type = (link.isCollection()) ? atom_feed_content_type : atom_entry_content_type;
          String href = relid + "/" + link.getTitle();
          if (link.isInline()) {
            writer.startElement("link");
            writer.writeAttribute("rel", rel);
            writer.writeAttribute("type", type);
            writer.writeAttribute("title", link.getTitle());
            writer.writeAttribute("href", href);
            // write the inlined entities inside the link element
            writeLinkInline(writer, link, href, baseUri, updated, isResponse);
            writer.endElement("link");
          } else {
            // deferred link.
            writeElement(
                writer,
                "link",
                null,
                "rel",
                rel,
                "type",
                type,
                "title",
                link.getTitle(),
                "href",
                href);
          }
        }
      } else {
        // for requests we include only the provided links
        // Note: It seems that OLinks for responses are only built using the
        // title and OLinks for requests have the additional info in them
        // alread.  I'm leaving that inconsistency in place for now but this
        // else and its preceding if could probably be unified.
        for (OLink olink : entityLinks) {
          String type = olink.isCollection() ? atom_feed_content_type : atom_entry_content_type;

          writer.startElement("link");
          writer.writeAttribute("rel", olink.getRelation());
          writer.writeAttribute("type", type);
          writer.writeAttribute("title", olink.getTitle());
          writer.writeAttribute("href", olink.getHref());
          if (olink.isInline()) {
            // write the inlined entities inside the link element
            writeLinkInline(writer, olink, olink.getHref(), baseUri, updated, isResponse);
          }
          writer.endElement("link");
        }
      }
    } // else entityLinks null

    writeElement(
        writer,
        "category",
        null,
        // oe is null for creates
        "term",
        oe == null
            ? ees.getType().getFullyQualifiedTypeName()
            : oe.getEntityType().getFullyQualifiedTypeName(),
        "scheme",
        scheme);

    boolean hasStream = false;
    if (oe != null) {
      OAtomStreamEntity stream = oe.findExtension(OAtomStreamEntity.class);
      if (stream != null) {
        hasStream = true;
        writer.startElement("content");
        writer.writeAttribute("type", stream.getAtomEntityType());
        writer.writeAttribute("src", baseUri + stream.getAtomEntitySource());
        writer.endElement("content");
      }
    }

    if (!hasStream) {
      writer.startElement("content");
      writer.writeAttribute("type", MediaType.APPLICATION_XML);
    }

    writer.startElement(new QName2(m, "properties", "m"));
    writeProperties(writer, entityProperties);
    writer.endElement("properties");

    if (!hasStream) {
      writer.endElement("content");
    }
    return absid;
  }
  protected String writeEntry(
      XMLWriter2 writer,
      OEntity oe,
      List<OProperty<?>> entityProperties,
      List<OLink> entityLinks,
      String entitySetName,
      String baseUri,
      String updated,
      EdmEntitySet ees,
      boolean isResponse) {

    String relid = null;
    String absid = null;
    if (isResponse) {
      relid = InternalUtil.getEntityRelId(oe);
      absid = baseUri + relid;
      writeElement(writer, "id", absid);
    }

    writeElement(writer, "title", null, "type", "text");
    writeElement(writer, "updated", updated);

    writer.startElement("author");
    writeElement(writer, "name", null);
    writer.endElement("author");

    if (isResponse) {
      writeElement(writer, "link", null, "rel", "edit", "title", entitySetName, "href", relid);
    }

    if (isResponse) {
      // for responses we need to include all links whether inlined or not
      for (EdmNavigationProperty np : ees.type.getAllNavigationProperties()) {
        if (!np.selected) {
          continue;
        }

        String otherEntity = np.name;
        String rel = related + otherEntity;
        String type = atom_feed_content_type;
        if (np.toRole.multiplicity != EdmMultiplicity.MANY) {
          type = atom_entry_content_type;
        }
        final String title = otherEntity;
        String href = relid + "/" + otherEntity;

        // check whether we have to write inlined entities
        OLink linkToInline =
            entityLinks != null
                ? Enumerable.create(entityLinks)
                    .firstOrNull(
                        new Predicate1<OLink>() {
                          @Override
                          public boolean apply(OLink input) {
                            return title.equals(input.getTitle());
                          }
                        })
                : null;

        if (linkToInline == null) {
          writeElement(
              writer, "link", null, "rel", rel, "type", type, "title", title, "href", href);
        } else {
          writer.startElement("link");
          writer.writeAttribute("rel", rel);
          writer.writeAttribute("type", type);
          writer.writeAttribute("title", title);
          writer.writeAttribute("href", href);
          // write the inlined entities inside the link element
          writeLinkInline(writer, linkToInline, href, baseUri, updated, isResponse);
          writer.endElement("link");
        }
      }

      writeElement(
          writer, "category", null, "term", ees.type.getFQNamespaceName(), "scheme", scheme);
    } else {
      // for requests we include only the provided links
      if (entityLinks != null) {
        for (OLink olink : entityLinks) {
          String type =
              olink instanceof ORelatedEntitiesLink
                  ? atom_feed_content_type
                  : atom_entry_content_type;

          writer.startElement("link");
          writer.writeAttribute("rel", olink.getRelation());
          writer.writeAttribute("type", type);
          writer.writeAttribute("title", olink.getTitle());
          writer.writeAttribute("href", olink.getHref());
          if (olink instanceof ORelatedEntitiesLinkInline
              || olink instanceof ORelatedEntityLinkInline) {
            // write the inlined entities inside the link element
            writeLinkInline(writer, olink, olink.getHref(), baseUri, updated, isResponse);
          }
          writer.endElement("link");
        }
      }
    }

    writer.startElement("content");
    writer.writeAttribute("type", MediaType.APPLICATION_XML);

    writer.startElement(new QName2(m, "properties", "m"));

    writeProperties(writer, entityProperties);

    writer.endElement("properties");
    writer.endElement("content");
    return absid;
  }