View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.fhir.iti67;
18  
19  import ca.uhn.fhir.model.api.Include;
20  import ca.uhn.fhir.rest.annotation.*;
21  import ca.uhn.fhir.rest.api.SortSpec;
22  import ca.uhn.fhir.rest.api.server.IBundleProvider;
23  import ca.uhn.fhir.rest.param.DateRangeParam;
24  import ca.uhn.fhir.rest.param.ReferenceParam;
25  import ca.uhn.fhir.rest.param.StringParam;
26  import ca.uhn.fhir.rest.param.TokenOrListParam;
27  import ca.uhn.fhir.rest.param.TokenParam;
28  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
29  import org.hl7.fhir.dstu3.model.DocumentReference;
30  import org.hl7.fhir.dstu3.model.IdType;
31  import org.hl7.fhir.dstu3.model.Patient;
32  import org.hl7.fhir.dstu3.model.Practitioner;
33  import org.hl7.fhir.instance.model.api.IAnyResource;
34  import org.openehealth.ipf.commons.ihe.fhir.AbstractPlainProvider;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.util.Set;
39  
40  /**
41   * Resource Provider for MHD (ITI-67).
42   * <p>
43   * Note (CP by Rick Riemer):
44   * When searching for XDS documents with specific referenceIdList values, MHD specifies to use the related-id query parameter.
45   * This parameter is of type token (https://www.hl7.org/fhir/search.html#token). The token type does not allow searching for
46   * the Identifier.type attribute, which would be a primary use case.
47   * <p>
48   * IHE should provide a mechanism to search for referenceIdList values by type, in addition to system and value.
49   * Suggestion: don’t use token, but use composite, (https://www.hl7.org/fhir/search.html#composite) and define how
50   * to use it for searching against referenceIdList values. A composite parameter could look like:
51   * related id=urn:oid:1.2.3.4.5.6|2013001$urn:ihe:iti:xds:2013:accession.
52   *
53   * @author Christian Ohr
54   * @since 3.4
55   */
56  public class Iti67ResourceProvider extends AbstractPlainProvider {
57  
58      @SuppressWarnings("unused")
59      @Search(type = DocumentReference.class)
60      public IBundleProvider documentReferenceSearch(
61              @RequiredParam(name = DocumentReference.SP_PATIENT, chainWhitelist = {"", Patient.SP_IDENTIFIER}) ReferenceParam patient,
62              @OptionalParam(name = DocumentReference.SP_STATUS) TokenOrListParam status,
63              @OptionalParam(name = DocumentReference.SP_INDEXED) DateRangeParam indexed,
64              @OptionalParam(name = DocumentReference.SP_AUTHOR + "." + Practitioner.SP_FAMILY) StringParam authorFamilyName,
65              @OptionalParam(name = DocumentReference.SP_AUTHOR + "." + Practitioner.SP_GIVEN) StringParam authorGivenName,
66              @OptionalParam(name = DocumentReference.SP_CLASS) TokenOrListParam class_,
67              @OptionalParam(name = DocumentReference.SP_TYPE) TokenOrListParam type,
68              @OptionalParam(name = DocumentReference.SP_SETTING) TokenOrListParam setting,
69              @OptionalParam(name = DocumentReference.SP_PERIOD) DateRangeParam period,
70              @OptionalParam(name = DocumentReference.SP_FACILITY) TokenOrListParam facility,
71              @OptionalParam(name = DocumentReference.SP_EVENT) TokenOrListParam event,
72              @OptionalParam(name = DocumentReference.SP_SECURITYLABEL) TokenOrListParam securityLabel,
73              @OptionalParam(name = DocumentReference.SP_FORMAT) TokenOrListParam format,
74              @OptionalParam(name = DocumentReference.SP_RELATED_ID) TokenOrListParam relatedId,
75              // Extension to ITI-66
76              @OptionalParam(name = IAnyResource.SP_RES_ID) TokenParam resourceId,
77              @Sort SortSpec sortSpec,
78              @IncludeParam Set<Include> includeSpec,
79              HttpServletRequest httpServletRequest,
80              HttpServletResponse httpServletResponse) {
81  
82          Iti67SearchParameters searchParameters = Iti67SearchParameters.builder()
83                  .status(status)
84                  .indexed(indexed)
85                  .authorFamilyName(authorFamilyName)
86                  .authorGivenName(authorGivenName)
87                  .class_(class_)
88                  .type(type)
89                  .setting(setting)
90                  .period(period)
91                  .facility(facility)
92                  .event(event)
93                  .securityLabel(securityLabel)
94                  .format(format)
95                  .relatedId(relatedId)
96                  ._id(resourceId)
97                  .sortSpec(sortSpec)
98                  .includeSpec(includeSpec)
99                  .fhirContext(getFhirContext())
100                 .build();
101 
102         String chain = patient.getChain();
103         if (Patient.SP_IDENTIFIER.equals(chain)) {
104             searchParameters.setPatientIdentifier(patient.toTokenParam(getFhirContext()));
105         } else if (chain == null || chain.isEmpty()) {
106             searchParameters.setPatientReference(patient);
107         }
108 
109         // Run down the route
110         return requestBundleProvider(null, searchParameters, httpServletRequest, httpServletResponse);
111     }
112 
113     /**
114      * Handles DocumentReference Retrieve. This is not an actual part of the ITI-67 specification, but in the
115      * context of restful FHIR IHE transaction it makes sense to be able to retrieve a DocumentReference by
116      * its resource ID.
117      *
118      * @param id                  resource ID
119      * @param httpServletRequest  servlet request
120      * @param httpServletResponse servlet response
121      * @return {@link DocumentReference} resource
122      */
123     @SuppressWarnings("unused")
124     @Read(version = true, type = DocumentReference.class)
125     public DocumentReference documentReferenceRetrieve(
126             @IdParam IdType id,
127             HttpServletRequest httpServletRequest,
128             HttpServletResponse httpServletResponse) {
129         if (id == null) throw new InvalidRequestException("Must provide ID with READ request");
130         // Run down the route
131         return requestResource(id, DocumentReference.class, httpServletRequest, httpServletResponse);
132     }
133 }