001package org.hl7.fhir.utilities.validation;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.utilities.FhirPublication;
005import org.hl7.fhir.utilities.Utilities;
006import org.hl7.fhir.utilities.i18n.AcceptLanguageHeader;
007
008public class ValidationOptions {
009  
010  public enum R5BundleRelativeReferencePolicy {
011    DEFAULT,
012    NEVER,
013    ALWAYS;
014
015    public String toCode() {
016      switch (this) {
017      case ALWAYS: return "always";
018      case DEFAULT: return "default";
019      case NEVER: return "never";
020      }
021      return null;
022    }
023
024    public static R5BundleRelativeReferencePolicy fromCode(String code) {
025      switch (code) {
026      case "always": return ALWAYS;
027      case "default": return DEFAULT;
028      case "never": return NEVER;
029      }
030      throw new FHIRException("bad code "+code);
031    }
032  }
033
034  private AcceptLanguageHeader langs = null;
035  private boolean useServer = true;
036  private boolean useClient = true;
037  private boolean guessSystem = false;
038  private boolean membershipOnly = false;
039  private boolean displayWarningMode = false;
040  private boolean vsAsUrl;
041  private boolean versionFlexible = true;
042  private boolean useValueSetDisplays;
043  private boolean englishOk = true;
044  private boolean activeOnly = false;
045  private boolean exampleOK = false;
046  private FhirPublication fhirVersion;
047  private R5BundleRelativeReferencePolicy r5BundleRelativeReferencePolicy = R5BundleRelativeReferencePolicy.DEFAULT;
048  private boolean isDefaultLang = false;
049  
050  public ValidationOptions() { this(FhirPublication.R5); }
051
052  public ValidationOptions(FhirPublication fhirVersion) {
053    super();
054    this.fhirVersion = fhirVersion;
055  }
056
057  public ValidationOptions(FhirPublication fhirVersion, String language) {
058    super();
059    if (!Utilities.noString(language)) {
060      langs = new AcceptLanguageHeader(language, false);
061      isDefaultLang = false;
062    }
063  }
064
065  public static ValidationOptions defaults() {
066    ValidationOptions vo = new ValidationOptions(FhirPublication.R5, "en, en-US");
067    vo.isDefaultLang  = true;
068    return vo;
069  }
070  
071  /**
072   * The language that the validation is in (for display name checking etc
073   * 
074   * See also englishOK
075   * 
076   * @return
077   */
078  public AcceptLanguageHeader getLanguages() {
079    return langs;
080  }
081
082  public boolean hasLanguages() {
083    return langs != null && !Utilities.noString(langs.getSource());
084  }
085
086
087  /**
088   * true (which is default) means that the validator will use the server if it needs to for this request
089   * 
090   * (there's a few corner cases where you want to turn this off?)
091   * @return
092   */
093  public boolean isUseServer() {
094    return useServer;
095  }
096
097  /**
098   * True means that the validator will try to resolve the terminology request locally with the resources at hand 
099   * 
100   * There have been a few corner case code systems where the local code system from THO is wrong
101   * 
102   * @return
103   */
104  public boolean isUseClient() {
105    return useClient;
106  }
107
108  /**
109   * True if this is called from a code context where there's no known code system (inferred from the value set)
110   * 
111   * @return
112   */
113  public boolean isGuessSystem() {
114    return guessSystem;
115  }
116  
117  public boolean isActiveOnly() {
118    return activeOnly;
119  }
120
121  /**
122   * Don't know what this does
123   * 
124   * @return
125   */
126  public boolean getVsAsUrl() {
127    return vsAsUrl;
128  }
129
130  /**
131   * Don't know exactly what this does
132   * 
133   * @return
134   */
135  public boolean isVersionFlexible() {
136    return versionFlexible;
137  }
138
139  /**
140   * see {link}
141   *  
142   * @return
143   */
144  public boolean isUseValueSetDisplays() {
145    return useValueSetDisplays;
146  }
147
148  public boolean isMembershipOnly() {
149    return membershipOnly;
150  }
151
152  /**
153   * if the language is other than english, should the validator accept english as well?
154   * 
155   * @return
156   */
157  public boolean isEnglishOk() {
158    return englishOk;
159  }
160
161
162  public ValidationOptions withLanguage(String language) {
163    if (language == null) {
164      return this;
165    }
166    ValidationOptions n = this.copy();
167    n.addLanguage(language);
168    return n;
169  }
170
171  public ValidationOptions withNoServer() {
172    ValidationOptions n = this.copy();
173    n.useServer = false;
174    return n;
175  }
176
177  public ValidationOptions withNoClient() {
178    ValidationOptions n = this.copy();
179    n.useClient = false;
180    return n;
181  }
182
183  public ValidationOptions withUseClient(boolean value) {
184    ValidationOptions n = this.copy();
185    n.useClient = value;
186    return n;
187  }
188
189  public ValidationOptions withGuessSystem() {
190    ValidationOptions n = this.copy();
191    n.guessSystem = true;
192    return n;
193  }
194
195
196  public ValidationOptions withGuessSystem(boolean value) {
197    ValidationOptions n = this.copy();
198    n.guessSystem = value;
199    return n;
200  }
201  
202  public ValidationOptions withActiveOnly() {
203    ValidationOptions n = this.copy();
204    n.activeOnly = true;
205    return n;
206  }
207  
208  /** Only for additional bindings **/
209  public ValidationOptions withCheckValueSetOnly() {
210    ValidationOptions n = this.copy();
211    n.membershipOnly = true;
212    return n;
213  }
214
215  public ValidationOptions withVsAsUrl() {
216    ValidationOptions n = this.copy();
217    n.vsAsUrl = true;
218    return n;
219  }
220
221  public ValidationOptions withVersionFlexible(boolean value) {
222    ValidationOptions n = this.copy();
223    n.versionFlexible = value;
224    return n;
225  }
226
227  public ValidationOptions withUseValueSetDisplays(boolean useValueSetDisplays) {
228    ValidationOptions n = this.copy();
229    n.useValueSetDisplays = useValueSetDisplays;
230    return n;
231  }
232  
233  public ValidationOptions withEnglishOk(boolean englishOk) {
234    ValidationOptions n = this.copy();
235    n.englishOk = englishOk;
236    return n;
237  }
238
239  public ValidationOptions addLanguage(String language) {
240    if (this.langs == null || isDefaultLang) {
241      langs = new AcceptLanguageHeader(language, false);
242    } else {
243      langs.add(language);
244      isDefaultLang = false;
245    }
246    return this;
247  }
248
249  public ValidationOptions setLanguages(String language) {
250    langs = new AcceptLanguageHeader(language, false);
251    isDefaultLang = false;
252    return this;
253  }
254
255  public ValidationOptions setUseServer(boolean useServer) {
256    this.useServer = useServer;
257    return this;
258  }
259  
260  public ValidationOptions setUseClient(boolean useClient) {
261    this.useClient = useClient;
262    return this;
263  }
264
265  public ValidationOptions setGuessSystem(boolean guessSystem) {
266    this.guessSystem = guessSystem;
267    return this;
268  }
269  
270  public ValidationOptions setActiveOnly(boolean activeOnly) {
271    this.activeOnly = activeOnly;
272    return this;
273  }
274  
275  public ValidationOptions setCheckValueSetOnly() {
276    this.membershipOnly = true;
277    return this;
278  }
279
280  public ValidationOptions setVsAsUrl(boolean value) {
281    this.vsAsUrl = value;
282    return this;
283  }
284
285  public ValidationOptions setVersionFlexible(boolean value) {
286    this.versionFlexible = value;
287    return this;
288  }
289
290  public ValidationOptions setUseValueSetDisplays(boolean useValueSetDisplays) {
291    this.useValueSetDisplays = useValueSetDisplays;
292    return this;
293  }
294
295  public ValidationOptions setEnglishOk(boolean englishOk) {
296    this.englishOk = englishOk;
297    return this;
298  }
299
300  public boolean isDisplayWarningMode() {
301    return displayWarningMode;
302  }
303
304  public ValidationOptions setDisplayWarningMode(boolean displayWarningMode) {
305    this.displayWarningMode = displayWarningMode;
306    return this;
307  }
308
309  public boolean isExampleOK() {
310    return exampleOK;
311  }
312
313  public ValidationOptions setExampleOK(boolean exampleOK) {
314    this.exampleOK = exampleOK;
315    return this;
316  }
317  
318  public ValidationOptions withExampleOK() {
319    return setExampleOK(true);
320  }
321
322  
323  public R5BundleRelativeReferencePolicy getR5BundleRelativeReferencePolicy() {
324    return r5BundleRelativeReferencePolicy;
325  }
326
327  public void setR5BundleRelativeReferencePolicy(R5BundleRelativeReferencePolicy r5BundleRelativeReferencePolicy) {
328    if (r5BundleRelativeReferencePolicy == null) {
329      r5BundleRelativeReferencePolicy = R5BundleRelativeReferencePolicy.DEFAULT;
330    } 
331    this.r5BundleRelativeReferencePolicy = r5BundleRelativeReferencePolicy;
332  }
333
334  public ValidationOptions withR5BundleRelativeReferencePolicy(R5BundleRelativeReferencePolicy r5BundleRelativeReferencePolicy) {
335    setR5BundleRelativeReferencePolicy(r5BundleRelativeReferencePolicy);
336    return this;
337  }
338
339  public ValidationOptions copy() {
340    ValidationOptions n = new ValidationOptions(fhirVersion);
341    n.langs = langs == null ? null : langs.copy();
342    n.isDefaultLang = isDefaultLang;
343    n.useServer = useServer;
344    n.useClient = useClient;
345    n.guessSystem = guessSystem; 
346    n.activeOnly = activeOnly; 
347    n.vsAsUrl = vsAsUrl;
348    n.versionFlexible = versionFlexible;
349    n.membershipOnly = membershipOnly;
350    n.useValueSetDisplays = useValueSetDisplays;   
351    n.displayWarningMode = displayWarningMode;
352    n.exampleOK = exampleOK;
353    n.r5BundleRelativeReferencePolicy = r5BundleRelativeReferencePolicy;
354    return n;
355  }
356  
357
358  public String toJson() {
359    return "\"langs\":\""+( langs == null ? "" : langs.toString())+"\", \"useServer\":\""+Boolean.toString(useServer)+"\", \"useClient\":\""+Boolean.toString(useClient)+"\", "+
360       "\"guessSystem\":\""+Boolean.toString(guessSystem)+"\", \"activeOnly\":\""+Boolean.toString(activeOnly)+(exampleOK ? "\", \"exampleOK\":\""+Boolean.toString(exampleOK) : "")+
361       "\", \"membershipOnly\":\""+Boolean.toString(membershipOnly)+"\", \"displayWarningMode\":\""+Boolean.toString(displayWarningMode)+
362       "\", \"versionFlexible\":\""+Boolean.toString(versionFlexible)+"\""+
363       (r5BundleRelativeReferencePolicy != R5BundleRelativeReferencePolicy.DEFAULT ? ", \"r5BundleRelativeReferencePolicy\":\""+r5BundleRelativeReferencePolicy.toCode()+"\"" : "");
364  }
365
366  public String langSummary() {
367    if (langs == null) {
368      return "--";
369    } else {
370      String s = langs.toString();
371      if (Utilities.noString(s)) {
372        s = "--";
373      }
374      return s;
375    }
376  }
377
378  public FhirPublication getFhirVersion() {
379    return fhirVersion;
380  }
381
382  
383}