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