예제 #1
0
  private void encodeBundleToWriterDstu2(Bundle theBundle, XMLStreamWriter theEventWriter)
      throws XMLStreamException {
    theEventWriter.writeStartElement("Bundle");
    theEventWriter.writeDefaultNamespace(FHIR_NS);

    writeOptionalTagWithValue(theEventWriter, "id", theBundle.getId().getIdPart());

    InstantDt updated =
        (InstantDt) theBundle.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
    IdDt bundleId = theBundle.getId();
    if (bundleId != null && isNotBlank(bundleId.getVersionIdPart())
        || (updated != null && !updated.isEmpty())) {
      theEventWriter.writeStartElement("meta");
      writeOptionalTagWithValue(theEventWriter, "versionId", bundleId.getVersionIdPart());
      if (updated != null) {
        writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
      }
      theEventWriter.writeEndElement();
    }

    String bundleBaseUrl = theBundle.getLinkBase().getValue();

    writeOptionalTagWithValue(theEventWriter, "type", theBundle.getType().getValue());
    writeOptionalTagWithValue(theEventWriter, "base", bundleBaseUrl);
    writeOptionalTagWithValue(
        theEventWriter, "total", theBundle.getTotalResults().getValueAsString());

    writeBundleResourceLink(theEventWriter, "first", theBundle.getLinkFirst());
    writeBundleResourceLink(theEventWriter, "previous", theBundle.getLinkPrevious());
    writeBundleResourceLink(theEventWriter, "next", theBundle.getLinkNext());
    writeBundleResourceLink(theEventWriter, "last", theBundle.getLinkLast());
    writeBundleResourceLink(theEventWriter, "self", theBundle.getLinkSelf());

    for (BundleEntry nextEntry : theBundle.getEntries()) {
      theEventWriter.writeStartElement("entry");

      boolean deleted = false;
      if (nextEntry.getDeletedAt() != null && nextEntry.getDeletedAt().isEmpty() == false) {
        deleted = true;
      }

      writeOptionalTagWithValue(
          theEventWriter, "base", determineResourceBaseUrl(bundleBaseUrl, nextEntry));

      IResource resource = nextEntry.getResource();
      if (resource != null && !resource.isEmpty() && !deleted) {
        theEventWriter.writeStartElement("resource");
        encodeResourceToXmlStreamWriter(resource, theEventWriter, false);
        theEventWriter.writeEndElement(); // content
      } else {
        ourLog.debug("Bundle entry contains null resource");
      }

      if (nextEntry.getSearchMode().isEmpty() == false || nextEntry.getScore().isEmpty() == false) {
        theEventWriter.writeStartElement("search");
        writeOptionalTagWithValue(
            theEventWriter, "mode", nextEntry.getSearchMode().getValueAsString());
        writeOptionalTagWithValue(theEventWriter, "score", nextEntry.getScore().getValueAsString());
        theEventWriter.writeEndElement();
        // IResource nextResource = nextEntry.getResource();
      }

      if (nextEntry.getTransactionMethod().isEmpty() == false
          || nextEntry.getLinkSearch().isEmpty() == false) {
        theEventWriter.writeStartElement("transaction");
        writeOptionalTagWithValue(
            theEventWriter, "method", nextEntry.getTransactionMethod().getValue());
        writeOptionalTagWithValue(theEventWriter, "url", nextEntry.getLinkSearch().getValue());
        theEventWriter.writeEndElement();
      }

      if (deleted) {
        theEventWriter.writeStartElement("deleted");
        writeOptionalTagWithValue(theEventWriter, "type", nextEntry.getId().getResourceType());
        writeOptionalTagWithValue(theEventWriter, "id", nextEntry.getId().getIdPart());
        writeOptionalTagWithValue(
            theEventWriter, "versionId", nextEntry.getId().getVersionIdPart());
        writeOptionalTagWithValue(
            theEventWriter, "instant", nextEntry.getDeletedAt().getValueAsString());
        theEventWriter.writeEndElement();
      }

      theEventWriter.writeEndElement(); // entry
    }

    theEventWriter.writeEndElement();
    theEventWriter.close();
  }
