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      return vs;
126    } else if (query.startsWith("fhir_vs=isa/")) {
127      String sct = query.substring(12);
128      ValueSet vs = new ValueSet();
129      vs.setUrl(url);
130      vs.setVersion(version);
131      vs.setName("SCTValueSetFor"+sct);
132      vs.setTitle("SCT ValueSet for "+sct);
133      vs.setDescription("SNOMED CT Concepts that is-a "+sct);
134      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");
135      vs.setStatus(PublicationStatus.ACTIVE);
136      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.ISA).setValue(sct);
137      vs.setWebPath("https://browser.ihtsdotools.org/?perspective=full&conceptId1="+sct);
138      return vs;
139    } else if (query.equals("fhir_vs=refset")) {
140      ValueSet vs = new ValueSet();
141      vs.setUrl(url);
142      vs.setVersion(version);
143      vs.setName("SCTReferenceSetList");
144      vs.setTitle("SCT Reference Set List");
145      vs.setDescription("SNOMED CT Reference Sets");
146      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");
147      vs.setStatus(PublicationStatus.ACTIVE);
148      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.ISA).setValue("refset-base");
149      return vs;
150    } else if (query.startsWith("fhir_vs=refset/")) {
151      String srt = query.substring(15);
152      ValueSet vs = new ValueSet();
153      vs.setUrl(url);
154      vs.setVersion(version);
155      vs.setName("SCTRefSet"+srt);
156      vs.setTitle("SCT Reference Set "+srt);
157      vs.setDescription("SNOMED CT Reference Set "+srt);
158      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");
159      vs.setStatus(PublicationStatus.ACTIVE);
160      vs.getCompose().addInclude().setSystem("http://snomed.info/sct").addFilter().setProperty("concept").setOp(Enumerations.FilterOperator.IN).setValue(srt);
161      vs.setWebPath("https://browser.ihtsdotools.org/?perspective=full&conceptId1="+srt);
162      return vs;
163    } else {
164      return null;
165    }
166  }
167
168  // -- LOINC -----------------
169
170  public boolean isImplicitLoincValueSet(String url) {
171    return url.startsWith("http://loinc.org/vs");
172  }
173
174  private ValueSet generateImplicitLoincValueSet(String url, String version) {
175    if (url.startsWith("http://loinc.org/vs/LL")) {
176      String c = url.substring(20);
177      ValueSet vs = new ValueSet();
178      vs.setUrl(url);
179      vs.setVersion(version);
180      vs.setName("LOINCAnswers"+c);
181      vs.setTitle("LOINC Answer Codes for "+c);
182      vs.setStatus(PublicationStatus.ACTIVE);
183      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");
184      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("LIST").setOp(Enumerations.FilterOperator.EQUAL).setValue(c);
185      return vs;
186    } else if (url.startsWith("http://loinc.org/vs/LP")) {
187      String c = url.substring("http://loinc.org/vs/".length());
188      ValueSet vs = new ValueSet();
189      vs.setUrl(url);
190      vs.setVersion(version);
191      vs.setName("LOINCPartList"+c);
192      vs.setTitle("LOINC Codes for Part "+c);
193      vs.setStatus(PublicationStatus.ACTIVE);
194      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");
195      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("ancestor").setOp(Enumerations.FilterOperator.EQUAL).setValue(c);
196      return vs;
197    } else if (url.equals("http://loinc.org/vs")) {
198      ValueSet vs = new ValueSet();
199      vs.setUrl(url);
200      vs.setVersion(version);
201      vs.setStatus(PublicationStatus.ACTIVE);
202      vs.setName("LOINCCodes");
203      vs.setTitle("All LOINC codes");
204      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");
205      vs.getCompose().addInclude().setSystem("http://loinc.org");
206      return vs;
207    } else if ("http://loinc.org/vs/valid-hl7-attachment-requests".equals(url)) {
208      ValueSet vs = new ValueSet();
209      vs.setUrl(url);
210      vs.setVersion(version);
211      vs.setStatus(PublicationStatus.ACTIVE);
212      vs.setName("HL7AttachmentRequests");
213      vs.setTitle("HL7 Attachment Requests");
214      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");
215      vs.getCompose().addInclude().setSystem("http://loinc.org").addFilter().setProperty("ValidHL7AttachmentRequest").setOp(Enumerations.FilterOperator.EQUAL).setValue("Y");
216      vs.setWebPath("https://loinc.org/attachments/");
217      return vs;
218    } else {
219      return null;
220    }
221  }
222
223  // -- UCUM ---------
224
225  private boolean isImplicitUcumValueSet(String url) {
226    return "http://unitsofmeasure.org/vs".equals(url);
227  }
228
229  private ValueSet allUcumValueSet() {
230    ValueSet vs = new ValueSet();
231    vs.setUrl("http://unitsofmeasure.org/vs");
232    vs.setStatus(PublicationStatus.ACTIVE);
233    vs.setName("AllUcumCodes");
234    vs.setTitle("All Ucum Codes");
235    vs.getCompose().addInclude().setSystem("http://unitsofmeasure.org");
236    return vs;
237  }
238
239  // -- MIME Types -------
240
241  private boolean isImplicitMimeTypesValueSet(String url) {
242    return "http://hl7.org/fhir/ValueSet/mimetypes".equals(url);
243  }
244
245  private ValueSet generateImplicitMimetypesValueSet(String theUri) {
246    ValueSet valueSet = new ValueSet();
247    valueSet.setStatus(PublicationStatus.ACTIVE);
248    valueSet.setUrl(theUri);
249    valueSet.setDescription("This value set includes all possible codes from BCP-13 (http://tools.ietf.org/html/bcp13)");
250    valueSet.getCompose()
251      .addInclude().setSystem("urn:ietf:bcp:13");
252    return valueSet;
253  }
254
255
256}