001package org.hl7.fhir.r4.elementmodel;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032import java.io.ByteArrayInputStream;
033import java.io.ByteArrayOutputStream;
034import java.io.IOException;
035import java.util.ArrayList;
036import java.util.List;
037
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.r4.conformance.ProfileUtilities;
040import org.hl7.fhir.r4.context.IWorkerContext;
041import org.hl7.fhir.r4.formats.IParser.OutputStyle;
042import org.hl7.fhir.r4.model.Base;
043import org.hl7.fhir.r4.model.CodeableConcept;
044import org.hl7.fhir.r4.model.Coding;
045import org.hl7.fhir.r4.model.ElementDefinition;
046import org.hl7.fhir.r4.model.Factory;
047import org.hl7.fhir.r4.model.Identifier;
048import org.hl7.fhir.r4.model.PrimitiveType;
049import org.hl7.fhir.r4.model.Reference;
050import org.hl7.fhir.r4.model.Resource;
051import org.hl7.fhir.r4.model.StructureDefinition;
052import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind;
053import org.hl7.fhir.r4.model.Type;
054
055@Deprecated
056public class ObjectConverter {
057
058  private IWorkerContext context;
059
060  public ObjectConverter(IWorkerContext context) {
061    this.context = context;
062  }
063
064  public Element convert(Resource ig) throws IOException, FHIRException {
065    if (ig == null)
066      return null;
067    ByteArrayOutputStream bs = new ByteArrayOutputStream();
068    org.hl7.fhir.r4.formats.JsonParser jp = new org.hl7.fhir.r4.formats.JsonParser();
069    jp.compose(bs, ig);
070    ByteArrayInputStream bi = new ByteArrayInputStream(bs.toByteArray());
071    return new JsonParser(context).parse(bi);
072  }
073
074  public Element convert(Property property, Type type) throws FHIRException {
075    return convertElement(property, type);
076  }
077
078  private Element convertElement(Property property, Base base) throws FHIRException {
079    if (base == null)
080      return null;
081    String tn = base.fhirType();
082    StructureDefinition sd = context.fetchResource(StructureDefinition.class,
083        ProfileUtilities.sdNs(tn, context.getOverrideVersionNs()));
084    if (sd == null)
085      throw new FHIRException("Unable to find definition for type " + tn);
086    Element res = new Element(property.getName(), property);
087    if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
088      res.setValue(((PrimitiveType) base).asStringValue());
089
090    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
091    for (ElementDefinition child : children) {
092      String n = tail(child.getPath());
093      if (sd.getKind() != StructureDefinitionKind.PRIMITIVETYPE || !"value".equals(n)) {
094        Base[] values = base.getProperty(n.hashCode(), n, false);
095        if (values != null)
096          for (Base value : values) {
097            res.getChildren().add(convertElement(new Property(context, child, sd), value));
098          }
099      }
100    }
101    return res;
102  }
103
104  private String tail(String path) {
105    if (path.contains("."))
106      return path.substring(path.lastIndexOf('.') + 1);
107    else
108      return path;
109  }
110
111  public Type convertToType(Element element) throws FHIRException {
112    Type b = new Factory().create(element.fhirType());
113    if (b instanceof PrimitiveType) {
114      ((PrimitiveType) b).setValueAsString(element.primitiveValue());
115    } else {
116      for (Element child : element.getChildren()) {
117        b.setProperty(child.getName(), convertToType(child));
118      }
119    }
120    return b;
121  }
122
123  public Resource convert(Element element) throws FHIRException {
124    ByteArrayOutputStream bo = new ByteArrayOutputStream();
125    try {
126      new JsonParser(context).compose(element, bo, OutputStyle.NORMAL, null);
127//      FileUtilities.bytesToFile(bo.toByteArray(), Utilities.path("[tmp]", "json.json");
128      return new org.hl7.fhir.r4.formats.JsonParser().parse(bo.toByteArray());
129    } catch (IOException e) {
130      // won't happen
131      throw new FHIRException(e);
132    }
133
134  }
135
136  public static CodeableConcept readAsCodeableConcept(Element element) {
137    CodeableConcept cc = new CodeableConcept();
138    List<Element> list = new ArrayList<Element>();
139    element.getNamedChildren("coding", list);
140    for (Element item : list)
141      cc.addCoding(readAsCoding(item));
142    cc.setText(element.getNamedChildValue("text"));
143    return cc;
144  }
145
146  public static Coding readAsCoding(Element item) {
147    Coding c = new Coding();
148    c.setSystem(item.getNamedChildValue("system"));
149    c.setVersion(item.getNamedChildValue("version"));
150    c.setCode(item.getNamedChildValue("code"));
151    c.setDisplay(item.getNamedChildValue("display"));
152    return c;
153  }
154
155  public static Identifier readAsIdentifier(Element item) {
156    Identifier r = new Identifier();
157    r.setSystem(item.getNamedChildValue("system"));
158    r.setValue(item.getNamedChildValue("value"));
159    return r;
160  }
161
162  public static Reference readAsReference(Element item) {
163    Reference r = new Reference();
164    r.setDisplay(item.getNamedChildValue("display"));
165    r.setReference(item.getNamedChildValue("reference"));
166    r.setType(item.getNamedChildValue("type"));
167    List<Element> identifier = item.getChildrenByName("identifier");
168    if (identifier.isEmpty() == false) {
169      r.setIdentifier(readAsIdentifier(identifier.get(0)));
170    }
171    return r;
172  }
173
174}