예제 #2
0
  private void encodeBundleToWriterDstu1(Bundle theBundle, XMLStreamWriter eventWriter)
      throws XMLStreamException {
    eventWriter.writeStartElement("feed");
    eventWriter.writeDefaultNamespace(ATOM_NS);

    writeTagWithTextNode(eventWriter, "title", theBundle.getTitle());
    writeTagWithTextNode(eventWriter, "id", theBundle.getBundleId());

    writeAtomLink(eventWriter, Constants.LINK_SELF, theBundle.getLinkSelf());
    writeAtomLink(eventWriter, Constants.LINK_FIRST, theBundle.getLinkFirst());
    writeAtomLink(eventWriter, Constants.LINK_PREVIOUS, theBundle.getLinkPrevious());
    writeAtomLink(eventWriter, Constants.LINK_NEXT, theBundle.getLinkNext());
    writeAtomLink(eventWriter, Constants.LINK_LAST, theBundle.getLinkLast());
    writeAtomLink(eventWriter, Constants.LINK_FHIR_BASE, theBundle.getLinkBase());

    if (theBundle.getTotalResults().getValue() != null) {
      eventWriter.writeStartElement("os", "totalResults", OPENSEARCH_NS);
      eventWriter.writeNamespace("os", OPENSEARCH_NS);
      eventWriter.writeCharacters(theBundle.getTotalResults().getValue().toString());
      eventWriter.writeEndElement();
    }

    writeOptionalTagWithTextNode(eventWriter, "updated", theBundle.getUpdated());

    if (StringUtils.isNotBlank(theBundle.getAuthorName().getValue())) {
      eventWriter.writeStartElement("author");
      writeTagWithTextNode(eventWriter, "name", theBundle.getAuthorName());
      writeOptionalTagWithTextNode(eventWriter, "uri", theBundle.getAuthorUri());
      eventWriter.writeEndElement();
    }

    writeCategories(eventWriter, theBundle.getCategories());

    for (BundleEntry nextEntry : theBundle.getEntries()) {
      boolean deleted = false;
      if (nextEntry.getDeletedAt() != null && nextEntry.getDeletedAt().isEmpty() == false) {
        deleted = true;
        eventWriter.writeStartElement("at", "deleted-entry", TOMBSTONES_NS);
        eventWriter.writeNamespace("at", TOMBSTONES_NS);

        if (nextEntry.getDeletedResourceId().isEmpty()) {
          writeOptionalAttribute(eventWriter, "ref", nextEntry.getId().getValueAsString());
        } else {
          writeOptionalAttribute(
              eventWriter, "ref", nextEntry.getDeletedResourceId().getValueAsString());
        }

        writeOptionalAttribute(eventWriter, "when", nextEntry.getDeletedAt().getValueAsString());
        if (nextEntry.getDeletedByEmail().isEmpty() == false
            || nextEntry.getDeletedByName().isEmpty() == false) {
          eventWriter.writeStartElement(TOMBSTONES_NS, "by");
          if (nextEntry.getDeletedByName().isEmpty() == false) {
            eventWriter.writeStartElement(TOMBSTONES_NS, "name");
            eventWriter.writeCharacters(nextEntry.getDeletedByName().getValue());
            eventWriter.writeEndElement();
          }
          if (nextEntry.getDeletedByEmail().isEmpty() == false) {
            eventWriter.writeStartElement(TOMBSTONES_NS, "email");
            eventWriter.writeCharacters(nextEntry.getDeletedByEmail().getValue());
            eventWriter.writeEndElement();
          }
          eventWriter.writeEndElement();
        }
        if (nextEntry.getDeletedComment().isEmpty() == false) {
          eventWriter.writeStartElement(TOMBSTONES_NS, "comment");
          eventWriter.writeCharacters(nextEntry.getDeletedComment().getValue());
          eventWriter.writeEndElement();
        }
      } else {
        eventWriter.writeStartElement("entry");
      }

      writeOptionalTagWithTextNode(eventWriter, "title", nextEntry.getTitle());
      if (!deleted) {
        if (nextEntry.getId().isEmpty() == false) {
          writeTagWithTextNode(eventWriter, "id", nextEntry.getId());
        } else {
          writeTagWithTextNode(eventWriter, "id", nextEntry.getResource().getId());
        }
      }
      writeOptionalTagWithTextNode(eventWriter, "updated", nextEntry.getUpdated());
      writeOptionalTagWithTextNode(eventWriter, "published", nextEntry.getPublished());

      writeCategories(eventWriter, nextEntry.getCategories());

      if (!nextEntry.getLinkSelf().isEmpty()) {
        writeAtomLink(eventWriter, "self", nextEntry.getLinkSelf());
      }

      if (!nextEntry.getLinkAlternate().isEmpty()) {
        writeAtomLink(eventWriter, "alternate", nextEntry.getLinkAlternate());
      }

      if (!nextEntry.getLinkSearch().isEmpty()) {
        writeAtomLink(eventWriter, "search", nextEntry.getLinkSearch());
      }

      IResource resource = nextEntry.getResource();
      if (resource != null && !resource.isEmpty() && !deleted) {
        eventWriter.writeStartElement("content");
        eventWriter.writeAttribute("type", "text/xml");
        encodeResourceToXmlStreamWriter(resource, eventWriter, false);
        eventWriter.writeEndElement(); // content
      } else {
        ourLog.debug("Bundle entry contains null resource");
      }

      if (!nextEntry.getSummary().isEmpty()) {
        eventWriter.writeStartElement("summary");
        eventWriter.writeAttribute("type", "xhtml");
        encodeXhtml(nextEntry.getSummary(), eventWriter);
        eventWriter.writeEndElement();
      }

      eventWriter.writeEndElement(); // entry
    }

    eventWriter.writeEndElement();
    eventWriter.close();
  }