View Javadoc
1   package ca.uhn.fhir.rest.param;
2   
3   import static org.apache.commons.lang3.StringUtils.defaultString;
4   
5   /*
6    * #%L
7    * HAPI FHIR - Core Library
8    * %%
9    * Copyright (C) 2014 - 2018 University Health Network
10   * %%
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   * 
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   * 
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   * #L%
23   */
24  
25  import ca.uhn.fhir.context.FhirContext;
26  import ca.uhn.fhir.model.api.IQueryParameterType;
27  import ca.uhn.fhir.rest.api.Constants;
28  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
29  
30  /**
31   * Implementation of the _has method parameter
32   */
33  public class HasParam extends BaseParam implements IQueryParameterType {
34  
35  	private static final long serialVersionUID = 1L;
36  	
37  	private String myOwningFieldName;
38  	private String myParameterName;
39  	private String myParameterValue;
40  	private String myTargetResourceType;
41  
42  	public HasParam() {
43  		super();
44  	}
45  
46  
47  	public HasParam(String theTargetResourceType, String theOwningFieldName, String theParameterName, String theParameterValue) {
48  		this();
49  		myTargetResourceType = theTargetResourceType;
50  		myOwningFieldName = theOwningFieldName;
51  		myParameterName = theParameterName;
52  		myParameterValue = theParameterValue;
53  	}
54  
55  
56  	@Override
57  	String doGetQueryParameterQualifier() {
58  		return myTargetResourceType + ':' + myParameterName + ':' + myParameterValue;
59  	}
60  	
61  	@Override
62  	String doGetValueAsQueryToken(FhirContext theContext) {
63  		return myParameterValue;
64  	}
65  
66  	@Override
67  	void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
68  		String qualifier = defaultString(theQualifier);
69  		if (!qualifier.startsWith(":")) {
70  			throwInvalidSyntaxException(Constants.PARAM_HAS + qualifier);
71  		}
72  		int colonIndex0 = qualifier.indexOf(':', 1);
73  		validateColon(qualifier, colonIndex0);
74  		int colonIndex1 = qualifier.indexOf(':', colonIndex0 + 1);
75  		validateColon(qualifier, colonIndex1);
76  		
77  		myTargetResourceType = qualifier.substring(1, colonIndex0);
78  		myOwningFieldName = qualifier.substring(colonIndex0 + 1, colonIndex1);
79  		myParameterName = qualifier.substring(colonIndex1 + 1);
80  		myParameterValue = theValue;
81  	}
82  
83  	public String getOwningFieldName() {
84  		return myOwningFieldName;
85  	}
86  
87  	public String getParameterName() {
88  		return myParameterName;
89  	}
90  
91  	public String getParameterValue() {
92  		return myParameterValue;
93  	}
94  
95  	public String getTargetResourceType() {
96  		return myTargetResourceType;
97  	}
98  
99  	private static void validateColon(String theParameterName, int colonIndex) {
100 		if (colonIndex == -1) {
101 			throwInvalidSyntaxException(theParameterName);
102 		}
103 	}
104 
105 
106 	private static void throwInvalidSyntaxException(String theParameterName) {
107 		throw new InvalidRequestException("Invalid _has parameter syntax: " + theParameterName);
108 	}
109 
110 }