Exemplo n.º 1
0
 private void writeOptionalTagWithTextNode(
     XMLStreamWriter theEventWriter, String theTagName, InstantDt theInstantDt)
     throws XMLStreamException {
   if (theInstantDt.getValue() != null) {
     theEventWriter.writeStartElement(theTagName);
     theEventWriter.writeCharacters(theInstantDt.getValueAsString());
     theEventWriter.writeEndElement();
   }
 }
Exemplo n.º 2
0
  private void encodeResourceToXmlStreamWriter(
      IBaseResource theResource,
      XMLStreamWriter theEventWriter,
      boolean theContainedResource,
      String theResourceId)
      throws XMLStreamException {
    if (!theContainedResource) {
      super.containResourcesForEncoding(theResource);
    }

    RuntimeResourceDefinition resDef = myContext.getResourceDefinition(theResource);
    if (resDef == null) {
      throw new ConfigurationException("Unknown resource type: " + theResource.getClass());
    }

    theEventWriter.writeStartElement(resDef.getName());
    theEventWriter.writeDefaultNamespace(FHIR_NS);

    if (theResource instanceof IAnyResource) {

      // HL7.org Structures
      encodeCompositeElementToStreamWriter(
          theResource, theResource, theEventWriter, resDef, theContainedResource);

    } else {

      if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU1)) {

        // DSTU2+

        IResource resource = (IResource) theResource;
        writeOptionalTagWithValue(theEventWriter, "id", theResourceId);

        InstantDt updated =
            (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
        IdDt resourceId = resource.getId();
        String versionIdPart = resourceId.getVersionIdPart();
        if (isBlank(versionIdPart)) {
          versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
        }
        List<BaseCodingDt> securityLabels =
            extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS);
        List<IdDt> profiles =
            extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES);
        TagList tags = ResourceMetadataKeyEnum.TAG_LIST.get(resource);
        if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, profiles) == false) {
          theEventWriter.writeStartElement("meta");
          writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart);
          if (updated != null) {
            writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
          }

          for (IdDt profile : profiles) {
            theEventWriter.writeStartElement("profile");
            theEventWriter.writeAttribute("value", profile.getValue());
            theEventWriter.writeEndElement();
          }
          for (BaseCodingDt securityLabel : securityLabels) {
            theEventWriter.writeStartElement("security");
            BaseRuntimeElementCompositeDefinition<?> def =
                (BaseRuntimeElementCompositeDefinition<?>)
                    myContext.getElementDefinition(securityLabel.getClass());
            encodeCompositeElementChildrenToStreamWriter(
                resource, securityLabel, theEventWriter, def.getChildren(), theContainedResource);
            theEventWriter.writeEndElement();
          }
          if (tags != null) {
            for (Tag tag : tags) {
              theEventWriter.writeStartElement("tag");
              writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme());
              writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm());
              writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel());
              theEventWriter.writeEndElement();
            }
          }
          theEventWriter.writeEndElement();
        }

        if (theResource instanceof IBaseBinary) {
          IBaseBinary bin = (IBaseBinary) theResource;
          writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType());
          writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64());
        } else {
          encodeResourceToStreamWriterInDstu2Format(
              resDef, theResource, theResource, theEventWriter, resDef, theContainedResource);
        }

      } else {

        // DSTU1
        if (theResourceId != null && theContainedResource) {
          theEventWriter.writeAttribute("id", theResourceId);
        }

        if (theResource instanceof IBaseBinary) {
          IBaseBinary bin = (IBaseBinary) theResource;
          if (bin.getContentType() != null) {
            theEventWriter.writeAttribute("contentType", bin.getContentType());
          }
          theEventWriter.writeCharacters(bin.getContentAsBase64());
        } else {
          encodeCompositeElementToStreamWriter(
              theResource, theResource, theEventWriter, resDef, theContainedResource);
        }
      }
    }

    theEventWriter.writeEndElement();
  }
Exemplo n.º 3
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();
  }