View Javadoc
1   package ca.uhn.fhir.jpa.provider.dstu3;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import java.util.*;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.hl7.fhir.dstu3.model.*;
27  import org.hl7.fhir.dstu3.model.CapabilityStatement.*;
28  import org.hl7.fhir.dstu3.model.Enumerations.SearchParamType;
29  
30  import ca.uhn.fhir.context.RuntimeResourceDefinition;
31  import ca.uhn.fhir.context.RuntimeSearchParam;
32  import ca.uhn.fhir.jpa.dao.DaoConfig;
33  import ca.uhn.fhir.jpa.dao.IFhirSystemDao;
34  import ca.uhn.fhir.rest.server.RestfulServer;
35  import ca.uhn.fhir.util.CoverageIgnore;
36  import ca.uhn.fhir.util.ExtensionConstants;
37  
38  import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
39  
40  public class JpaConformanceProviderDstu3 extends org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider {
41  
42  	private volatile CapabilityStatement myCachedValue;
43  	private DaoConfig myDaoConfig;
44  	private String myImplementationDescription;
45  	private boolean myIncludeResourceCounts;
46  	private RestfulServer myRestfulServer;
47  	private IFhirSystemDao<Bundle, Meta> mySystemDao;
48  	
49  	/**
50  	 * Constructor
51  	 */
52  	@CoverageIgnore
53  	public JpaConformanceProviderDstu3(){
54  		super();
55  		super.setCache(false);
56  		setIncludeResourceCounts(true);
57  	}
58  	
59  	/**
60  	 * Constructor
61  	 */
62  	public JpaConformanceProviderDstu3(RestfulServer theRestfulServer, IFhirSystemDao<Bundle, Meta> theSystemDao, DaoConfig theDaoConfig) {
63  		super(theRestfulServer);
64  		myRestfulServer = theRestfulServer;
65  		mySystemDao = theSystemDao;
66  		myDaoConfig = theDaoConfig;
67  		super.setCache(false);
68  		setIncludeResourceCounts(true);
69  	}
70  
71  	@Override
72  	public CapabilityStatement getServerConformance(HttpServletRequest theRequest) {
73  		CapabilityStatement retVal = myCachedValue;
74  
75  		Map<String, Long> counts = null;
76  		if (myIncludeResourceCounts) {
77  			counts = mySystemDao.getResourceCountsFromCache();
78  		}
79  		counts = defaultIfNull(counts, Collections.emptyMap());
80  
81  		retVal = super.getServerConformance(theRequest);
82  		for (CapabilityStatementRestComponent nextRest : retVal.getRest()) {
83  
84  			for (CapabilityStatementRestResourceComponent nextResource : nextRest.getResource()) {
85  
86  				nextResource.setVersioning(ResourceVersionPolicy.VERSIONEDUPDATE);
87  				
88  				ConditionalDeleteStatus conditionalDelete = nextResource.getConditionalDelete();
89  				if (conditionalDelete == ConditionalDeleteStatus.MULTIPLE && myDaoConfig.isAllowMultipleDelete() == false) {
90  					nextResource.setConditionalDelete(ConditionalDeleteStatus.SINGLE);
91  				}
92  
93  				// Add resource counts
94  				Long count = counts.get(nextResource.getTypeElement().getValueAsString());
95  				if (count != null) {
96  					nextResource.addExtension(new Extension(ExtensionConstants.CONF_RESOURCE_COUNT, new DecimalType(count)));
97  				}
98  
99  				nextResource.getSearchParam().clear();
100 				String resourceName = nextResource.getType();
101 				RuntimeResourceDefinition resourceDef = myRestfulServer.getFhirContext().getResourceDefinition(resourceName);
102 				Collection<RuntimeSearchParam> searchParams = mySystemDao.getSearchParamsByResourceType(resourceDef);
103 				for (RuntimeSearchParam runtimeSp : searchParams) {
104 					CapabilityStatementRestResourceSearchParamComponent confSp = nextResource.addSearchParam();
105 
106 					confSp.setName(runtimeSp.getName());
107 					confSp.setDocumentation(runtimeSp.getDescription());
108 					confSp.setDefinition(runtimeSp.getUri());
109 					switch (runtimeSp.getParamType()) {
110 					case COMPOSITE:
111 						confSp.setType(SearchParamType.COMPOSITE);
112 						break;
113 					case DATE:
114 						confSp.setType(SearchParamType.DATE);
115 						break;
116 					case NUMBER:
117 						confSp.setType(SearchParamType.NUMBER);
118 						break;
119 					case QUANTITY:
120 						confSp.setType(SearchParamType.QUANTITY);
121 						break;
122 					case REFERENCE:
123 						confSp.setType(SearchParamType.REFERENCE);
124 						break;
125 					case STRING:
126 						confSp.setType(SearchParamType.STRING);
127 						break;
128 					case TOKEN:
129 						confSp.setType(SearchParamType.TOKEN);
130 						break;
131 					case URI:
132 						confSp.setType(SearchParamType.URI);
133 						break;
134 					case HAS:
135 						// Shouldn't happen
136 						break;
137 					}
138 					
139 				}
140 				
141 			}
142 		}
143 
144 		massage(retVal);
145 		
146 		retVal.getImplementation().setDescription(myImplementationDescription);
147 		myCachedValue = retVal;
148 		return retVal;
149 	}
150 
151 	public boolean isIncludeResourceCounts() {
152 		return myIncludeResourceCounts;
153 	}
154 	
155 	/**
156 	 * Subclasses may override
157 	 */
158 	protected void massage(CapabilityStatement theStatement) {
159 		// nothing
160 	}
161 
162 	public void setDaoConfig(DaoConfig myDaoConfig) {
163 		this.myDaoConfig = myDaoConfig;
164 	}
165 
166 	@CoverageIgnore
167 	public void setImplementationDescription(String theImplDesc) {
168 		myImplementationDescription = theImplDesc;
169 	}
170 
171 	public void setIncludeResourceCounts(boolean theIncludeResourceCounts) {
172 		myIncludeResourceCounts = theIncludeResourceCounts;
173 	}
174 
175 	@Override
176 	public void setRestfulServer(RestfulServer theRestfulServer) {
177 		this.myRestfulServer = theRestfulServer;
178 		super.setRestfulServer(theRestfulServer);
179 	}
180 
181 	@CoverageIgnore
182 	public void setSystemDao(IFhirSystemDao<Bundle, Meta> mySystemDao) {
183 		this.mySystemDao = mySystemDao;
184 	}
185 }