Ejemplo n.º 1
0
  private void encodeCompositeElementChildrenToStreamWriter(
      IBaseResource theResource,
      IBase theElement,
      XMLStreamWriter theEventWriter,
      List<? extends BaseRuntimeChildDefinition> children,
      boolean theIncludedResource)
      throws XMLStreamException, DataFormatException {
    for (BaseRuntimeChildDefinition nextChild : children) {
      if (nextChild.getElementName().equals("extension")
          || nextChild.getElementName().equals("modifierExtension")) {
        continue;
      }

      if (nextChild instanceof RuntimeChildNarrativeDefinition && !theIncludedResource) {
        INarrativeGenerator gen = myContext.getNarrativeGenerator();
        if (theResource instanceof IResource) {
          BaseNarrativeDt<?> narr = ((IResource) theResource).getText();
          if (gen != null && narr.isEmpty()) {
            String resourceProfile =
                myContext.getResourceDefinition(theResource).getResourceProfile();
            gen.generateNarrative(resourceProfile, theResource, narr);
          }
          if (narr.isEmpty() == false) {
            RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
            String childName = nextChild.getChildNameByDatatype(child.getDatatype());
            BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
            encodeChildElementToStreamWriter(
                theResource, theEventWriter, narr, childName, type, null, theIncludedResource);
            continue;
          }
        } else {
          INarrative narr1 = ((IDomainResource) theResource).getText();
          BaseNarrativeDt<?> narr2 = null;
          if (gen != null && narr1.isEmpty()) {
            // TODO: need to implement this
            String resourceProfile =
                myContext.getResourceDefinition(theResource).getResourceProfile();
            gen.generateNarrative(resourceProfile, theResource, null);
          }
          if (narr2 != null) {
            RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
            String childName = nextChild.getChildNameByDatatype(child.getDatatype());
            BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
            encodeChildElementToStreamWriter(
                theResource, theEventWriter, narr2, childName, type, null, theIncludedResource);
            continue;
          }
        }
      }

      List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
      if (values == null || values.isEmpty()) {
        continue;
      }

      for (IBase nextValue : values) {
        if ((nextValue == null || nextValue.isEmpty()) && !(nextValue instanceof BaseContainedDt)) {
          continue;
        }
        Class<? extends IBase> type = nextValue.getClass();
        String childName = nextChild.getChildNameByDatatype(type);
        String extensionUrl = nextChild.getExtensionUrl();
        BaseRuntimeElementDefinition<?> childDef =
            nextChild.getChildElementDefinitionByDatatype(type);
        if (childDef == null) {
          super.throwExceptionForUnknownChildType(nextChild, type);
        }

        if (nextValue instanceof IBaseExtension
            && myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) {
          // This is called for the Query resource in DSTU1 only
          extensionUrl = ((IBaseExtension<?>) nextValue).getUrl();
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              extensionUrl,
              theIncludedResource);

        } else if (extensionUrl != null && childName.equals("extension") == false) {
          RuntimeChildDeclaredExtensionDefinition extDef =
              (RuntimeChildDeclaredExtensionDefinition) nextChild;
          if (extDef.isModifier()) {
            theEventWriter.writeStartElement("modifierExtension");
          } else {
            theEventWriter.writeStartElement("extension");
          }

          theEventWriter.writeAttribute("url", extensionUrl);
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              null,
              theIncludedResource);
          theEventWriter.writeEndElement();
        } else if (nextChild instanceof RuntimeChildNarrativeDefinition && theIncludedResource) {
          // suppress narratives from contained resources
        } else {
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              extensionUrl,
              theIncludedResource);
        }
      }
    }
  }