001package org.hl7.fhir.convertors.txClient;
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
033import org.hl7.fhir.convertors.conv10_50.resources10_50.TerminologyCapabilities10_50;
034import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
035import org.hl7.fhir.dstu2.model.Resource;
036import org.hl7.fhir.dstu2.utils.client.FHIRToolingClient;
037import org.hl7.fhir.exceptions.FHIRException;
038import org.hl7.fhir.r5.model.*;
039import org.hl7.fhir.r5.terminologies.TerminologyClient;
040import org.hl7.fhir.r5.utils.client.network.ClientHeaders;
041import org.hl7.fhir.utilities.ToolingClientLogger;
042import org.hl7.fhir.utilities.Utilities;
043
044import java.net.URISyntaxException;
045import java.util.Map;
046
047public class TerminologyClientR2 implements TerminologyClient {
048
049  private final FHIRToolingClient client; // todo: use the R2 client
050
051  public TerminologyClientR2(String address) throws URISyntaxException {
052    client = new FHIRToolingClient(address);
053  }
054
055  @Override
056  public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException {
057    return TerminologyCapabilities10_50.convertTerminologyCapabilities(client.getTerminologyCapabilities());
058  }
059
060  @Override
061  public String getAddress() {
062    return client.getAddress();
063  }
064
065  @Override
066  public ValueSet expandValueset(ValueSet vs, Parameters p, Map<String, String> params) throws FHIRException {
067    org.hl7.fhir.dstu2.model.ValueSet vs2 = (org.hl7.fhir.dstu2.model.ValueSet) VersionConvertorFactory_10_50.convertResource(vs);
068    org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(p);
069    vs2 = client.expandValueset(vs2, p2, params);
070    return (ValueSet) VersionConvertorFactory_10_50.convertResource(vs2);
071  }
072
073  @Override
074  public Parameters validateCS(Parameters pin) throws FHIRException {
075    org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(pin);
076    p2 = client.operateType(org.hl7.fhir.dstu2.model.ValueSet.class, "validate-code", p2);
077    return (Parameters) VersionConvertorFactory_10_50.convertResource(p2);
078  }
079
080  @Override
081  public Parameters validateVS(Parameters pin) throws FHIRException {
082    org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(pin);
083    p2 = client.operateType(org.hl7.fhir.dstu2.model.ValueSet.class, "validate-code", p2);
084    return (Parameters) VersionConvertorFactory_10_50.convertResource(p2);
085  }
086
087  @Override
088  public TerminologyClient setTimeout(int i) {
089    client.setTimeout(i);
090    return this;
091  }
092
093  @Override
094  public TerminologyClient setLogger(ToolingClientLogger txLog) {
095    client.setLogger(txLog);
096    return this;
097  }
098
099  @Override
100  public TerminologyClient setRetryCount(int retryCount) throws FHIRException {
101    client.setRetryCount(retryCount);
102    return this;
103  }
104
105  @Override
106  public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException {
107    return (CapabilityStatement) VersionConvertorFactory_10_50.convertResource(client.getConformanceStatementQuick());
108  }
109
110  @Override
111  public Parameters lookupCode(Map<String, String> params) throws FHIRException {
112    return (Parameters) VersionConvertorFactory_10_50.convertResource(client.lookupCode(params));
113  }
114
115  @Override
116  public int getRetryCount() throws FHIRException {
117    return client.getRetryCount();
118  }
119
120  @Override
121  public Bundle validateBatch(Bundle batch) {
122    return (Bundle) VersionConvertorFactory_10_50.convertResource(client.transaction((org.hl7.fhir.dstu2.model.Bundle) VersionConvertorFactory_10_50.convertResource(batch)));
123  }
124
125  @Override
126  public CanonicalResource read(String type, String id) {
127    Class<Resource> t;
128    try {
129      t = (Class<Resource>) Class.forName("org.hl7.fhir.dstu2.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited...
130    } catch (ClassNotFoundException e) {
131      throw new FHIRException("Unable to fetch resources of type " + type + " in R2");
132    }
133    org.hl7.fhir.dstu2.model.Resource r2 = client.read(t, id);
134    if (r2 == null) {
135      throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id));
136    }
137    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2);
138    if (r5 != null) {
139      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
140    }
141    if (!(r5 instanceof CanonicalResource)) {
142      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
143    }
144    return (CanonicalResource) r5;
145  }
146
147  @Override
148  public ClientHeaders getClientHeaders() {
149    return null;
150  }
151
152  @Override
153  public TerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
154    return null;
155  }
156}