001package org.hl7.fhir.r5.terminologies;
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
031import org.hl7.fhir.exceptions.FHIRException;
032import org.hl7.fhir.r5.context.IWorkerContext;
033import org.hl7.fhir.r5.model.CodeType;
034import org.hl7.fhir.r5.model.Enumerations;
035import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
036import org.hl7.fhir.r5.model.Parameters;
037import org.hl7.fhir.r5.model.ValueSet;
038import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
039import org.hl7.fhir.utilities.Utilities;
040
041import java.util.List;
042
043@MarkedToMoveToAdjunctPackage
044public class ImplicitValueSets {
045
046  private Parameters expParameters;
047
048  public ImplicitValueSets(Parameters expParameters) {
049    this.expParameters = expParameters;
050  }
051
052  public ValueSet generateImplicitValueSet(String url) {
053    if (Utilities.noString(url)) {
054      return null;
055    }
056    
057    if (url.startsWith("http://snomed.info/sct")) {
058      return generateImplicitSnomedValueSet(url, defaultVersion("http://snomed.info/sct"));
059    }
060    if (url.startsWith("http://loinc.org/vs")) {
061      return generateImplicitLoincValueSet(url, defaultVersion("http://loinc.org"));
062    }
063    if (url.equals("http://unitsofmeasure.org/vs")) {
064      return allUcumValueSet();
065    }
066    if (url.equals("http://hl7.org/fhir/ValueSet/mimetypes")) {
067      return generateImplicitMimetypesValueSet(url);
068    }
069    return null;
070  }
071
072  private String defaultVersion(String url) {
073    for (Parameters.ParametersParameterComponent p : expParameters.getParameter()) {
074      if ("default-version".equals(p.getName())) {
075        String u = p.getValue().primitiveValue();
076        if (u.startsWith(url+"|")) {
077          return u.substring(u.indexOf(1)+1);
078        }
079      }
080    }
081    return null;
082  }
083
084  public ValueSet generateImplicitValueSet(String url, String version) {
085    if (Utilities.noString(url)) {
086      return null;
087    }
088
089    if (Utilities.noString(version)) {
090      return generateImplicitValueSet(url);
091    }
092
093    if (url.startsWith("http://snomed.info/sct")) {
094      return generateImplicitSnomedValueSet(url, version);
095    } else if (url.startsWith("http://loinc.org/vs")) {
096      return generateImplicitLoincValueSet(url, version);
097    } else {
098      return null;
099    }
100  }
101
102  public boolean isImplicitValueSet(String url) {
103    return isImplicitSCTValueSet(url) || isImplicitLoincValueSet(url) || isImplicitUcumValueSet(url) || isImplicitMimeTypesValueSet(url);
104  }
105
106
107  //-- SCT -------------------
108
109  public boolean isImplicitSCTValueSet(String url) {
110    return url.startsWith("http://snomed.info/sct") && url.contains("?fhir_vs");
111  }
112
113  private ValueSet generateImplicitSnomedValueSet(String url, String version) {
114    String query = url.substring(url.indexOf("?")+1);
115    if ("fhir_vs".equals(query)) {
116      ValueSet vs = new ValueSet();
117      vs.setUrl(url);
118      vs.setVersion(version);
119      vs.setName("SCTValueSetAll");
120      vs.setTitle("All Codes SCT ValueSet");
121      vs.setDescription("Value Set for All SNOMED CT Concepts");
122      vs.setCopyright("This value set includes content from SNOMED CT, which is copyright © 2002+ International Health Terminology Standards Development Organisation (SNOMED International), and distributed by agreement between SNOMED International and HL7. Implementer use of SNOMED CT is not covered by this agreement");
123      vs.setStatus(PublicationStatus.ACTIVE);
124      vs.getCompose().addInclude().setSystem("http://snomed.info/sct");
125      vs.setWebPath("https://browser.ihtsdotools.org/");
126      return vs;
127    } else if (query.startsWith("fhir_vs=isa/")) {
128      String sct = query.substring(12);
129      ValueSet vs = new ValueSet();
130      vs.setUrl(url);
131      vs.setVersion(version);
132      vs.setName("SCTValueSetFor"+sct);
133      vs.setTitle("SCT ValueSet for "+sct);
134      vs.setDescription("SNOMED CT Concepts that is-a "+sct);
135      vs.setCopyright("This value set includes content from SNOMED CT, which is copyright © 2002+ International Health Terminology Standards Development Organisation (SNOMED International), and distributed by agreement between SNOMED International and HL7. Implementer use of SNOMED CT is not covered by this agreement");
136      vs.setStatus(PublicationStatus.ACTIVE);
137      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.ISA).setValue(sct);
138      vs.setWebPath("https://browser.ihtsdotools.org/?perspective=full&conceptId1="+sct);
139      return vs;
140    } else if (query.equals("fhir_vs=refset")) {
141      ValueSet vs = new ValueSet();
142      vs.setUrl(url);
143      vs.setVersion(version);
144      vs.setName("SCTReferenceSetList");
145      vs.setTitle("SCT Reference Set List");
146      vs.setDescription("SNOMED CT Reference Sets");
147      vs.setCopyright("This value set includes content from SNOMED CT, which is copyright © 2002+ International Health Terminology Standards Development Organisation (SNOMED International), and distributed by agreement between SNOMED International and HL7. Implementer use of SNOMED CT is not covered by this agreement");
148      vs.setStatus(PublicationStatus.ACTIVE);
149      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.ISA).setValue("refset-base");
150      return vs;
151    } else if (query.startsWith("fhir_vs=refset/")) {
152      String srt = query.substring(15);
153      ValueSet vs = new ValueSet();
154      vs.setUrl(url);
155      vs.setVersion(version);
156      vs.setName("SCTRefSet"+srt);
157      vs.setTitle("SCT Reference Set "+srt);
158      vs.setDescription("SNOMED CT Reference Set "+srt);
159      vs.setCopyright("This value set includes content from SNOMED CT, which is copyright © 2002+ International Health Terminology Standards Development Organisation (SNOMED International), and distributed by agreement between SNOMED International and HL7. Implementer use of SNOMED CT is not covered by this agreement");
160      vs.setStatus(PublicationStatus.ACTIVE);
161      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.IN).setValue(srt);
162      vs.setWebPath("https://browser.ihtsdotools.org/?perspective=full&conceptId1="+srt);
163      return vs;
164    } else {
165      return null;
166    }
167  }
168
169  // -- LOINC -----------------
170
171  public boolean isImplicitLoincValueSet(String url) {
172    return url.startsWith("http://loinc.org/vs");
173  }
174
175  private ValueSet generateImplicitLoincValueSet(String url, String version) {
176    if (url.startsWith("http://loinc.org/vs/LL")) {
177      String c = url.substring(20);
178      ValueSet vs = new ValueSet();
179      vs.setUrl(url);
180      vs.setVersion(version);
181      vs.setName("LOINCAnswers"+c);
182      vs.setTitle("LOINC Answer Codes for "+c);
183      vs.setStatus(PublicationStatus.ACTIVE);
184      vs.setCopyright("This content LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at http://loinc.org/terms-of-use");
185      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("LIST").setOp(Enumerations.FilterOperator.EQUAL).setValue(c);
186      vs.setWebPath("https://loinc.org/LL"+c);
187      return vs;
188    } else if (url.startsWith("http://loinc.org/vs/LP")) {
189      String c = url.substring("http://loinc.org/vs/".length());
190      ValueSet vs = new ValueSet();
191      vs.setUrl(url);
192      vs.setVersion(version);
193      vs.setName("LOINCPartList"+c);
194      vs.setTitle("LOINC Codes for Part "+c);
195      vs.setStatus(PublicationStatus.ACTIVE);
196      vs.setCopyright("This content LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at http://loinc.org/terms-of-use");
197      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("ancestor").setOp(Enumerations.FilterOperator.EQUAL).setValue(c);
198      vs.setWebPath("https://loinc.org/LP"+c);
199      return vs;
200    } else if (url.equals("http://loinc.org/vs")) {
201      ValueSet vs = new ValueSet();
202      vs.setUrl(url);
203      vs.setVersion(version);
204      vs.setStatus(PublicationStatus.ACTIVE);
205      vs.setName("LOINCCodes");
206      vs.setTitle("All LOINC codes");
207      vs.setCopyright("This content LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at http://loinc.org/terms-of-use");
208      vs.getCompose().addInclude().setSystem("http://loinc.org");
209      vs.setWebPath("http://loinc.org/");
210      return vs;
211    } else if ("http://loinc.org/vs/valid-hl7-attachment-requests".equals(url)) {
212      ValueSet vs = new ValueSet();
213      vs.setUrl(url);
214      vs.setVersion(version);
215      vs.setStatus(PublicationStatus.ACTIVE);
216      vs.setName("HL7AttachmentRequests");
217      vs.setTitle("HL7 Attachment Requests");
218      vs.setCopyright("This content LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at http://loinc.org/terms-of-use");
219      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("ValidHL7AttachmentRequest").setOp(Enumerations.FilterOperator.EQUAL).setValue("Y");
220      vs.setWebPath("https://loinc.org/attachments/");
221      return vs;
222    } else {
223      return null;
224    }
225  }
226
227  // -- UCUM ---------
228
229  private boolean isImplicitUcumValueSet(String url) {
230    return "http://unitsofmeasure.org/vs".equals(url);
231  }
232
233  private ValueSet allUcumValueSet() {
234    ValueSet vs = new ValueSet();
235    vs.setUrl("http://unitsofmeasure.org/vs");
236    vs.setStatus(PublicationStatus.ACTIVE);
237    vs.setName("AllUcumCodes");
238    vs.setTitle("All Ucum Codes");
239    vs.getCompose().addInclude().setSystem("http://unitsofmeasure.org");
240    return vs;
241  }
242
243  // -- MIME Types -------
244
245  private boolean isImplicitMimeTypesValueSet(String url) {
246    return "http://hl7.org/fhir/ValueSet/mimetypes".equals(url);
247  }
248
249  private ValueSet generateImplicitMimetypesValueSet(String theUri) {
250    ValueSet valueSet = new ValueSet();
251    valueSet.setStatus(PublicationStatus.ACTIVE);
252    valueSet.setUrl(theUri);
253    valueSet.setDescription("This value set includes all possible codes from BCP-13 (http://tools.ietf.org/html/bcp13)");
254    valueSet.getCompose()
255      .addInclude().setSystem("urn:ietf:bcp:13");
256    return valueSet;
257  }
258
259
260}