View Javadoc
1   package ca.uhn.fhir.jpa.entity;
2   
3   import ca.uhn.fhir.jpa.dao.SearchParameterMap;
4   import ca.uhn.fhir.model.api.Include;
5   import ca.uhn.fhir.rest.param.DateRangeParam;
6   import org.apache.commons.lang3.SerializationUtils;
7   import org.hibernate.annotations.OptimisticLock;
8   
9   import javax.persistence.*;
10  import javax.validation.constraints.NotNull;
11  import java.io.Serializable;
12  import java.util.*;
13  
14  import static org.apache.commons.lang3.StringUtils.left;
15  
16  /*
17   * #%L
18   * HAPI FHIR JPA Server
19   * %%
20   * Copyright (C) 2014 - 2018 University Health Network
21   * %%
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   * 
26   *      http://www.apache.org/licenses/LICENSE-2.0
27   * 
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   * #L%
34   */
35  
36  @Entity
37  @Table(name = "HFJ_SEARCH", uniqueConstraints = {
38  	@UniqueConstraint(name = "IDX_SEARCH_UUID", columnNames = "SEARCH_UUID")
39  }, indexes = {
40  	@Index(name = "IDX_SEARCH_LASTRETURNED", columnList = "SEARCH_LAST_RETURNED"),
41  	@Index(name = "IDX_SEARCH_RESTYPE_HASHS", columnList = "RESOURCE_TYPE,SEARCH_QUERY_STRING_HASH,CREATED")
42  })
43  public class Search implements Serializable {
44  
45  	@SuppressWarnings("WeakerAccess")
46  	public static final int UUID_COLUMN_LENGTH = 36;
47  	private static final int MAX_SEARCH_QUERY_STRING = 10000;
48  	private static final int FAILURE_MESSAGE_LENGTH = 500;
49  	private static final long serialVersionUID = 1L;
50  	@Temporal(TemporalType.TIMESTAMP)
51  	@Column(name = "CREATED", nullable = false, updatable = false)
52  	private Date myCreated;
53  	@OptimisticLock(excluded = true)
54  	@Column(name = "SEARCH_DELETED", nullable = true)
55  	private Boolean myDeleted;
56  	@Column(name = "FAILURE_CODE", nullable = true)
57  	private Integer myFailureCode;
58  	@Column(name = "FAILURE_MESSAGE", length = FAILURE_MESSAGE_LENGTH, nullable = true)
59  	private String myFailureMessage;
60  	@Id
61  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SEARCH")
62  	@SequenceGenerator(name = "SEQ_SEARCH", sequenceName = "SEQ_SEARCH")
63  	@Column(name = "PID")
64  	private Long myId;
65  	@OneToMany(mappedBy = "mySearch")
66  	private Collection<SearchInclude> myIncludes;
67  	@Temporal(TemporalType.TIMESTAMP)
68  	@Column(name = "LAST_UPDATED_HIGH", nullable = true, insertable = true, updatable = false)
69  	private Date myLastUpdatedHigh;
70  	@Temporal(TemporalType.TIMESTAMP)
71  	@Column(name = "LAST_UPDATED_LOW", nullable = true, insertable = true, updatable = false)
72  	private Date myLastUpdatedLow;
73  	@Column(name = "NUM_FOUND", nullable = false)
74  	private int myNumFound;
75  	@Column(name = "PREFERRED_PAGE_SIZE", nullable = true)
76  	private Integer myPreferredPageSize;
77  	@Column(name = "RESOURCE_ID", nullable = true)
78  	private Long myResourceId;
79  	@Column(name = "RESOURCE_TYPE", length = 200, nullable = true)
80  	private String myResourceType;
81  	@OneToMany(mappedBy = "mySearch", fetch = FetchType.LAZY)
82  	private Collection<SearchResult> myResults;
83  	@NotNull
84  	@Temporal(TemporalType.TIMESTAMP)
85  	@Column(name = "SEARCH_LAST_RETURNED", nullable = false, updatable = false)
86  	@OptimisticLock(excluded = true)
87  	private Date mySearchLastReturned;
88  	@Lob()
89  	@Basic(fetch = FetchType.LAZY)
90  	@Column(name = "SEARCH_QUERY_STRING", nullable = true, updatable = false, length = MAX_SEARCH_QUERY_STRING)
91  	private String mySearchQueryString;
92  	@Column(name = "SEARCH_QUERY_STRING_HASH", nullable = true, updatable = false)
93  	private Integer mySearchQueryStringHash;
94  	@Enumerated(EnumType.ORDINAL)
95  	@Column(name = "SEARCH_TYPE", nullable = false)
96  	private SearchTypeEnum mySearchType;
97  	@Enumerated(EnumType.STRING)
98  	@Column(name = "SEARCH_STATUS", nullable = false, length = 10)
99  	private SearchStatusEnum myStatus;
100 	@Column(name = "TOTAL_COUNT", nullable = true)
101 	private Integer myTotalCount;
102 	@Column(name = "SEARCH_UUID", length = UUID_COLUMN_LENGTH, nullable = false, updatable = false)
103 	private String myUuid;
104 	@SuppressWarnings("unused")
105 	@Version
106 	@Column(name = "OPTLOCK_VERSION", nullable = true)
107 	private Integer myVersion;
108 	@Lob
109 	@Column(name = "SEARCH_PARAM_MAP", nullable = true)
110 	private byte[] mySearchParameterMap;
111 
112 	/**
113 	 * Constructor
114 	 */
115 	public Search() {
116 		super();
117 	}
118 
119 	public Boolean getDeleted() {
120 		return myDeleted;
121 	}
122 
123 	public void setDeleted(Boolean theDeleted) {
124 		myDeleted = theDeleted;
125 	}
126 
127 	public Date getCreated() {
128 		return myCreated;
129 	}
130 
131 	public void setCreated(Date theCreated) {
132 		myCreated = theCreated;
133 	}
134 
135 	public Integer getFailureCode() {
136 		return myFailureCode;
137 	}
138 
139 	public void setFailureCode(Integer theFailureCode) {
140 		myFailureCode = theFailureCode;
141 	}
142 
143 	public String getFailureMessage() {
144 		return myFailureMessage;
145 	}
146 
147 	public void setFailureMessage(String theFailureMessage) {
148 		myFailureMessage = left(theFailureMessage, FAILURE_MESSAGE_LENGTH);
149 	}
150 
151 	public Long getId() {
152 		return myId;
153 	}
154 
155 	public Collection<SearchInclude> getIncludes() {
156 		if (myIncludes == null) {
157 			myIncludes = new ArrayList<>();
158 		}
159 		return myIncludes;
160 	}
161 
162 	public DateRangeParam getLastUpdated() {
163 		if (myLastUpdatedLow == null && myLastUpdatedHigh == null) {
164 			return null;
165 		} else {
166 			return new DateRangeParam(myLastUpdatedLow, myLastUpdatedHigh);
167 		}
168 	}
169 
170 	public void setLastUpdated(DateRangeParam theLastUpdated) {
171 		if (theLastUpdated == null) {
172 			myLastUpdatedLow = null;
173 			myLastUpdatedHigh = null;
174 		} else {
175 			myLastUpdatedLow = theLastUpdated.getLowerBoundAsInstant();
176 			myLastUpdatedHigh = theLastUpdated.getUpperBoundAsInstant();
177 		}
178 	}
179 
180 	public Date getLastUpdatedHigh() {
181 		return myLastUpdatedHigh;
182 	}
183 
184 	public Date getLastUpdatedLow() {
185 		return myLastUpdatedLow;
186 	}
187 
188 	public int getNumFound() {
189 		return myNumFound;
190 	}
191 
192 	public void setNumFound(int theNumFound) {
193 		myNumFound = theNumFound;
194 	}
195 
196 	public Integer getPreferredPageSize() {
197 		return myPreferredPageSize;
198 	}
199 
200 	public void setPreferredPageSize(Integer thePreferredPageSize) {
201 		myPreferredPageSize = thePreferredPageSize;
202 	}
203 
204 	public Long getResourceId() {
205 		return myResourceId;
206 	}
207 
208 	public void setResourceId(Long theResourceId) {
209 		myResourceId = theResourceId;
210 	}
211 
212 	public String getResourceType() {
213 		return myResourceType;
214 	}
215 
216 	public void setResourceType(String theResourceType) {
217 		myResourceType = theResourceType;
218 	}
219 
220 	public Date getSearchLastReturned() {
221 		return mySearchLastReturned;
222 	}
223 
224 	public void setSearchLastReturned(Date theDate) {
225 		mySearchLastReturned = theDate;
226 	}
227 
228 	public String getSearchQueryString() {
229 		return mySearchQueryString;
230 	}
231 
232 	public void setSearchQueryString(String theSearchQueryString) {
233 		if (theSearchQueryString != null && theSearchQueryString.length() > MAX_SEARCH_QUERY_STRING) {
234 			mySearchQueryString = null;
235 		} else {
236 			mySearchQueryString = theSearchQueryString;
237 		}
238 	}
239 
240 	public SearchTypeEnum getSearchType() {
241 		return mySearchType;
242 	}
243 
244 	public void setSearchType(SearchTypeEnum theSearchType) {
245 		mySearchType = theSearchType;
246 	}
247 
248 	public SearchStatusEnum getStatus() {
249 		return myStatus;
250 	}
251 
252 	public void setStatus(SearchStatusEnum theStatus) {
253 		myStatus = theStatus;
254 	}
255 
256 	public Integer getTotalCount() {
257 		return myTotalCount;
258 	}
259 
260 	public void setTotalCount(Integer theTotalCount) {
261 		myTotalCount = theTotalCount;
262 	}
263 
264 	public String getUuid() {
265 		return myUuid;
266 	}
267 
268 	public void setUuid(String theUuid) {
269 		myUuid = theUuid;
270 	}
271 
272 	public void setLastUpdated(Date theLowerBound, Date theUpperBound) {
273 		myLastUpdatedLow = theLowerBound;
274 		myLastUpdatedHigh = theUpperBound;
275 	}
276 
277 	public void setSearchQueryStringHash(Integer theSearchQueryStringHash) {
278 		mySearchQueryStringHash = theSearchQueryStringHash;
279 	}
280 
281 	private Set<Include> toIncList(boolean theWantReverse) {
282 		HashSet<Include> retVal = new HashSet<>();
283 		for (SearchInclude next : getIncludes()) {
284 			if (theWantReverse == next.isReverse()) {
285 				retVal.add(new Include(next.getInclude(), next.isRecurse()));
286 			}
287 		}
288 		return Collections.unmodifiableSet(retVal);
289 	}
290 
291 	public Set<Include> toIncludesList() {
292 		return toIncList(false);
293 	}
294 
295 	public Set<Include> toRevIncludesList() {
296 		return toIncList(true);
297 	}
298 
299 	public void addInclude(SearchInclude theInclude) {
300 		getIncludes().add(theInclude);
301 	}
302 
303 	public Integer getVersion() {
304 		return myVersion;
305 	}
306 
307 	public SearchParameterMap getSearchParameterMap() {
308 		return SerializationUtils.deserialize(mySearchParameterMap);
309 	}
310 
311 	public void setSearchParameterMap(SearchParameterMap theSearchParameterMap) {
312 		mySearchParameterMap = SerializationUtils.serialize(theSearchParameterMap);
313 	}
314 }