View Javadoc
1   /*
2    * Copyright 2018 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.pcc44;
18  
19  import ca.uhn.fhir.rest.client.api.IGenericClient;
20  import ca.uhn.fhir.rest.gclient.IClientExecutable;
21  import ca.uhn.fhir.rest.gclient.ICriterion;
22  import ca.uhn.fhir.rest.gclient.IQuery;
23  import org.hl7.fhir.dstu3.model.Bundle;
24  import org.hl7.fhir.instance.model.api.IBaseBundle;
25  import org.hl7.fhir.instance.model.api.IBaseResource;
26  import org.openehealth.ipf.commons.ihe.fhir.ClientRequestFactory;
27  import org.openehealth.ipf.commons.ihe.fhir.Constants;
28  
29  import java.util.Map;
30  
31  /**
32   * Request Factory for PCC-44 requests returning a bundle of resources that were queried
33   *
34   * @author Christian Ohr
35   * @since 3.5
36   */
37  public class Pcc44ClientRequestFactory implements ClientRequestFactory<IQuery<Bundle>> {
38  
39      @Override
40      public IClientExecutable<IQuery<Bundle>, ?> getClientExecutable(IGenericClient client, Object requestData, Map<String, Object> parameters) {
41          IQuery<IBaseBundle> query;
42          String queriedResourceType = (String)parameters.get(Constants.FHIR_RESOURCE_TYPE_HEADER);
43          if (requestData instanceof ICriterion) {
44              query = client.search()
45                      .forResource(queriedResourceType)
46                      .where((ICriterion<?>) requestData);
47          } else {
48              query = client.search()
49                      .byUrl(requestData.toString());
50          }
51          if (parameters.containsKey(Constants.FHIR_COUNT)) {
52              query.count(Integer.parseInt(parameters.get(Constants.FHIR_COUNT).toString()));
53          }
54          return query.returnBundle(Bundle.class);
55      }
56  
57  }