001package org.hl7.fhir.dstu3.elementmodel;
002
003import java.io.ByteArrayInputStream;
004import java.io.ByteArrayOutputStream;
005import java.io.IOException;
006import java.util.ArrayList;
007import java.util.List;
008
009import org.hl7.fhir.dstu3.conformance.ProfileUtilities;
010import org.hl7.fhir.dstu3.context.IWorkerContext;
011import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
012import org.hl7.fhir.dstu3.model.Base;
013import org.hl7.fhir.dstu3.model.CodeableConcept;
014import org.hl7.fhir.dstu3.model.Coding;
015import org.hl7.fhir.dstu3.model.ElementDefinition;
016import org.hl7.fhir.dstu3.model.Factory;
017import org.hl7.fhir.dstu3.model.Identifier;
018import org.hl7.fhir.dstu3.model.PrimitiveType;
019import org.hl7.fhir.dstu3.model.Reference;
020import org.hl7.fhir.dstu3.model.Resource;
021import org.hl7.fhir.dstu3.model.StructureDefinition;
022import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind;
023import org.hl7.fhir.dstu3.model.Type;
024import org.hl7.fhir.exceptions.DefinitionException;
025import org.hl7.fhir.exceptions.FHIRException;
026import org.hl7.fhir.exceptions.FHIRFormatError;
027
028
029
030public class ObjectConverter  {
031
032  private IWorkerContext context;
033
034  public ObjectConverter(IWorkerContext context) {
035    this.context = context;
036  }
037
038  public Element convert(Resource ig) throws IOException, FHIRFormatError, DefinitionException {
039    if (ig == null)
040      return null;
041    ByteArrayOutputStream bs = new ByteArrayOutputStream();
042    org.hl7.fhir.dstu3.formats.JsonParser jp = new org.hl7.fhir.dstu3.formats.JsonParser();
043    jp.compose(bs, ig);
044    ByteArrayInputStream bi = new ByteArrayInputStream(bs.toByteArray());
045    return new JsonParser(context).parse(bi);
046  }
047
048  public Element convert(Property property, Type type) throws FHIRException {
049    return convertElement(property, type);
050  }
051  
052  private Element convertElement(Property property, Base base) throws FHIRException {
053    if (base == null)
054      return null;
055    String tn = base.fhirType();
056    StructureDefinition sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+tn);
057    if (sd == null)
058      throw new FHIRException("Unable to find definition for type "+tn);
059    Element res = new Element(property.getName(), property);
060    if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 
061      res.setValue(((PrimitiveType) base).asStringValue());
062
063    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep()); 
064    for (ElementDefinition child : children) {
065      String n = tail(child.getPath());
066      if (sd.getKind() != StructureDefinitionKind.PRIMITIVETYPE || !"value".equals(n)) {
067        Base[] values = base.getProperty(n.hashCode(), n, false);
068        if (values != null)
069          for (Base value : values) {
070            res.getChildren().add(convertElement(new Property(context, child, sd), value));
071          }
072      }
073    }
074    return res;
075  }
076
077  private String tail(String path) {
078    if (path.contains("."))
079      return path.substring(path.lastIndexOf('.')+1);
080    else
081      return path;
082  }
083
084  public Type convertToType(Element element) throws FHIRException {
085    Type b = new Factory().create(element.fhirType());
086    if (b instanceof PrimitiveType) {
087      ((PrimitiveType) b).setValueAsString(element.primitiveValue());
088    } else {
089      for (Element child : element.getChildren()) {
090        b.setProperty(child.getName(), convertToType(child));
091      }
092    }
093    return b;
094  }
095
096  public Resource convert(Element element) throws FHIRException {
097    ByteArrayOutputStream bo = new ByteArrayOutputStream();
098    try {
099      new JsonParser(context).compose(element, bo, OutputStyle.NORMAL, null);
100      return new org.hl7.fhir.dstu3.formats.JsonParser().parse(bo.toByteArray());
101    } catch (IOException e) {
102      // won't happen
103      throw new FHIRException(e);
104    }
105    
106  }
107
108  public static CodeableConcept readAsCodeableConcept(Element element) {
109    CodeableConcept cc = new CodeableConcept();
110    List<Element> list = new ArrayList<Element>();
111    element.getNamedChildren("coding", list);
112    for (Element item : list)
113      cc.addCoding(readAsCoding(item));
114    cc.setText(element.getNamedChildValue("text"));
115    return cc;
116  }
117
118  public static Coding readAsCoding(Element item) {
119    Coding c = new Coding();
120    c.setSystem(item.getNamedChildValue("system"));
121    c.setVersion(item.getNamedChildValue("version"));
122    c.setCode(item.getNamedChildValue("code"));
123    c.setDisplay(item.getNamedChildValue("display"));
124    return c;
125  }
126
127  public static Identifier readAsIdentifier(Element item) {
128    Identifier r = new Identifier();
129    r.setSystem(item.getNamedChildValue("system"));
130    r.setValue(item.getNamedChildValue("value"));
131    return r;
132  }
133
134  public static Reference readAsReference(Element item) {
135    Reference r = new Reference();
136    r.setDisplay(item.getNamedChildValue("display"));
137    r.setReference(item.getNamedChildValue("reference"));
138    List<Element> identifier = item.getChildrenByName("identifier");
139    if (identifier.isEmpty() == false) {
140      r.setIdentifier(readAsIdentifier(identifier.get(0)));
141    }
142    return r;
143  }
144
145}