View Javadoc
1   package ca.uhn.fhir.jpa.entity;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 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 ca.uhn.fhir.model.api.IQueryParameterType;
24  import ca.uhn.fhir.rest.param.TokenParam;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.lang3.builder.EqualsBuilder;
27  import org.apache.commons.lang3.builder.HashCodeBuilder;
28  import org.apache.commons.lang3.builder.ToStringBuilder;
29  import org.apache.commons.lang3.builder.ToStringStyle;
30  import org.hibernate.search.annotations.Field;
31  
32  import javax.persistence.*;
33  
34  import static org.apache.commons.lang3.StringUtils.defaultString;
35  import static org.apache.commons.lang3.StringUtils.trim;
36  
37  @Embeddable
38  @Entity
39  @Table(name = "HFJ_SPIDX_TOKEN", indexes = {
40  	/*
41  	 * Note: We previously had indexes with the following names,
42  	 * do not reuse these names:
43  	 * IDX_SP_TOKEN
44  	 * IDX_SP_TOKEN_UNQUAL
45  	 */
46  	@Index(name = "IDX_SP_TOKEN_HASH", columnList = "HASH_IDENTITY"),
47  	@Index(name = "IDX_SP_TOKEN_HASH_S", columnList = "HASH_SYS"),
48  	@Index(name = "IDX_SP_TOKEN_HASH_SV", columnList = "HASH_SYS_AND_VALUE"),
49  	@Index(name = "IDX_SP_TOKEN_HASH_V", columnList = "HASH_VALUE"),
50  
51  	@Index(name = "IDX_SP_TOKEN_UPDATED", columnList = "SP_UPDATED"),
52  	@Index(name = "IDX_SP_TOKEN_RESID", columnList = "RES_ID")
53  })
54  public class ResourceIndexedSearchParamToken extends BaseResourceIndexedSearchParam {
55  
56  	public static final int MAX_LENGTH = 200;
57  
58  	private static final long serialVersionUID = 1L;
59  
60  	@Field()
61  	@Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
62  	public String mySystem;
63  	@Field()
64  	@Column(name = "SP_VALUE", nullable = true, length = MAX_LENGTH)
65  	private String myValue;
66  	@SuppressWarnings("unused")
67  	@Id
68  	@SequenceGenerator(name = "SEQ_SPIDX_TOKEN", sequenceName = "SEQ_SPIDX_TOKEN")
69  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_TOKEN")
70  	@Column(name = "SP_ID")
71  	private Long myId;
72  	/**
73  	 * @since 3.4.0 - At some point this should be made not-null
74  	 */
75  	@Column(name = "HASH_IDENTITY", nullable = true)
76  	private Long myHashIdentity;
77  	/**
78  	 * @since 3.4.0 - At some point this should be made not-null
79  	 */
80  	@Column(name = "HASH_SYS", nullable = true)
81  	private Long myHashSystem;
82  	/**
83  	 * @since 3.4.0 - At some point this should be made not-null
84  	 */
85  	@Column(name = "HASH_SYS_AND_VALUE", nullable = true)
86  	private Long myHashSystemAndValue;
87  	/**
88  	 * @since 3.4.0 - At some point this should be made not-null
89  	 */
90  	@Column(name = "HASH_VALUE", nullable = true)
91  	private Long myHashValue;
92  
93  
94  	/**
95  	 * Constructor
96  	 */
97  	public ResourceIndexedSearchParamToken() {
98  		super();
99  	}
100 
101 	/**
102 	 * Constructor
103 	 */
104 	public ResourceIndexedSearchParamToken(String theName, String theSystem, String theValue) {
105 		super();
106 		setParamName(theName);
107 		setSystem(theSystem);
108 		setValue(theValue);
109 	}
110 
111 	@PrePersist
112 	public void calculateHashes() {
113 		if (myHashSystem == null) {
114 			String resourceType = getResourceType();
115 			String paramName = getParamName();
116 			String system = getSystem();
117 			String value = getValue();
118 			setHashIdentity(calculateHashIdentity(resourceType, paramName));
119 			setHashSystem(calculateHashSystem(resourceType, paramName, system));
120 			setHashSystemAndValue(calculateHashSystemAndValue(resourceType, paramName, system, value));
121 			setHashValue(calculateHashValue(resourceType, paramName, value));
122 		}
123 	}
124 
125 	@Override
126 	protected void clearHashes() {
127 		myHashSystem = null;
128 		myHashSystemAndValue = null;
129 		myHashValue = null;
130 	}
131 
132 	@Override
133 	public boolean equals(Object theObj) {
134 		if (this == theObj) {
135 			return true;
136 		}
137 		if (theObj == null) {
138 			return false;
139 		}
140 		if (!(theObj instanceof ResourceIndexedSearchParamToken)) {
141 			return false;
142 		}
143 		ResourceIndexedSearchParamToken obj = (ResourceIndexedSearchParamToken) theObj;
144 		EqualsBuilder b = new EqualsBuilder();
145 		b.append(getParamName(), obj.getParamName());
146 		b.append(getResource(), obj.getResource());
147 		b.append(getSystem(), obj.getSystem());
148 		b.append(getValue(), obj.getValue());
149 		b.append(getHashIdentity(), obj.getHashIdentity());
150 		b.append(getHashSystem(), obj.getHashSystem());
151 		b.append(getHashSystemAndValue(), obj.getHashSystemAndValue());
152 		b.append(getHashValue(), obj.getHashValue());
153 		return b.isEquals();
154 	}
155 
156 	Long getHashSystem() {
157 		calculateHashes();
158 		return myHashSystem;
159 	}
160 
161 	private Long getHashIdentity() {
162 		calculateHashes();
163 		return myHashIdentity;
164 	}
165 
166 	private void setHashIdentity(Long theHashIdentity) {
167 		myHashIdentity = theHashIdentity;
168 	}
169 
170 	private void setHashSystem(Long theHashSystem) {
171 		myHashSystem = theHashSystem;
172 	}
173 
174 	Long getHashSystemAndValue() {
175 		calculateHashes();
176 		return myHashSystemAndValue;
177 	}
178 
179 	private void setHashSystemAndValue(Long theHashSystemAndValue) {
180 		calculateHashes();
181 		myHashSystemAndValue = theHashSystemAndValue;
182 	}
183 
184 	Long getHashValue() {
185 		calculateHashes();
186 		return myHashValue;
187 	}
188 
189 	private void setHashValue(Long theHashValue) {
190 		myHashValue = theHashValue;
191 	}
192 
193 	@Override
194 	protected Long getId() {
195 		return myId;
196 	}
197 
198 	public String getSystem() {
199 		return mySystem;
200 	}
201 
202 	public void setSystem(String theSystem) {
203 		clearHashes();
204 		mySystem = StringUtils.defaultIfBlank(theSystem, null);
205 	}
206 
207 	public String getValue() {
208 		return myValue;
209 	}
210 
211 	public void setValue(String theValue) {
212 		clearHashes();
213 		myValue = StringUtils.defaultIfBlank(theValue, null);
214 	}
215 
216 	@Override
217 	public int hashCode() {
218 		calculateHashes();
219 		HashCodeBuilder b = new HashCodeBuilder();
220 		b.append(getParamName());
221 		b.append(getResource());
222 		b.append(getSystem());
223 		b.append(getValue());
224 		return b.toHashCode();
225 	}
226 
227 	@Override
228 	public IQueryParameterType toQueryParameterType() {
229 		return new TokenParam(getSystem(), getValue());
230 	}
231 
232 	@Override
233 	public String toString() {
234 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
235 		b.append("paramName", getParamName());
236 		b.append("resourceId", getResourcePid());
237 		b.append("system", getSystem());
238 		b.append("value", getValue());
239 		return b.build();
240 	}
241 
242 	public static long calculateHashSystem(String theResourceType, String theParamName, String theSystem) {
243 		return hash(theResourceType, theParamName, trim(theSystem));
244 	}
245 
246 	public static long calculateHashSystemAndValue(String theResourceType, String theParamName, String theSystem, String theValue) {
247 		return hash(theResourceType, theParamName, defaultString(trim(theSystem)), trim(theValue));
248 	}
249 
250 	public static long calculateHashValue(String theResourceType, String theParamName, String theValue) {
251 		return hash(theResourceType, theParamName, trim(theValue));
252 	}
253 }