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.jpa.util.BigDecimalNumericFieldBridge;
24  import ca.uhn.fhir.model.api.IQueryParameterType;
25  import ca.uhn.fhir.rest.param.QuantityParam;
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  import org.hibernate.search.annotations.FieldBridge;
32  import org.hibernate.search.annotations.NumericField;
33  
34  import javax.persistence.*;
35  import java.math.BigDecimal;
36  
37  //@formatter:off
38  @Embeddable
39  @Entity
40  @Table(name = "HFJ_SPIDX_QUANTITY", indexes = {
41  //	We used to have an index named IDX_SP_QUANTITY - Dont reuse
42  	@Index(name = "IDX_SP_QUANTITY_HASH", columnList = "HASH_IDENTITY,SP_VALUE"),
43  	@Index(name = "IDX_SP_QUANTITY_HASH_UN", columnList = "HASH_IDENTITY_AND_UNITS,SP_VALUE"),
44  	@Index(name = "IDX_SP_QUANTITY_HASH_SYSUN", columnList = "HASH_IDENTITY_SYS_UNITS,SP_VALUE"),
45  	@Index(name = "IDX_SP_QUANTITY_UPDATED", columnList = "SP_UPDATED"),
46  	@Index(name = "IDX_SP_QUANTITY_RESID", columnList = "RES_ID")
47  })
48  public class ResourceIndexedSearchParamQuantity extends BaseResourceIndexedSearchParam {
49  
50  	private static final int MAX_LENGTH = 200;
51  
52  	private static final long serialVersionUID = 1L;
53  	@Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
54  	@Field
55  	public String mySystem;
56  	@Column(name = "SP_UNITS", nullable = true, length = MAX_LENGTH)
57  	@Field
58  	public String myUnits;
59  	@Column(name = "SP_VALUE", nullable = true)
60  	@Field
61  	@NumericField
62  	@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
63  	public BigDecimal myValue;
64  	@Id
65  	@SequenceGenerator(name = "SEQ_SPIDX_QUANTITY", sequenceName = "SEQ_SPIDX_QUANTITY")
66  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_QUANTITY")
67  	@Column(name = "SP_ID")
68  	private Long myId;
69  	/**
70  	 * @since 3.5.0 - At some point this should be made not-null
71  	 */
72  	@Column(name = "HASH_IDENTITY_AND_UNITS", nullable = true)
73  	private Long myHashIdentityAndUnits;
74  	/**
75  	 * @since 3.5.0 - At some point this should be made not-null
76  	 */
77  	@Column(name = "HASH_IDENTITY_SYS_UNITS", nullable = true)
78  	private Long myHashIdentitySystemAndUnits;
79  	/**
80  	 * @since 3.5.0 - At some point this should be made not-null
81  	 */
82  	@Column(name = "HASH_IDENTITY", nullable = true)
83  	private Long myHashIdentity;
84  
85  	public ResourceIndexedSearchParamQuantity() {
86  		// nothing
87  	}
88  
89  
90  	public ResourceIndexedSearchParamQuantity(String theParamName, BigDecimal theValue, String theSystem, String theUnits) {
91  		setParamName(theParamName);
92  		setSystem(theSystem);
93  		setValue(theValue);
94  		setUnits(theUnits);
95  	}
96  
97  	@PrePersist
98  	public void calculateHashes() {
99  		if (myHashIdentity == null) {
100 			String resourceType = getResourceType();
101 			String paramName = getParamName();
102 			String units = getUnits();
103 			String system = getSystem();
104 			setHashIdentity(calculateHashIdentity(resourceType, paramName));
105 			setHashIdentityAndUnits(calculateHashUnits(resourceType, paramName, units));
106 			setHashIdentitySystemAndUnits(calculateHashSystemAndUnits(resourceType, paramName, system, units));
107 		}
108 	}
109 
110 	@Override
111 	protected void clearHashes() {
112 		myHashIdentity = null;
113 		myHashIdentityAndUnits = null;
114 	}
115 
116 	@Override
117 	public boolean equals(Object theObj) {
118 		if (this == theObj) {
119 			return true;
120 		}
121 		if (theObj == null) {
122 			return false;
123 		}
124 		if (!(theObj instanceof ResourceIndexedSearchParamQuantity)) {
125 			return false;
126 		}
127 		ResourceIndexedSearchParamQuantity obj = (ResourceIndexedSearchParamQuantity) theObj;
128 		EqualsBuilder b = new EqualsBuilder();
129 		b.append(getParamName(), obj.getParamName());
130 		b.append(getResource(), obj.getResource());
131 		b.append(getSystem(), obj.getSystem());
132 		b.append(getUnits(), obj.getUnits());
133 		b.append(getValue(), obj.getValue());
134 		b.append(getHashIdentity(), obj.getHashIdentity());
135 		b.append(getHashIdentitySystemAndUnits(), obj.getHashIdentitySystemAndUnits());
136 		b.append(getHashIdentityAndUnits(), obj.getHashIdentityAndUnits());
137 		return b.isEquals();
138 	}
139 
140 	public Long getHashIdentity() {
141 		calculateHashes();
142 		return myHashIdentity;
143 	}
144 
145 	public void setHashIdentity(Long theHashIdentity) {
146 		myHashIdentity = theHashIdentity;
147 	}
148 
149 	public Long getHashIdentityAndUnits() {
150 		calculateHashes();
151 		return myHashIdentityAndUnits;
152 	}
153 
154 	public void setHashIdentityAndUnits(Long theHashIdentityAndUnits) {
155 		myHashIdentityAndUnits = theHashIdentityAndUnits;
156 	}
157 
158 	private Long getHashIdentitySystemAndUnits() {
159 		return myHashIdentitySystemAndUnits;
160 	}
161 
162 	public void setHashIdentitySystemAndUnits(Long theHashIdentitySystemAndUnits) {
163 		myHashIdentitySystemAndUnits = theHashIdentitySystemAndUnits;
164 	}
165 
166 	@Override
167 	protected Long getId() {
168 		return myId;
169 	}
170 
171 	public String getSystem() {
172 		return mySystem;
173 	}
174 
175 	public void setSystem(String theSystem) {
176 		clearHashes();
177 		mySystem = theSystem;
178 	}
179 
180 	public String getUnits() {
181 		return myUnits;
182 	}
183 
184 	public void setUnits(String theUnits) {
185 		clearHashes();
186 		myUnits = theUnits;
187 	}
188 
189 	public BigDecimal getValue() {
190 		return myValue;
191 	}
192 
193 	public void setValue(BigDecimal theValue) {
194 		clearHashes();
195 		myValue = theValue;
196 	}
197 
198 	@Override
199 	public int hashCode() {
200 		calculateHashes();
201 		HashCodeBuilder b = new HashCodeBuilder();
202 		b.append(getResourceType());
203 		b.append(getParamName());
204 		b.append(getSystem());
205 		b.append(getUnits());
206 		b.append(getValue());
207 		return b.toHashCode();
208 	}
209 
210 	@Override
211 	public IQueryParameterType toQueryParameterType() {
212 		return new QuantityParam(null, getValue(), getSystem(), getUnits());
213 	}
214 
215 	@Override
216 	public String toString() {
217 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
218 		b.append("paramName", getParamName());
219 		b.append("resourceId", getResourcePid());
220 		b.append("system", getSystem());
221 		b.append("units", getUnits());
222 		b.append("value", getValue());
223 		b.append("missing", isMissing());
224 		b.append("hashIdentitySystemAndUnits", myHashIdentitySystemAndUnits);
225 		return b.build();
226 	}
227 
228 	public static long calculateHashSystemAndUnits(String theResourceType, String theParamName, String theSystem, String theUnits) {
229 		return hash(theResourceType, theParamName, theSystem, theUnits);
230 	}
231 
232 	public static long calculateHashUnits(String theResourceType, String theParamName, String theUnits) {
233 		return hash(theResourceType, theParamName, theUnits);
234 	}
235 
236 }