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  		super();
87  	}
88  
89  
90  	public ResourceIndexedSearchParamQuantity(String theParamName, BigDecimal theValue, String theSystem, String theUnits) {
91  		this();
92  		setParamName(theParamName);
93  		setSystem(theSystem);
94  		setValue(theValue);
95  		setUnits(theUnits);
96  	}
97  
98  	@Override
99  	@PrePersist
100 	public void calculateHashes() {
101 		if (myHashIdentity == null) {
102 			String resourceType = getResourceType();
103 			String paramName = getParamName();
104 			String units = getUnits();
105 			String system = getSystem();
106 			setHashIdentity(calculateHashIdentity(resourceType, paramName));
107 			setHashIdentityAndUnits(calculateHashUnits(resourceType, paramName, units));
108 			setHashIdentitySystemAndUnits(calculateHashSystemAndUnits(resourceType, paramName, system, units));
109 		}
110 	}
111 
112 	@Override
113 	protected void clearHashes() {
114 		myHashIdentity = null;
115 		myHashIdentityAndUnits = null;
116 	}
117 
118 	@Override
119 	public boolean equals(Object theObj) {
120 		if (this == theObj) {
121 			return true;
122 		}
123 		if (theObj == null) {
124 			return false;
125 		}
126 		if (!(theObj instanceof ResourceIndexedSearchParamQuantity)) {
127 			return false;
128 		}
129 		ResourceIndexedSearchParamQuantity obj = (ResourceIndexedSearchParamQuantity) theObj;
130 		EqualsBuilder b = new EqualsBuilder();
131 		b.append(getParamName(), obj.getParamName());
132 		b.append(getResource(), obj.getResource());
133 		b.append(getSystem(), obj.getSystem());
134 		b.append(getUnits(), obj.getUnits());
135 		b.append(getValue(), obj.getValue());
136 		b.append(getHashIdentity(), obj.getHashIdentity());
137 		b.append(getHashIdentitySystemAndUnits(), obj.getHashIdentitySystemAndUnits());
138 		b.append(getHashIdentityAndUnits(), obj.getHashIdentityAndUnits());
139 		return b.isEquals();
140 	}
141 
142 	public Long getHashIdentity() {
143 		calculateHashes();
144 		return myHashIdentity;
145 	}
146 
147 	public void setHashIdentity(Long theHashIdentity) {
148 		myHashIdentity = theHashIdentity;
149 	}
150 
151 	public Long getHashIdentityAndUnits() {
152 		calculateHashes();
153 		return myHashIdentityAndUnits;
154 	}
155 
156 	public void setHashIdentityAndUnits(Long theHashIdentityAndUnits) {
157 		myHashIdentityAndUnits = theHashIdentityAndUnits;
158 	}
159 
160 	private Long getHashIdentitySystemAndUnits() {
161 		return myHashIdentitySystemAndUnits;
162 	}
163 
164 	public void setHashIdentitySystemAndUnits(Long theHashIdentitySystemAndUnits) {
165 		myHashIdentitySystemAndUnits = theHashIdentitySystemAndUnits;
166 	}
167 
168 	@Override
169 	protected Long getId() {
170 		return myId;
171 	}
172 
173 	public String getSystem() {
174 		return mySystem;
175 	}
176 
177 	public void setSystem(String theSystem) {
178 		clearHashes();
179 		mySystem = theSystem;
180 	}
181 
182 	public String getUnits() {
183 		return myUnits;
184 	}
185 
186 	public void setUnits(String theUnits) {
187 		clearHashes();
188 		myUnits = theUnits;
189 	}
190 
191 	public BigDecimal getValue() {
192 		return myValue;
193 	}
194 
195 	public void setValue(BigDecimal theValue) {
196 		clearHashes();
197 		myValue = theValue;
198 	}
199 
200 	@Override
201 	public int hashCode() {
202 		calculateHashes();
203 		HashCodeBuilder b = new HashCodeBuilder();
204 		b.append(getResourceType());
205 		b.append(getParamName());
206 		b.append(getSystem());
207 		b.append(getUnits());
208 		b.append(getValue());
209 		return b.toHashCode();
210 	}
211 
212 	@Override
213 	public IQueryParameterType toQueryParameterType() {
214 		return new QuantityParam(null, getValue(), getSystem(), getUnits());
215 	}
216 
217 	@Override
218 	public String toString() {
219 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
220 		b.append("paramName", getParamName());
221 		b.append("resourceId", getResourcePid());
222 		b.append("system", getSystem());
223 		b.append("units", getUnits());
224 		b.append("value", getValue());
225 		b.append("missing", isMissing());
226 		b.append("hashIdentitySystemAndUnits", myHashIdentitySystemAndUnits);
227 		return b.build();
228 	}
229 
230 	public static long calculateHashSystemAndUnits(String theResourceType, String theParamName, String theSystem, String theUnits) {
231 		return hash(theResourceType, theParamName, theSystem, theUnits);
232 	}
233 
234 	public static long calculateHashUnits(String theResourceType, String theParamName, String theUnits) {
235 		return hash(theResourceType, theParamName, theUnits);
236 	}
237 
238 }