001package org.hl7.fhir.validation;
002
003import org.hl7.fhir.convertors.conv10_30.VersionConvertor_10_30;
004import org.hl7.fhir.convertors.conv10_40.VersionConvertor_10_40;
005import org.hl7.fhir.convertors.conv14_30.VersionConvertor_14_30;
006import org.hl7.fhir.convertors.conv14_40.VersionConvertor_14_40;
007import org.hl7.fhir.convertors.conv30_40.VersionConvertor_30_40;
008import org.hl7.fhir.convertors.factory.*;
009import org.hl7.fhir.dstu2016may.model.Resource;
010import org.hl7.fhir.exceptions.FHIRException;
011import org.hl7.fhir.r5.elementmodel.Manager;
012import org.hl7.fhir.utilities.VersionUtilities;
013
014import java.io.ByteArrayOutputStream;
015import java.io.IOException;
016
017public class VersionConvertor {
018
019  public static byte[] convertVersionNativeR2(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
020    org.hl7.fhir.dstu2.model.Resource r2;
021    switch (cnt.cntType) {
022      case JSON:
023        r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt.focus);
024        break;
025      case XML:
026        r2 = new org.hl7.fhir.dstu2.formats.XmlParser().parse(cnt.focus);
027        break;
028      default:
029        throw new FHIRException("Unsupported input format: " + cnt.cntType.toString());
030    }
031    if (VersionUtilities.isR2Ver(targetVer)) {
032      return getBytesDstu2(cnt, format, r2);
033    } else if (VersionUtilities.isR2BVer(targetVer)) {
034      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2);
035      org.hl7.fhir.dstu2016may.model.Resource r2b = VersionConvertorFactory_14_30.convertResource(r3);
036      return getBytesDstu2016(cnt, format, r2b);
037    } else if (VersionUtilities.isR3Ver(targetVer)) {
038      return getBytesDstu3(cnt, format, VersionConvertorFactory_10_30.convertResource(r2));
039    } else if (VersionUtilities.isR4Ver(targetVer)) {
040      return getBytesR4(cnt, format, VersionConvertorFactory_10_40.convertResource(r2));
041    } else {
042      throw new FHIRException("Target Version not supported yet: " + targetVer);
043    }
044  }
045
046  public static byte[] convertVersionNativeR2b(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
047    org.hl7.fhir.dstu2016may.model.Resource r2b;
048    switch (cnt.cntType) {
049      case JSON:
050        r2b = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt.focus);
051        break;
052      case XML:
053        r2b = new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(cnt.focus);
054        break;
055      default:
056        throw new FHIRException("Unsupported input format: " + cnt.cntType.toString());
057    }
058    if (VersionUtilities.isR2Ver(targetVer)) {
059      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_14_30.convertResource(r2b);
060      org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_30.convertResource(r3);
061      return getBytesDstu2(cnt, format, r2);
062    } else if (VersionUtilities.isR2BVer(targetVer)) {
063      return getBytesDstu2016(cnt, format, r2b);
064    } else if (VersionUtilities.isR3Ver(targetVer)) {
065      return getBytesDstu3(cnt, format, VersionConvertorFactory_14_30.convertResource(r2b));
066    } else if (VersionUtilities.isR4Ver(targetVer)) {
067      return getBytesR4(cnt, format, VersionConvertorFactory_14_40.convertResource(r2b));
068    } else {
069      throw new FHIRException("Target Version not supported yet: " + targetVer);
070    }
071  }
072
073  public static byte[] convertVersionNativeR3(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
074    org.hl7.fhir.dstu3.model.Resource r3;
075    switch (cnt.cntType) {
076      case JSON:
077        r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt.focus);
078        break;
079      case XML:
080        r3 = new org.hl7.fhir.dstu3.formats.XmlParser().parse(cnt.focus);
081        break;
082      default:
083        throw new FHIRException("Unsupported input format: " + cnt.cntType.toString());
084    }
085    if (VersionUtilities.isR2Ver(targetVer)) {
086      return getBytesDstu2(cnt, format, VersionConvertorFactory_10_30.convertResource(r3));
087    } else if (VersionUtilities.isR2BVer(targetVer)) {
088      return getBytesDstu2016(cnt, format, VersionConvertorFactory_14_30.convertResource(r3));
089    } else if (VersionUtilities.isR3Ver(targetVer)) {
090      return getBytesDstu3(cnt, format, r3);
091    } else if (VersionUtilities.isR4Ver(targetVer)) {
092      return getBytesR4(cnt, format, VersionConvertorFactory_30_40.convertResource(r3));
093    } else {
094      throw new FHIRException("Target Version not supported yet: " + targetVer);
095    }
096  }
097
098  public static byte[] convertVersionNativeR4(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
099    org.hl7.fhir.r4.model.Resource r4;
100    switch (cnt.cntType) {
101      case JSON:
102        r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt.focus);
103        break;
104      case XML:
105        r4 = new org.hl7.fhir.r4.formats.XmlParser().parse(cnt.focus);
106        break;
107      default:
108        throw new FHIRException("Unsupported input format: " + cnt.cntType.toString());
109    }
110    if (VersionUtilities.isR2Ver(targetVer)) {
111      return getBytesDstu2(cnt, format, VersionConvertorFactory_10_40.convertResource(r4));
112    } else if (VersionUtilities.isR2BVer(targetVer)) {
113      return getBytesDstu2016(cnt, format, VersionConvertorFactory_14_40.convertResource(r4));
114    } else if (VersionUtilities.isR3Ver(targetVer)) {
115      return getBytesDstu3(cnt, format, VersionConvertorFactory_30_40.convertResource(r4));
116    } else if (VersionUtilities.isR4Ver(targetVer)) {
117      return getBytesR4(cnt, format, r4);
118    } else {
119      throw new FHIRException("Target Version not supported yet: " + targetVer);
120    }
121  }
122
123  private static byte[] getBytesDstu2(Content cnt, Manager.FhirFormat format, org.hl7.fhir.dstu2.model.Resource r2) throws IOException {
124    ByteArrayOutputStream bs = new ByteArrayOutputStream();
125    switch (format) {
126      case JSON:
127        new org.hl7.fhir.dstu2.formats.JsonParser().compose(bs, r2);
128        return bs.toByteArray();
129      case XML:
130        new org.hl7.fhir.dstu2.formats.XmlParser().compose(bs, r2);
131        return bs.toByteArray();
132      default:
133        throw new FHIRException("Unsupported output format: " + cnt.cntType.toString());
134    }
135  }
136
137  private static byte[] getBytesDstu2016(Content cnt, Manager.FhirFormat format, Resource r2b) throws IOException {
138    ByteArrayOutputStream bs = new ByteArrayOutputStream();
139    switch (format) {
140      case JSON:
141        new org.hl7.fhir.dstu2016may.formats.JsonParser().compose(bs, r2b);
142        return bs.toByteArray();
143      case XML:
144        new org.hl7.fhir.dstu2016may.formats.XmlParser().compose(bs, r2b);
145        return bs.toByteArray();
146      default:
147        throw new FHIRException("Unsupported output format: " + cnt.cntType.toString());
148    }
149  }
150
151  private static byte[] getBytesDstu3(Content cnt, Manager.FhirFormat format, org.hl7.fhir.dstu3.model.Resource r3) throws IOException {
152    ByteArrayOutputStream bs = new ByteArrayOutputStream();
153    switch (format) {
154      case JSON:
155        new org.hl7.fhir.dstu3.formats.JsonParser().compose(bs, r3);
156        return bs.toByteArray();
157      case XML:
158        new org.hl7.fhir.dstu3.formats.XmlParser().compose(bs, r3);
159        return bs.toByteArray();
160      default:
161        throw new FHIRException("Unsupported output format: " + cnt.cntType.toString());
162    }
163  }
164
165  private static byte[] getBytesR4(Content cnt, Manager.FhirFormat format, org.hl7.fhir.r4.model.Resource r4) throws IOException {
166    ByteArrayOutputStream bs = new ByteArrayOutputStream();
167    switch (format) {
168      case JSON:
169        new org.hl7.fhir.r4.formats.JsonParser().compose(bs, r4);
170        return bs.toByteArray();
171      case XML:
172        new org.hl7.fhir.r4.formats.XmlParser().compose(bs, r4);
173        return bs.toByteArray();
174      default:
175        throw new FHIRException("Unsupported output format: " + cnt.cntType.toString());
176    }
177  }
178}