View Javadoc
1   package ca.uhn.fhir.jpa.dao;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2017 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  
23  import java.util.ArrayList;
24  import java.util.HashSet;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.apache.commons.lang3.builder.ToStringBuilder;
30  import org.apache.commons.lang3.builder.ToStringStyle;
31  
32  import ca.uhn.fhir.model.api.IQueryParameterAnd;
33  import ca.uhn.fhir.model.api.IQueryParameterOr;
34  import ca.uhn.fhir.model.api.IQueryParameterType;
35  import ca.uhn.fhir.model.api.Include;
36  import ca.uhn.fhir.rest.api.SortSpec;
37  import ca.uhn.fhir.rest.method.RequestDetails;
38  import ca.uhn.fhir.rest.param.DateRangeParam;
39  import ca.uhn.fhir.rest.server.Constants;
40  
41  public class SearchParameterMap extends LinkedHashMap<String, List<List<? extends IQueryParameterType>>> {
42  
43  	private static final long serialVersionUID = 1L;
44  
45  	private Integer myCount;
46  	private EverythingModeEnum myEverythingMode = null;
47  	private Set<Include> myIncludes;
48  	private DateRangeParam myLastUpdated;
49  	private boolean myPersistResults = true;
50  	private RequestDetails myRequestDetails;
51  	private Set<Include> myRevIncludes;
52  	private SortSpec mySort;
53  
54  	public void add(String theName, IQueryParameterAnd<?> theAnd) {
55  		if (theAnd == null) {
56  			return;
57  		}
58  		if (!containsKey(theName)) {
59  			put(theName, new ArrayList<List<? extends IQueryParameterType>>());
60  		}
61  
62  		for (IQueryParameterOr<?> next : theAnd.getValuesAsQueryTokens()) {
63  			if (next == null) {
64  				continue;
65  			}
66  			get(theName).add(next.getValuesAsQueryTokens());
67  		}
68  	}
69  
70  	public void add(String theName, IQueryParameterOr<?> theOr) {
71  		if (theOr == null) {
72  			return;
73  		}
74  		if (!containsKey(theName)) {
75  			put(theName, new ArrayList<List<? extends IQueryParameterType>>());
76  		}
77  
78  		get(theName).add(theOr.getValuesAsQueryTokens());
79  	}
80  
81  	public void add(String theName, IQueryParameterType theParam) {
82  		assert!Constants.PARAM_LASTUPDATED.equals(theName); // this has it's own field in the map
83  
84  		if (theParam == null) {
85  			return;
86  		}
87  		if (!containsKey(theName)) {
88  			put(theName, new ArrayList<List<? extends IQueryParameterType>>());
89  		}
90  		ArrayList<IQueryParameterType> list = new ArrayList<IQueryParameterType>();
91  		list.add(theParam);
92  		get(theName).add(list);
93  	}
94  
95  	public void addInclude(Include theInclude) {
96  		getIncludes().add(theInclude);
97  	}
98  
99  	public void addRevInclude(Include theInclude) {
100 		getRevIncludes().add(theInclude);
101 	}
102 
103 	public Integer getCount() {
104 		return myCount;
105 	}
106 
107 	public EverythingModeEnum getEverythingMode() {
108 		return myEverythingMode;
109 	}
110 
111 	public Set<Include> getIncludes() {
112 		if (myIncludes == null) {
113 			myIncludes = new HashSet<Include>();
114 		}
115 		return myIncludes;
116 	}
117 
118 	/**
119 	 * Returns null if there is no last updated value
120 	 */
121 	public DateRangeParam getLastUpdated() {
122 		if (myLastUpdated != null) {
123 			if (myLastUpdated.isEmpty()) {
124 				myLastUpdated = null;
125 			}
126 		}
127 		return myLastUpdated;
128 	}
129 
130 	/**
131 	 * Returns null if there is no last updated value, and removes the lastupdated
132 	 * value from this map
133 	 */
134 	public DateRangeParam getLastUpdatedAndRemove() {
135 		DateRangeParam retVal = getLastUpdated();
136 		myLastUpdated = null;
137 		return retVal;
138 	}
139 
140 	public RequestDetails getRequestDetails() {
141 		return myRequestDetails;
142 	}
143 
144 	public Set<Include> getRevIncludes() {
145 		if (myRevIncludes == null) {
146 			myRevIncludes = new HashSet<Include>();
147 		}
148 		return myRevIncludes;
149 	}
150 
151 	public SortSpec getSort() {
152 		return mySort;
153 	}
154 
155 	public boolean isPersistResults() {
156 		return myPersistResults;
157 	}
158 
159 	public void setCount(Integer theCount) {
160 		myCount = theCount;
161 	}
162 
163 	public void setEverythingMode(EverythingModeEnum theConsolidateMatches) {
164 		myEverythingMode = theConsolidateMatches;
165 	}
166 
167 	public void setIncludes(Set<Include> theIncludes) {
168 		myIncludes = theIncludes;
169 	}
170 
171 	public void setLastUpdated(DateRangeParam theLastUpdated) {
172 		myLastUpdated = theLastUpdated;
173 	}
174 
175 	/**
176 	 * Should results be persisted into a table for paging
177 	 */
178 	public void setPersistResults(boolean thePersistResults) {
179 		myPersistResults = thePersistResults;
180 	}
181 
182 	public void setRequestDetails(RequestDetails theRequestDetails) {
183 		myRequestDetails = theRequestDetails;
184 	}
185 
186 	public void setRevIncludes(Set<Include> theRevIncludes) {
187 		myRevIncludes = theRevIncludes;
188 	}
189 
190 	public void setSort(SortSpec theSort) {
191 		mySort = theSort;
192 	}
193 
194 	@Override
195 	public String toString() {
196 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
197 		if (isEmpty() == false) {
198 			b.append("params", super.toString());
199 		}
200 		if (getIncludes().isEmpty() == false) {
201 			b.append("includes", getIncludes());
202 		}
203 		return b.toString();
204 	}
205 
206 	public enum EverythingModeEnum {
207 		/*
208 		 * Don't reorder! We rely on the ordinals
209 		 */
210 		ENCOUNTER_INSTANCE(false, true, true), 
211 		ENCOUNTER_TYPE(false, true, false), 
212 		PATIENT_INSTANCE(true, false, true), 
213 		PATIENT_TYPE(true, false, false);
214 
215 		private final boolean myEncounter;
216 
217 		private final boolean myInstance;
218 
219 		private final boolean myPatient;
220 
221 		private EverythingModeEnum(boolean thePatient, boolean theEncounter, boolean theInstance) {
222 			assert thePatient ^ theEncounter;
223 			myPatient = thePatient;
224 			myEncounter = theEncounter;
225 			myInstance = theInstance;
226 		}
227 
228 		public boolean isEncounter() {
229 			return myEncounter;
230 		}
231 		public boolean isInstance() {
232 			return myInstance;
233 		}
234 
235 		public boolean isPatient() {
236 			return myPatient;
237 		}
238 	}
239 
240 }