001package org.hl7.fhir.convertors.txClient;
002
003import java.net.URISyntaxException;
004import java.util.EnumSet;
005import java.util.Map;
006
007/*
008  Copyright (c) 2011+, HL7, Inc.
009  All rights reserved.
010  
011  Redistribution and use in source and binary forms, with or without modification, 
012  are permitted provided that the following conditions are met:
013    
014   * Redistributions of source code must retain the above copyright notice, this 
015     list of conditions and the following disclaimer.
016   * Redistributions in binary form must reproduce the above copyright notice, 
017     this list of conditions and the following disclaimer in the documentation 
018     and/or other materials provided with the distribution.
019   * Neither the name of HL7 nor the names of its contributors may be used to 
020     endorse or promote products derived from this software without specific 
021     prior written permission.
022  
023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
026  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
027  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
028  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
029  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
030  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
031  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
032  POSSIBILITY OF SUCH DAMAGE.
033  
034 */
035
036
037import org.hl7.fhir.exceptions.FHIRException;
038import org.hl7.fhir.r5.model.Bundle;
039import org.hl7.fhir.r5.model.CanonicalResource;
040import org.hl7.fhir.r5.model.CapabilityStatement;
041import org.hl7.fhir.r5.model.CodeSystem;
042import org.hl7.fhir.r5.model.Parameters;
043import org.hl7.fhir.r5.model.Resource;
044import org.hl7.fhir.r5.model.TerminologyCapabilities;
045import org.hl7.fhir.r5.model.ValueSet;
046import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
047import org.hl7.fhir.r5.utils.client.FHIRToolingClient;
048import org.hl7.fhir.r5.utils.client.network.ClientHeaders;
049import org.hl7.fhir.utilities.FhirPublication;
050import org.hl7.fhir.utilities.ToolingClientLogger;
051import org.hl7.fhir.utilities.Utilities;
052
053public class TerminologyClientR5 implements ITerminologyClient {
054
055  private final FHIRToolingClient client;
056  private ClientHeaders clientHeaders;
057  private String id;
058
059  public TerminologyClientR5(String id, String address, String userAgent) throws URISyntaxException {
060    this.client = new FHIRToolingClient(address, userAgent);
061    setClientHeaders(new ClientHeaders());
062    this.id = id;
063  }
064
065  @Override
066  public String getId() {
067    return id;
068  }
069
070  public TerminologyClientR5(String id, String address, String userAgent, ClientHeaders clientHeaders) throws URISyntaxException {
071    this.client = new FHIRToolingClient(address, userAgent);
072    setClientHeaders(clientHeaders);
073    this.id = id;
074  }
075
076  public EnumSet<FhirPublication> supportableVersions() {
077    // todo
078    return EnumSet.range(FhirPublication.STU3, FhirPublication.R5);
079  }
080  
081  public void setAllowedVersions(EnumSet<FhirPublication> versions) {
082    // todo
083  }
084  
085  public EnumSet<FhirPublication> getAllowedVersions() {
086    return null; // todo
087  }
088  
089  public FhirPublication getActualVersion() {
090    return FhirPublication.STU3;
091  }
092  
093  
094  @Override
095  public TerminologyCapabilities getTerminologyCapabilities() {
096    return client.getTerminologyCapabilities();
097  }
098
099  @Override
100  public String getAddress() {
101    return client.getAddress();
102  }
103
104  @Override
105  public ValueSet expandValueset(ValueSet vs, Parameters p, Map<String, String> params) {
106    return client.expandValueset(vs, p, params);
107  }
108
109  @Override
110  public Parameters validateCS(Parameters pin) {
111    return client.operateType(CodeSystem.class, "validate-code", pin);
112  }
113
114  @Override
115  public Parameters validateVS(Parameters pin) {
116    return client.operateType(ValueSet.class, "validate-code", pin);
117  }
118
119  @Override
120  public ITerminologyClient setTimeout(int i) {
121    client.setTimeout(i);
122    return this;
123  }
124
125  @Override
126  public ITerminologyClient setLogger(ToolingClientLogger txLog) {
127    client.setLogger(txLog);
128    return this;
129  }
130
131  @Override
132  public CapabilityStatement getCapabilitiesStatementQuick() {
133    return client.getCapabilitiesStatementQuick();
134  }
135
136  @Override
137  public Parameters lookupCode(Map<String, String> params) {
138    return client.lookupCode(params);
139  }
140
141  @Override
142  public ITerminologyClient setRetryCount(int retryCount) throws FHIRException {
143    client.setRetryCount(retryCount);
144    return this;
145  }
146
147  @Override
148  public int getRetryCount() throws FHIRException {
149    return client.getRetryCount();
150  }
151
152  @Override
153  public Bundle validateBatch(Bundle batch) {
154    return client.transaction(batch);
155  }
156
157  @Override
158  public CanonicalResource read(String type, String id) {
159    Class<Resource> t;
160    try {
161      t = (Class<Resource>) Class.forName("org.hl7.fhir.r5.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited...
162    } catch (ClassNotFoundException e) {
163      throw new FHIRException("Unable to fetch resources of type " + type + " in R5");
164    }
165    org.hl7.fhir.r5.model.Resource r5 = client.read(t, id);
166    if (r5 == null) {
167      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
168    }
169    if (!(r5 instanceof CanonicalResource)) {
170      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
171    }
172    return (CanonicalResource) r5;
173  }
174
175  @Override
176  public ClientHeaders getClientHeaders() {
177    return clientHeaders;
178  }
179
180  @Override
181  public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
182    this.clientHeaders = clientHeaders;
183    this.client.setClientHeaders(this.clientHeaders.headers());
184    return this;
185  }
186
187  @Override
188  public ITerminologyClient setUserAgent(String userAgent) {
189    client.setUserAgent(userAgent);
190    return this;
191  }
192
193  @Override
194  public String getUserAgent() {
195    return client.getUserAgent();
196  }
197
198  @Override
199  public String getServerVersion() {
200    return client.getServerVersion();
201  }
202
203  @Override
204  public ITerminologyClient setLanguage(String lang) {
205    client.setLanguage(lang);
206    return this;
207  }
208}