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 	@Override
112 	@PrePersist
113 	public void calculateHashes() {
114 		if (myHashSystem == null) {
115 			String resourceType = getResourceType();
116 			String paramName = getParamName();
117 			String system = getSystem();
118 			String value = getValue();
119 			setHashIdentity(calculateHashIdentity(resourceType, paramName));
120 			setHashSystem(calculateHashSystem(resourceType, paramName, system));
121 			setHashSystemAndValue(calculateHashSystemAndValue(resourceType, paramName, system, value));
122 			setHashValue(calculateHashValue(resourceType, paramName, value));
123 		}
124 	}
125 
126 	@Override
127 	protected void clearHashes() {
128 		myHashSystem = null;
129 		myHashSystemAndValue = null;
130 		myHashValue = null;
131 	}
132 
133 	@Override
134 	public boolean equals(Object theObj) {
135 		if (this == theObj) {
136 			return true;
137 		}
138 		if (theObj == null) {
139 			return false;
140 		}
141 		if (!(theObj instanceof ResourceIndexedSearchParamToken)) {
142 			return false;
143 		}
144 		ResourceIndexedSearchParamToken obj = (ResourceIndexedSearchParamToken) theObj;
145 		EqualsBuilder b = new EqualsBuilder();
146 		b.append(getParamName(), obj.getParamName());
147 		b.append(getResource(), obj.getResource());
148 		b.append(getSystem(), obj.getSystem());
149 		b.append(getValue(), obj.getValue());
150 		b.append(getHashIdentity(), obj.getHashIdentity());
151 		b.append(getHashSystem(), obj.getHashSystem());
152 		b.append(getHashSystemAndValue(), obj.getHashSystemAndValue());
153 		b.append(getHashValue(), obj.getHashValue());
154 		return b.isEquals();
155 	}
156 
157 	Long getHashSystem() {
158 		calculateHashes();
159 		return myHashSystem;
160 	}
161 
162 	private Long getHashIdentity() {
163 		calculateHashes();
164 		return myHashIdentity;
165 	}
166 
167 	private void setHashIdentity(Long theHashIdentity) {
168 		myHashIdentity = theHashIdentity;
169 	}
170 
171 	private void setHashSystem(Long theHashSystem) {
172 		myHashSystem = theHashSystem;
173 	}
174 
175 	Long getHashSystemAndValue() {
176 		calculateHashes();
177 		return myHashSystemAndValue;
178 	}
179 
180 	private void setHashSystemAndValue(Long theHashSystemAndValue) {
181 		calculateHashes();
182 		myHashSystemAndValue = theHashSystemAndValue;
183 	}
184 
185 	Long getHashValue() {
186 		calculateHashes();
187 		return myHashValue;
188 	}
189 
190 	private void setHashValue(Long theHashValue) {
191 		myHashValue = theHashValue;
192 	}
193 
194 	@Override
195 	protected Long getId() {
196 		return myId;
197 	}
198 
199 	public String getSystem() {
200 		return mySystem;
201 	}
202 
203 	public void setSystem(String theSystem) {
204 		clearHashes();
205 		mySystem = StringUtils.defaultIfBlank(theSystem, null);
206 	}
207 
208 	public String getValue() {
209 		return myValue;
210 	}
211 
212 	public void setValue(String theValue) {
213 		clearHashes();
214 		myValue = StringUtils.defaultIfBlank(theValue, null);
215 	}
216 
217 	@Override
218 	public int hashCode() {
219 		calculateHashes();
220 		HashCodeBuilder b = new HashCodeBuilder();
221 		b.append(getParamName());
222 		b.append(getResource());
223 		b.append(getSystem());
224 		b.append(getValue());
225 		return b.toHashCode();
226 	}
227 
228 	@Override
229 	public IQueryParameterType toQueryParameterType() {
230 		return new TokenParam(getSystem(), getValue());
231 	}
232 
233 	@Override
234 	public String toString() {
235 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
236 		b.append("paramName", getParamName());
237 		b.append("resourceId", getResourcePid());
238 		b.append("system", getSystem());
239 		b.append("value", getValue());
240 		return b.build();
241 	}
242 
243 	public static long calculateHashSystem(String theResourceType, String theParamName, String theSystem) {
244 		return hash(theResourceType, theParamName, trim(theSystem));
245 	}
246 
247 	public static long calculateHashSystemAndValue(String theResourceType, String theParamName, String theSystem, String theValue) {
248 		return hash(theResourceType, theParamName, defaultString(trim(theSystem)), trim(theValue));
249 	}
250 
251 	public static long calculateHashValue(String theResourceType, String theParamName, String theValue) {
252 		return hash(theResourceType, theParamName, trim(theValue));
253 	}
254 }