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  	@Override
73  	@PrePersist
74  	public void calculateHashes() {
75  		if (myHashIdentity == null) {
76  			String resourceType = getResourceType();
77  			String paramName = getParamName();
78  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
79  		}
80  	}
81  
82  	@Override
83  	protected void clearHashes() {
84  		myHashIdentity = null;
85  	}
86  
87  	@Override
88  	public boolean equals(Object theObj) {
89  		if (this == theObj) {
90  			return true;
91  		}
92  		if (theObj == null) {
93  			return false;
94  		}
95  		if (!(theObj instanceof ResourceIndexedSearchParamNumber)) {
96  			return false;
97  		}
98  		ResourceIndexedSearchParamNumber obj = (ResourceIndexedSearchParamNumber) theObj;
99  		EqualsBuilder b = new EqualsBuilder();
100 		b.append(getParamName(), obj.getParamName());
101 		b.append(getResource(), obj.getResource());
102 		b.append(getValue(), obj.getValue());
103 		b.append(isMissing(), obj.isMissing());
104 		b.append(getHashIdentity(), obj.getHashIdentity());
105 		return b.isEquals();
106 	}
107 
108 	public Long getHashIdentity() {
109 		return myHashIdentity;
110 	}
111 
112 	public void setHashIdentity(Long theHashIdentity) {
113 		myHashIdentity = theHashIdentity;
114 	}
115 
116 	@Override
117 	protected Long getId() {
118 		return myId;
119 	}
120 
121 	public BigDecimal getValue() {
122 		return myValue;
123 	}
124 
125 	public void setValue(BigDecimal theValue) {
126 		myValue = theValue;
127 	}
128 
129 	@Override
130 	public int hashCode() {
131 		HashCodeBuilder b = new HashCodeBuilder();
132 		b.append(getParamName());
133 		b.append(getResource());
134 		b.append(getValue());
135 		b.append(isMissing());
136 		return b.toHashCode();
137 	}
138 
139 	@Override
140 	public IQueryParameterType toQueryParameterType() {
141 		return new NumberParam(myValue.toPlainString());
142 	}
143 
144 	@Override
145 	public String toString() {
146 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
147 		b.append("paramName", getParamName());
148 		b.append("resourceId", getResource().getId()); // TODO: add a field so we don't need to resolve this
149 		b.append("value", getValue());
150 		return b.build();
151 	}
152 }