View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.fhir.support;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import org.hl7.fhir.dstu3.model.Bundle;
21  import org.hl7.fhir.dstu3.model.NamingSystem;
22  
23  import java.io.BufferedReader;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.net.URL;
28  
29  /**
30   * Default Implementation of a NamingSystem that loads and parses a Bundle of NamingSystem
31   * resources using a Reader or from a FHIR server.
32   *
33   * @author Christian Ohr
34   * @since 3.4
35   */
36  public class DefaultNamingSystemServiceImpl extends AbstractNamingSystemServiceImpl {
37  
38      private FhirContext fhirContext;
39  
40      public DefaultNamingSystemServiceImpl(FhirContext fhirContext) {
41          super();
42          this.fhirContext = fhirContext;
43      }
44  
45      public DefaultNamingSystemServiceImpl addNamingSystemsFromXml(Reader reader) {
46          addNamingSystems(fhirContext.newXmlParser().parseResource(Bundle.class, reader));
47          return this;
48      }
49  
50      public void setNamingSystemsFromXml(URL... urls) throws IOException {
51          for (URL url : urls) {
52              addNamingSystemsFromXml(new BufferedReader(new InputStreamReader(url.openStream())));
53          }
54      }
55  
56      public DefaultNamingSystemServiceImpl addNamingSystemsFromJson(Reader reader) {
57          addNamingSystems(fhirContext.newJsonParser().parseResource(Bundle.class, reader));
58          return this;
59      }
60  
61      public void setNamingSystemsFromJson(URL... urls) throws IOException {
62          for (URL url : urls) {
63              addNamingSystemsFromJson(new BufferedReader(new InputStreamReader(url.openStream())));
64          }
65      }
66  
67      public DefaultNamingSystemServiceImpl addNamingSystemsFromFhirServer(String url) {
68          addNamingSystems(fhirContext.newRestfulGenericClient(url)
69                  .search()
70                  .forResource(NamingSystem.class)
71                  .returnBundle(Bundle.class)
72                  .execute());
73          return this;
74      }
75  
76  }