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