View Javadoc
1   package ca.uhn.fhir.jpa.dao.r4;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import ca.uhn.fhir.jpa.dao.BaseSearchParamExtractor;
5   import ca.uhn.fhir.jpa.dao.IFhirResourceDaoSearchParameter;
6   import ca.uhn.fhir.jpa.dao.IFhirSystemDao;
7   import ca.uhn.fhir.jpa.entity.ResourceTable;
8   import ca.uhn.fhir.parser.DataFormatException;
9   import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
10  import ca.uhn.fhir.util.ElementUtil;
11  import org.hl7.fhir.instance.model.api.IPrimitiveType;
12  import org.hl7.fhir.r4.model.*;
13  import org.springframework.beans.factory.annotation.Autowired;
14  
15  import java.util.List;
16  
17  import static org.apache.commons.lang3.StringUtils.isBlank;
18  
19  /*
20   * #%L
21   * HAPI FHIR JPA Server
22   * %%
23   * Copyright (C) 2014 - 2018 University Health Network
24   * %%
25   * Licensed under the Apache License, Version 2.0 (the "License");
26   * you may not use this file except in compliance with the License.
27   * You may obtain a copy of the License at
28   * 
29   * http://www.apache.org/licenses/LICENSE-2.0
30   * 
31   * Unless required by applicable law or agreed to in writing, software
32   * distributed under the License is distributed on an "AS IS" BASIS,
33   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34   * See the License for the specific language governing permissions and
35   * limitations under the License.
36   * #L%
37   */
38  
39  public class FhirResourceDaoSearchParameterR4 extends FhirResourceDaoR4<SearchParameter> implements IFhirResourceDaoSearchParameter<SearchParameter> {
40  
41  	@Autowired
42  	private IFhirSystemDao<Bundle, Meta> mySystemDao;
43  
44  	protected void markAffectedResources(SearchParameter theResource) {
45  		Boolean reindex = theResource != null ? CURRENTLY_REINDEXING.get(theResource) : null;
46  		String expression = theResource != null ? theResource.getExpression() : null;
47  		markResourcesMatchingExpressionAsNeedingReindexing(reindex, expression);
48  	}
49  
50  
51  	@Override
52  	protected void postPersist(ResourceTable theEntity, SearchParameter theResource) {
53  		super.postPersist(theEntity, theResource);
54  		markAffectedResources(theResource);
55  	}
56  
57  	@Override
58  	protected void postUpdate(ResourceTable theEntity, SearchParameter theResource) {
59  		super.postUpdate(theEntity, theResource);
60  		markAffectedResources(theResource);
61  	}
62  
63  	@Override
64  	protected void preDelete(SearchParameter theResourceToDelete, ResourceTable theEntityToDelete) {
65  		super.preDelete(theResourceToDelete, theEntityToDelete);
66  		markAffectedResources(theResourceToDelete);
67  	}
68  
69  	@Override
70  	protected void validateResourceForStorage(SearchParameter theResource, ResourceTable theEntityToSave) {
71  		super.validateResourceForStorage(theResource, theEntityToSave);
72  
73  		Enum<?> status = theResource.getStatus();
74  		List<CodeType> base = theResource.getBase();
75  		String expression = theResource.getExpression();
76  		FhirContext context = getContext();
77  		Enum<?> type = theResource.getType();
78  
79  		FhirResourceDaoSearchParameterR4.validateSearchParam(type, status, base, expression, context);
80  	}
81  
82  	public static void validateSearchParam(Enum<?> theType, Enum<?> theStatus, List<? extends IPrimitiveType> theBase, String theExpression, FhirContext theContext) {
83  		if (theStatus == null) {
84  			throw new UnprocessableEntityException("SearchParameter.status is missing or invalid");
85  		}
86  
87  		if (ElementUtil.isEmpty(theBase)) {
88  			throw new UnprocessableEntityException("SearchParameter.base is missing");
89  		}
90  
91  		if (theType != null && theType.name().equals(Enumerations.SearchParamType.COMPOSITE.name()) && isBlank(theExpression)) {
92  
93  			// this is ok
94  
95  		} else if (isBlank(theExpression)) {
96  
97  			throw new UnprocessableEntityException("SearchParameter.expression is missing");
98  
99  		} else {
100 
101 			theExpression = theExpression.trim();
102 
103 			String[] expressionSplit = BaseSearchParamExtractor.SPLIT.split(theExpression);
104 			for (String nextPath : expressionSplit) {
105 				nextPath = nextPath.trim();
106 
107 				int dotIdx = nextPath.indexOf('.');
108 				if (dotIdx == -1) {
109 					throw new UnprocessableEntityException("Invalid SearchParameter.expression value \"" + nextPath + "\". Must start with a resource name");
110 				}
111 
112 				String resourceName = nextPath.substring(0, dotIdx);
113 				try {
114 					theContext.getResourceDefinition(resourceName);
115 				} catch (DataFormatException e) {
116 					throw new UnprocessableEntityException("Invalid SearchParameter.expression value \"" + nextPath + "\": " + e.getMessage());
117 				}
118 
119 			}
120 
121 		} // if have expression
122 	}
123 
124 }