001package org.hl7.fhir.utilities;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.apache.commons.lang3.StringUtils;
007import org.hl7.fhir.exceptions.FHIRException;
008
009/*
010  Copyright (c) 2011+, HL7, Inc.
011  All rights reserved.
012
013  Redistribution and use in source and binary forms, with or without modification, 
014  are permitted provided that the following conditions are met:
015
016 * Redistributions of source code must retain the above copyright notice, this 
017     list of conditions and the following disclaimer.
018 * Redistributions in binary form must reproduce the above copyright notice, 
019     this list of conditions and the following disclaimer in the documentation 
020     and/or other materials provided with the distribution.
021 * Neither the name of HL7 nor the names of its contributors may be used to 
022     endorse or promote products derived from this software without specific 
023     prior written permission.
024
025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
026  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
027  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
028  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
029  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
030  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
031  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
032  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
033  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
034  POSSIBILITY OF SUCH DAMAGE.
035
036 */
037
038
039public class VersionUtilities {
040
041
042  public static class VersionURLInfo {
043    private String version;
044    private String url;
045    public VersionURLInfo(String version, String url) {
046      super();
047      this.version = version;
048      this.url = url;
049    }
050    public String getVersion() {
051      return version;
052    }
053    public String getUrl() {
054      return url;
055    }
056  }
057
058  public static final String CURRENT_VERSION = "4.6";
059  public static final String CURRENT_FULL_VERSION = "4.6.0";
060
061  public static String packageForVersion(String v) {
062    if (isR2Ver(v)) {
063      return "hl7.fhir.r2.core";
064    }
065    if (isR2BVer(v)) {
066      return "hl7.fhir.r2b.core";
067    }
068    if (isR3Ver(v)) {
069      return "hl7.fhir.r3.core";
070    }
071    if (isR4Ver(v)) {
072      return "hl7.fhir.r4.core";
073    }
074    
075    if (isR4BVer(v)) {
076      return "hl7.fhir.r4b.core";
077    }
078
079    if (isR5Ver(v)) {
080      return "hl7.fhir.r5.core";
081    }
082    
083    if ("current".equals(v)) {
084      return "hl7.fhir.r5.core";
085    }
086    if (v != null && v.startsWith(CURRENT_VERSION)) {
087      return "hl7.fhir.r5.core";
088    }
089    if (Utilities.existsInList(v, "4.4.0", "4.5.0")) {
090      return "hl7.fhir.r5.core";
091    }
092    return null;
093  }
094
095  public static String getCurrentVersion(String v) {
096    if (isR2Ver(v)) {
097      return "1.0.2";
098    }
099    if (isR2BVer(v)) {
100      return "1.4.0";
101    }
102    if (isR3Ver(v)) {
103      return "3.0.2";
104    }
105    if (isR4Ver(v)) {
106      return "4.0.1";
107    }
108    if (isR5Ver(v)) {
109      return "5.0.0";
110    }
111    if (v != null && v.startsWith(CURRENT_VERSION)) {
112      return "current";
113    }
114    return v;
115  }
116
117  public static String getCurrentPackageVersion(String v) {
118    if (isR2Ver(v)) {
119      return "1.0";
120    }
121    if (isR2BVer(v)) {
122      return "1.4";
123    }
124    if (isR3Ver(v)) {
125      return "3.0";
126    }
127    if (isR4Ver(v)) {
128      return "4.0";
129    }
130    if (v != null && v.startsWith(CURRENT_VERSION)) {
131      return "current";
132    }
133    return v;
134  }
135
136  public static boolean isSupportedVersion(String version) {
137    if (version.contains("-")) {
138      version = version.substring(0, version.indexOf("-"));
139    }
140    return Utilities.existsInList(version, "1.0.2", "1.4.0", "3.0.2", "4.0.1", "4.1.0", "4.3.0", "5.0.0", CURRENT_FULL_VERSION);
141  }
142
143  public static String listSupportedVersions() {
144    return "1.0.2, 1.4.0, 3.0.2, 4.0.1, 4.1.0, 4.3.0, 5.0, " + CURRENT_FULL_VERSION;
145  }
146
147  public static boolean isR5Ver(String ver) {
148    return ver != null && (ver.startsWith("5.0") || ver.startsWith(CURRENT_VERSION) || ver.equals("current"));
149  }
150
151  public static boolean isR4BVer(String ver) {
152    return ver != null && (ver.startsWith("4.1") || ver.startsWith("4.3"));
153  }
154
155  public static boolean isR4Ver(String ver) {
156    return ver != null && ver.startsWith("4.0");
157  }
158
159  public static boolean isR3Ver(String ver) {
160    return ver != null && ver.startsWith("3.0");
161  }
162
163  public static boolean isR2BVer(String ver) {
164    return ver != null && ver.startsWith("1.4");
165  }
166
167  public static boolean isR2Ver(String ver) {
168    return ver != null && ver.startsWith("1.0");
169  }
170
171  public static boolean versionsCompatible(String v1, String v2) {
172    if (v1 == null || v2 == null) {
173      return false;
174    }
175    String[] v1l = v1.split("\\|"); 
176    String[] v2l = v2.split("\\|");
177    for (String vs1 : v1l) {
178      for (String vs2 : v2l) {
179        String mm1 = getMajMin(vs1);
180        String mm2 = getMajMin(vs2);
181        if (mm1 == null || mm2 == null) {
182          return false;
183        } else {
184          if (mm1.equals(mm2)) {
185            return true;
186          }
187        }
188      }
189    }
190    return false;
191  }
192
193  public static boolean isCorePackage(String s) {
194    if (s.contains("#")) {
195      s = s.substring(0, s.indexOf("#"));
196    }
197    return Utilities.existsInList(s, "hl7.fhir.core","hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core");
198  }
199
200  public static String getMajMin(String version) {
201    if (version == null)
202      return null;
203    
204    if ("current".equals(version)) {
205      return CURRENT_VERSION;
206    }
207
208    if (Utilities.charCount(version, '.') == 1) {
209      String[] p = version.split("\\.");
210      return p[0]+"."+p[1];
211    } else if (Utilities.charCount(version, '.') == 2) {
212      String[] p = version.split("\\.");
213      return p[0]+"."+p[1];
214    } else {
215      return null;
216    }
217  }
218
219  public static String getPatch(String version) {
220    if (version == null)
221      return null;
222    if (Utilities.charCount(version, '.') == 2) {
223      String[] p = version.split("\\.");
224      return p[2];  
225    }
226    return null;
227  }
228
229  public static boolean isSemVer(String version) {
230    if (Utilities.charCount(version, '.') != 2) {
231      return false;
232    }
233    String[] p = version.split("\\.");
234    if (p[2].contains("-")) {
235      p[2] = p[2].substring(0, p[2].indexOf("-"));
236    }
237    return Utilities.isInteger(p[0]) && Utilities.isInteger(p[1]) && Utilities.isInteger(p[2]);
238  }
239
240  /** 
241   * return true if the current version equals test, or later,
242   * so if a feature is defined in 4.0, if (VersionUtilities.isThisOrLater("4.0", version))
243   * <p>
244   * This method tries to perform a numeric parse, so that <code>0.9</code> will be considered below <code>0.10</code>
245   * in accordance with SemVer. If either side contains a non-numeric character in a version string, a simple text
246   * compare will be done instead.
247   * </p>
248   *  
249   * @param test The value to compare to
250   * @param current The value being compared
251   * @return Is {@literal current} later or equal to {@literal test}? For example, if <code>this = 0.5</code> and <code>current = 0.6</code> this method will return true
252   */
253  public static boolean isThisOrLater(String test, String current) {
254    if (test == null || current == null) {
255      return false;
256    }
257    String t = getMajMin(test);
258    String c = getMajMin(current);
259    if (t == null || c == null) {
260      return false;
261    }
262    if (c.compareTo(t) == 0) {
263      return isMajMinOrLaterPatch(test, current);
264    }
265
266    String[] testParts = t.split("\\.");
267    String[] currentParts = c.split("\\.");
268
269    for (int i = 0; i < Math.max(testParts.length, currentParts.length); i++) {
270      if (i == testParts.length) {
271        return true;
272      } else if (i == currentParts.length) {
273        return false;
274      }
275      String testPart = testParts[i];
276      String currentPart = currentParts[i];
277      if (testPart.equals(currentPart)) {
278        continue;
279      }
280      return compareVersionPart(testPart, currentPart);
281    }
282
283    return true;
284  }
285
286  private static boolean compareVersionPart(String theTestPart, String theCurrentPart) {
287    if (StringUtils.isNumeric(theTestPart) && StringUtils.isNumeric(theCurrentPart)) {
288      return Integer.parseInt(theCurrentPart) - Integer.parseInt(theTestPart) >= 0;
289    } else {
290      return theCurrentPart.compareTo(theTestPart) >= 0;
291    }
292  }
293
294  /** 
295   * return true if the current version equals test for major and min, or later patch 
296   * 
297   * @param test
298   * @param current
299   * @return
300   */
301  public static boolean isMajMinOrLaterPatch(String test, String current) {
302    String t = getMajMin(test);
303    String c = getMajMin(current);
304    if (c != null && c.compareTo(t) == 0) {
305      String pt = getPatch(test);
306      String pc = getPatch(current);
307      if (pt==null || "x".equals(pt)) {
308        return true;
309      }
310      if (pc!=null) {
311        return compareVersionPart(pt, pc);
312      }
313    }
314    return false;
315  }
316
317  public static String incMajorVersion(String v) {
318    assert isSemVer(v);
319    int[] parts = splitParts(v);
320    return Integer.toString(parts[0]+1)+".0.0";
321  }
322
323  public static String incMinorVersion(String v) {
324    assert isSemVer(v);
325    int[] parts = splitParts(v);
326    return Integer.toString(parts[0])+"."+Integer.toString(parts[1]+1)+".0";
327  }
328
329  public static String incPatchVersion(String v) {
330    assert isSemVer(v);
331    int[] parts = splitParts(v);
332    return Integer.toString(parts[0])+"."+Integer.toString(parts[1])+"."+Integer.toString(parts[2]+1);
333  }
334
335  private static int[] splitParts(String v) {
336    String[] p = v.split("\\.");
337    int[] i = new int[] {Integer.parseInt(p[0]),Integer.parseInt(p[1]),Integer.parseInt(p[2])};
338    return i;
339  }
340
341  public static String versionFromCode(String version) {
342    if ("r2".equals(version)) {
343      return "1.0.2";
344    }
345    if ("r2b".equals(version)) {
346      return "1.4.0";
347    }
348    if ("r3".equals(version)) {
349      return "3.0.2";
350    }
351    if ("r4".equals(version)) {
352      return "4.0.1";
353    }
354    if ("r5".equals(version)) {
355      return CURRENT_FULL_VERSION;
356    }
357    throw new FHIRException("Unknown version "+version);
358  }
359
360  public static VersionURLInfo parseVersionUrl(String url) {
361    if (url.length() < 24) {
362      return null;
363    }
364    String v = url.substring(20, 24);
365    if (v.endsWith("/")) {
366      v = v.substring(0, v.length()-1);
367      if (Utilities.existsInList(v, "1.0", "1.4", "3.0", "4.0", "5.0", CURRENT_VERSION)) {
368        return new VersionURLInfo(v, "http://hl7.org/fhir/"+url.substring(24));
369      }
370    }
371    return null;
372  }
373
374  public static List<String> getCanonicalResourceNames(String version) {
375    ArrayList<String> res = new ArrayList<String>();
376    if (isR2Ver(version) || isR2BVer(version)) {
377      res.add("ValueSet");
378      res.add("ConceptMap");
379      res.add("NamingSystem");
380      res.add("StructureDefinition");
381      res.add("DataElement");
382      res.add("Conformance");
383      res.add("OperationDefinition");
384      res.add("SearchParameter");
385      res.add("ImplementationGuide");
386      res.add("TestScript");
387    }
388    if (isR3Ver(version)) {
389      res.add("CodeSystem");
390      res.add("CapabilityStatement");
391      res.add("StructureDefinition");
392      res.add("ImplementationGuide");
393      res.add("SearchParameter");
394      res.add("MessageDefinition");
395      res.add("OperationDefinition");
396      res.add("CompartmentDefinition");
397      res.add("StructureMap");
398      res.add("GraphDefinition");
399      res.add("DataElement");
400      res.add("CodeSystem");
401      res.add("ValueSet");
402      res.add("ConceptMap");
403      res.add("ExpansionProfile");
404      res.add("Questionnaire");
405      res.add("ActivityDefinition");
406      res.add("ServiceDefinition");
407      res.add("PlanDefinition");
408      res.add("Measure");
409      res.add("TestScript");
410
411    }
412    if (isR4Ver(version)) {
413      res.add("CodeSystem");
414      res.add("ActivityDefinition");
415      res.add("CapabilityStatement");
416      res.add("ChargeItemDefinition");
417      res.add("CodeSystem");
418      res.add("CompartmentDefinition");
419      res.add("ConceptMap");
420      res.add("EffectEvidenceSynthesis");
421      res.add("EventDefinition");
422      res.add("Evidence");
423      res.add("EvidenceVariable");
424      res.add("ExampleScenario");
425      res.add("GraphDefinition");
426      res.add("ImplementationGuide");
427      res.add("Library");
428      res.add("Measure");
429      res.add("MessageDefinition");
430      res.add("NamingSystem");
431      res.add("OperationDefinition");
432      res.add("PlanDefinition");
433      res.add("Questionnaire");
434      res.add("ResearchDefinition");
435      res.add("ResearchElementDefinition");
436      res.add("RiskEvidenceSynthesis");
437      res.add("SearchParameter");
438      res.add("StructureDefinition");
439      res.add("StructureMap");
440      res.add("TerminologyCapabilities");
441      res.add("TestScript");
442      res.add("ValueSet");
443    }
444    if (isR4BVer(version)) {
445      res.add("ActivityDefinition");
446      res.add("CapabilityStatement");
447      res.add("ChargeItemDefinition");
448      res.add("Citation");
449      res.add("CodeSystem");
450      res.add("CompartmentDefinition");
451      res.add("ConceptMap");
452      res.add("EventDefinition");
453      res.add("Evidence");
454      res.add("EvidenceReport");
455      res.add("EvidenceVariable");
456      res.add("ExampleScenario");
457      res.add("GraphDefinition");
458      res.add("ImplementationGuide");
459      res.add("Library");
460      res.add("Measure");
461      res.add("MessageDefinition");
462      res.add("NamingSystem");
463      res.add("OperationDefinition");
464      res.add("PlanDefinition");
465      res.add("Questionnaire");
466      res.add("ResearchDefinition");
467      res.add("ResearchElementDefinition");
468      res.add("SearchParameter");
469      res.add("StructureDefinition");
470      res.add("StructureMap");
471      res.add("SubscriptionTopic");
472      res.add("TerminologyCapabilities");
473      res.add("TestScript");
474      res.add("ValueSet");
475    }
476
477    if (isR5Ver(version) || "current".equals(version)) {
478
479      res.add("ActivityDefinition");
480      res.add("CapabilityStatement");
481      res.add("CapabilityStatement2");
482      res.add("ChargeItemDefinition");
483      res.add("Citation");
484      res.add("CodeSystem");
485      res.add("CompartmentDefinition");
486      res.add("ConceptMap");
487      res.add("ConditionDefinition");
488      res.add("EventDefinition");
489      res.add("Evidence");
490      res.add("EvidenceReport");
491      res.add("EvidenceVariable");
492      res.add("ExampleScenario");
493      res.add("GraphDefinition");
494      res.add("ImplementationGuide");
495      res.add("Library");
496      res.add("Measure");
497      res.add("MessageDefinition");
498      res.add("NamingSystem");
499      res.add("OperationDefinition");
500      res.add("PlanDefinition");
501      res.add("Questionnaire");
502      res.add("SearchParameter");
503      res.add("StructureDefinition");
504      res.add("StructureMap");
505      res.add("TerminologyCapabilities");
506      res.add("TestScript");
507      res.add("ValueSet");
508    }
509    return res;
510  }
511
512  public static String getVersionForPackage(String pid) {
513    if (pid.startsWith("hl7.fhir.r")) {
514      String[] p = pid.split("\\.");
515      return versionFromCode(p[2]);
516    }
517    return null;
518  }
519
520  public static boolean versionsMatch(String v1, String v2) {
521    String mm1 = getMajMin(v1);
522    String mm2 = getMajMin(v2);
523    return mm1 != null && mm2 != null && mm1.equals(mm2);
524  }
525
526  public static boolean isR5VerOrLater(String version) {
527    if (version == null) {
528      return false;
529    }
530    if (version.startsWith(CURRENT_VERSION) || version.equals("current")) {
531      return true;
532    }
533    String v = getMajMin(version);
534    return v.compareTo("4.5") >= 0; 
535  }
536
537
538}