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  import java.math.RoundingMode;
37  
38  //@formatter:off
39  @Embeddable
40  @Entity
41  @Table(name = "HFJ_SPIDX_QUANTITY", indexes = {
42  	@Index(name = "IDX_SP_QUANTITY", columnList = "RES_TYPE,SP_NAME,SP_SYSTEM,SP_UNITS,SP_VALUE"),
43  	@Index(name = "IDX_SP_QUANTITY_UPDATED", columnList = "SP_UPDATED"),
44  	@Index(name = "IDX_SP_QUANTITY_RESID", columnList = "RES_ID")
45  })
46  //@formatter:on
47  public class ResourceIndexedSearchParamQuantity extends BaseResourceIndexedSearchParam {
48  
49  	private static final int MAX_LENGTH = 200;
50  
51  	private static final long serialVersionUID = 1L;
52  	@Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
53  	@Field
54  	public String mySystem;
55  	@Column(name = "SP_UNITS", nullable = true, length = MAX_LENGTH)
56  	@Field
57  	public String myUnits;
58  	@Column(name = "SP_VALUE", nullable = true)
59  	@Field
60  	@NumericField
61  	@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
62  	public BigDecimal myValue;
63  	@Id
64  	@SequenceGenerator(name = "SEQ_SPIDX_QUANTITY", sequenceName = "SEQ_SPIDX_QUANTITY")
65  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_QUANTITY")
66  	@Column(name = "SP_ID")
67  	private Long myId;
68  	/**
69  	 * @since 3.4.0 - At some point this should be made not-null
70  	 */
71  	@Column(name = "HASH_UNITS_AND_VALPREFIX", nullable = true)
72  	private Long myHashUnitsAndValPrefix;
73  	/**
74  	 * @since 3.4.0 - At some point this should be made not-null
75  	 */
76  	@Column(name = "HASH_VALPREFIX", nullable = true)
77  	private Long myHashValPrefix;
78  
79  	public ResourceIndexedSearchParamQuantity() {
80  		// nothing
81  	}
82  
83  	public ResourceIndexedSearchParamQuantity(String theParamName, BigDecimal theValue, String theSystem, String theUnits) {
84  		setParamName(theParamName);
85  		setSystem(theSystem);
86  		setValue(theValue);
87  		setUnits(theUnits);
88  	}
89  
90  	@PrePersist
91  	public void calculateHashes() {
92  		if (myHashUnitsAndValPrefix == null) {
93  			setHashUnitsAndValPrefix(hash(getResourceType(), getParamName(), getSystem(), getUnits(), toTruncatedString(getValue())));
94  			setHashValPrefix(hash(getResourceType(), getParamName(), toTruncatedString(getValue())));
95  		}
96  	}
97  
98  	@Override
99  	protected void clearHashes() {
100 		myHashUnitsAndValPrefix = null;
101 		myHashValPrefix = null;
102 	}
103 
104 	@Override
105 	public boolean equals(Object theObj) {
106 		if (this == theObj) {
107 			return true;
108 		}
109 		if (theObj == null) {
110 			return false;
111 		}
112 		if (!(theObj instanceof ResourceIndexedSearchParamQuantity)) {
113 			return false;
114 		}
115 		ResourceIndexedSearchParamQuantity obj = (ResourceIndexedSearchParamQuantity) theObj;
116 		EqualsBuilder b = new EqualsBuilder();
117 		b.append(getParamName(), obj.getParamName());
118 		b.append(getResource(), obj.getResource());
119 		b.append(getSystem(), obj.getSystem());
120 		b.append(getUnits(), obj.getUnits());
121 		b.append(getValue(), obj.getValue());
122 		b.append(getHashUnitsAndValPrefix(), obj.getHashUnitsAndValPrefix());
123 		b.append(getHashValPrefix(), obj.getHashValPrefix());
124 		return b.isEquals();
125 	}
126 
127 	public Long getHashUnitsAndValPrefix() {
128 		calculateHashes();
129 		return myHashUnitsAndValPrefix;
130 	}
131 
132 	public void setHashUnitsAndValPrefix(Long theHashUnitsAndValPrefix) {
133 		myHashUnitsAndValPrefix = theHashUnitsAndValPrefix;
134 	}
135 
136 	public Long getHashValPrefix() {
137 		calculateHashes();
138 		return myHashValPrefix;
139 	}
140 
141 	public void setHashValPrefix(Long theHashValPrefix) {
142 		myHashValPrefix = theHashValPrefix;
143 	}
144 
145 	@Override
146 	protected Long getId() {
147 		return myId;
148 	}
149 
150 	public String getSystem() {
151 		return mySystem;
152 	}
153 
154 	public void setSystem(String theSystem) {
155 		clearHashes();
156 		mySystem = theSystem;
157 	}
158 
159 	public String getUnits() {
160 		return myUnits;
161 	}
162 
163 	public void setUnits(String theUnits) {
164 		clearHashes();
165 		myUnits = theUnits;
166 	}
167 
168 	public BigDecimal getValue() {
169 		return myValue;
170 	}
171 
172 	public void setValue(BigDecimal theValue) {
173 		clearHashes();
174 		myValue = theValue;
175 	}
176 
177 	@Override
178 	public int hashCode() {
179 		HashCodeBuilder b = new HashCodeBuilder();
180 		b.append(getParamName());
181 		b.append(getResource());
182 		b.append(getSystem());
183 		b.append(getUnits());
184 		b.append(getValue());
185 		b.append(getHashUnitsAndValPrefix());
186 		b.append(getHashValPrefix());
187 		return b.toHashCode();
188 	}
189 
190 	@Override
191 	public IQueryParameterType toQueryParameterType() {
192 		return new QuantityParam(null, getValue(), getSystem(), getUnits());
193 	}
194 
195 	@Override
196 	public String toString() {
197 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
198 		b.append("paramName", getParamName());
199 		b.append("resourceId", getResourcePid());
200 		b.append("system", getSystem());
201 		b.append("units", getUnits());
202 		b.append("value", getValue());
203 		b.append("missing", isMissing());
204 		return b.build();
205 	}
206 
207 	private static String toTruncatedString(BigDecimal theValue) {
208 		if (theValue == null) {
209 			return null;
210 		}
211 		return theValue.setScale(0, RoundingMode.FLOOR).toPlainString();
212 	}
213 
214 }