001package org.hl7.fhir.dstu2.utils;
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 */
031
032import java.util.List;
033
034import java.util.Locale;
035import org.hl7.fhir.dstu2.formats.IParser;
036import org.hl7.fhir.dstu2.formats.ParserType;
037import org.hl7.fhir.dstu2.model.CodeableConcept;
038import org.hl7.fhir.dstu2.model.Coding;
039import org.hl7.fhir.dstu2.model.ConceptMap;
040import org.hl7.fhir.dstu2.model.Resource;
041import org.hl7.fhir.dstu2.model.StructureDefinition;
042import org.hl7.fhir.dstu2.model.ValueSet;
043import org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent;
044import org.hl7.fhir.dstu2.model.ValueSet.ConceptSetComponent;
045import org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionComponent;
046import org.hl7.fhir.dstu2.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
047import org.hl7.fhir.dstu2.utils.validation.IResourceValidator;
048import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
049
050/**
051 * This is the standard interface used for access to underlying FHIR services
052 * through the tools and utilities provided by the reference implementation.
053 * 
054 * The functionality it provides is - get access to parsers, validators,
055 * narrative builders etc (you can't create these directly because they need
056 * access to the right context for their information)
057 * 
058 * - find resources that the tools need to carry out their tasks
059 * 
060 * - provide access to terminology services they need. (typically, these
061 * terminology service requests are just passed through to the local
062 * implementation's terminology service)
063 * 
064 * @author Grahame
065 */
066
067public interface IWorkerContext {
068
069  // -- Parsers (read and write instances)
070  // ----------------------------------------
071
072  /**
073   * Get a parser to read/write instances. Use the defined type (will be extended
074   * as further types are added, though the only currently anticipate type is RDF)
075   * 
076   * XML/JSON - the standard renderers XHTML - render the narrative only (generate
077   * it if necessary)
078   * 
079   * @param type
080   * @return
081   */
082  public IParser getParser(ParserType type);
083
084  /**
085   * Get a parser to read/write instances. Determine the type from the stated
086   * type. Supported value for type: - the recommended MIME types - variants of
087   * application/xml and application/json - _format values xml, json
088   * 
089   * @param type
090   * @return
091   */
092  public IParser getParser(String type);
093
094  /**
095   * Get a JSON parser
096   * 
097   * @return
098   */
099  public IParser newJsonParser();
100
101  /**
102   * Get an XML parser
103   * 
104   * @return
105   */
106  public IParser newXmlParser();
107
108  /**
109   * Get a generator that can generate narrative for the instance
110   * 
111   * @return a prepared generator
112   */
113  public INarrativeGenerator getNarrativeGenerator(String prefix, String basePath);
114
115  /**
116   * Get a validator that can check whether a resource is valid
117   * 
118   * @return a prepared generator @
119   */
120  public IResourceValidator newValidator();
121
122  // -- resource fetchers ---------------------------------------------------
123
124  /**
125   * Find an identified resource. The most common use of this is to access the the
126   * standard conformance resources that are part of the standard - structure
127   * definitions, value sets, concept maps, etc.
128   * 
129   * Also, the narrative generator uses this, and may access any kind of resource
130   * 
131   * The URI is called speculatively for things that might exist, so not finding a
132   * matching resouce, return null, not an error
133   * 
134   * The URI can have one of 3 formats: - a full URL e.g.
135   * http://acme.org/fhir/ValueSet/[id] - a relative URL e.g. ValueSet/[id] - a
136   * logical id e.g. [id]
137   * 
138   * It's an error if the second form doesn't agree with class_. It's an error if
139   * class_ is null for the last form
140   * 
141   * @param resource
142   * @param Reference
143   * @return
144   * @throws Exception
145   */
146  public <T extends Resource> T fetchResource(Class<T> class_, String uri);
147
148  /**
149   * find whether a resource is available.
150   * 
151   * Implementations of the interface can assume that if hasResource ruturns true,
152   * the resource will usually be fetched subsequently
153   * 
154   * @param class_
155   * @param uri
156   * @return
157   */
158  public <T extends Resource> boolean hasResource(Class<T> class_, String uri);
159
160  // -- profile services ---------------------------------------------------------
161
162  public List<String> getResourceNames();
163
164  // -- Terminology services
165  // ------------------------------------------------------
166
167  // these are the terminology services used internally by the tools
168  /**
169   * Find a value set for the nominated system uri. return null if there isn't one
170   * (then the tool might try supportsSystem)
171   * 
172   * @param system
173   * @return
174   */
175  public ValueSet fetchCodeSystem(String system);
176
177  /**
178   * True if the underlying terminology service provider will do expansion and
179   * code validation for the terminology. Corresponds to the extension
180   * 
181   * http://hl7.org/fhir/StructureDefinition/conformance-supported-system
182   * 
183   * in the Conformance resource
184   * 
185   * @param system
186   * @return
187   */
188  public boolean supportsSystem(String system);
189
190  /**
191   * find concept maps for a source
192   * 
193   * @param url
194   * @return
195   */
196  public List<ConceptMap> findMapsForSource(String url);
197
198  /**
199   * ValueSet Expansion - see $expand
200   * 
201   * @param source
202   * @return
203   */
204  public ValueSetExpansionOutcome expandVS(ValueSet source, boolean cacheOk);
205
206  /**
207   * Value set expanion inside the internal expansion engine - used for references
208   * to supported system (see "supportsSystem") for which there is no value set.
209   * 
210   * @param inc
211   * @return
212   */
213  public ValueSetExpansionComponent expandVS(ConceptSetComponent inc);
214
215  Locale getLocale();
216
217  void setLocale(Locale locale);
218
219  String formatMessage(String theMessage, Object... theMessageArguments);
220
221  void setValidationMessageLanguage(Locale locale);
222
223  public class ValidationResult {
224    private ConceptDefinitionComponent definition;
225    private IssueSeverity severity;
226    private String message;
227
228    public ValidationResult(IssueSeverity severity, String message) {
229      this.severity = severity;
230      this.message = message;
231    }
232
233    public ValidationResult(ConceptDefinitionComponent definition) {
234      this.definition = definition;
235    }
236
237    public ValidationResult(IssueSeverity severity, String message, ConceptDefinitionComponent definition) {
238      this.severity = severity;
239      this.message = message;
240      this.definition = definition;
241    }
242
243    public boolean isOk() {
244      return definition != null;
245    }
246
247    public String getDisplay() {
248      return definition == null ? "??" : definition.getDisplay();
249    }
250
251    public ConceptDefinitionComponent asConceptDefinition() {
252      return definition;
253    }
254
255    public IssueSeverity getSeverity() {
256      return severity;
257    }
258
259    public String getMessage() {
260      return message;
261    }
262  }
263
264  /**
265   * Validation of a code - consult the terminology service to see whether it is
266   * known. If known, return a description of it
267   * 
268   * note: always return a result, with either an error or a code description
269   * 
270   * corresponds to 2 terminology service calls: $validate-code and $lookup
271   * 
272   * @param system
273   * @param code
274   * @param display
275   * @return
276   */
277  public ValidationResult validateCode(String system, String code, String display);
278
279  /**
280   * Validation of a code - consult the terminology service to see whether it is
281   * known. If known, return a description of it Also, check whether it's in the
282   * provided value set
283   * 
284   * note: always return a result, with either an error or a code description, or
285   * both (e.g. known code, but not in the value set)
286   * 
287   * corresponds to 2 terminology service calls: $validate-code and $lookup
288   * 
289   * @param system
290   * @param code
291   * @param display
292   * @return
293   */
294  public ValidationResult validateCode(String system, String code, String display, ValueSet vs);
295
296  public ValidationResult validateCode(Coding code, ValueSet vs);
297
298  public ValidationResult validateCode(CodeableConcept code, ValueSet vs);
299
300  /**
301   * Validation of a code - consult the terminology service to see whether it is
302   * known. If known, return a description of it Also, check whether it's in the
303   * provided value set fragment (for supported systems with no value set
304   * definition)
305   * 
306   * note: always return a result, with either an error or a code description, or
307   * both (e.g. known code, but not in the value set)
308   * 
309   * corresponds to 2 terminology service calls: $validate-code and $lookup
310   * 
311   * @param system
312   * @param code
313   * @param display
314   * @return
315   */
316  public ValidationResult validateCode(String system, String code, String display, ConceptSetComponent vsi);
317
318  /**
319   * returns the recommended tla for the type
320   * 
321   * @param name
322   * @return
323   */
324  public String getAbbreviation(String name);
325
326  public List<StructureDefinition> allStructures();
327
328  public StructureDefinition fetchTypeDefinition(String typeName);
329
330}