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