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.NumberParam;
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  @Embeddable
38  @Entity
39  @Table(name = "HFJ_SPIDX_NUMBER", indexes = {
40  //	We used to have an index with name IDX_SP_NUMBER - Dont reuse
41  	@Index(name = "IDX_SP_NUMBER_HASH_VAL", columnList = "HASH_IDENTITY,SP_VALUE"),
42  	@Index(name = "IDX_SP_NUMBER_UPDATED", columnList = "SP_UPDATED"),
43  	@Index(name = "IDX_SP_NUMBER_RESID", columnList = "RES_ID")
44  })
45  public class ResourceIndexedSearchParamNumber extends BaseResourceIndexedSearchParam {
46  
47  	private static final long serialVersionUID = 1L;
48  	@Column(name = "SP_VALUE", nullable = true)
49  	@Field
50  	@NumericField
51  	@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
52  	public BigDecimal myValue;
53  	@Id
54  	@SequenceGenerator(name = "SEQ_SPIDX_NUMBER", sequenceName = "SEQ_SPIDX_NUMBER")
55  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_NUMBER")
56  	@Column(name = "SP_ID")
57  	private Long myId;
58  	/**
59  	 * @since 3.5.0 - At some point this should be made not-null
60  	 */
61  	@Column(name = "HASH_IDENTITY", nullable = true)
62  	private Long myHashIdentity;
63  
64  	public ResourceIndexedSearchParamNumber() {
65  	}
66  
67  	public ResourceIndexedSearchParamNumber(String theParamName, BigDecimal theValue) {
68  		setParamName(theParamName);
69  		setValue(theValue);
70  	}
71  
72  	@PrePersist
73  	public void calculateHashes() {
74  		if (myHashIdentity == null) {
75  			String resourceType = getResourceType();
76  			String paramName = getParamName();
77  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
78  		}
79  	}
80  
81  	@Override
82  	protected void clearHashes() {
83  		myHashIdentity = null;
84  	}
85  
86  	@Override
87  	public boolean equals(Object theObj) {
88  		if (this == theObj) {
89  			return true;
90  		}
91  		if (theObj == null) {
92  			return false;
93  		}
94  		if (!(theObj instanceof ResourceIndexedSearchParamNumber)) {
95  			return false;
96  		}
97  		ResourceIndexedSearchParamNumber obj = (ResourceIndexedSearchParamNumber) theObj;
98  		EqualsBuilder b = new EqualsBuilder();
99  		b.append(getParamName(), obj.getParamName());
100 		b.append(getResource(), obj.getResource());
101 		b.append(getValue(), obj.getValue());
102 		b.append(isMissing(), obj.isMissing());
103 		b.append(getHashIdentity(), obj.getHashIdentity());
104 		return b.isEquals();
105 	}
106 
107 	public Long getHashIdentity() {
108 		return myHashIdentity;
109 	}
110 
111 	public void setHashIdentity(Long theHashIdentity) {
112 		myHashIdentity = theHashIdentity;
113 	}
114 
115 	@Override
116 	protected Long getId() {
117 		return myId;
118 	}
119 
120 	public BigDecimal getValue() {
121 		return myValue;
122 	}
123 
124 	public void setValue(BigDecimal theValue) {
125 		myValue = theValue;
126 	}
127 
128 	@Override
129 	public int hashCode() {
130 		HashCodeBuilder b = new HashCodeBuilder();
131 		b.append(getParamName());
132 		b.append(getResource());
133 		b.append(getValue());
134 		b.append(isMissing());
135 		return b.toHashCode();
136 	}
137 
138 	@Override
139 	public IQueryParameterType toQueryParameterType() {
140 		return new NumberParam(myValue.toPlainString());
141 	}
142 
143 	@Override
144 	public String toString() {
145 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
146 		b.append("paramName", getParamName());
147 		b.append("resourceId", getResource().getId()); // TODO: add a field so we don't need to resolve this
148 		b.append("value", getValue());
149 		return b.build();
150 	}
151 }