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.model.api.IQueryParameterType;
24  import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
25  import ca.uhn.fhir.model.primitive.InstantDt;
26  import ca.uhn.fhir.rest.param.DateParam;
27  import org.apache.commons.lang3.builder.EqualsBuilder;
28  import org.apache.commons.lang3.builder.HashCodeBuilder;
29  import org.apache.commons.lang3.builder.ToStringBuilder;
30  import org.apache.commons.lang3.builder.ToStringStyle;
31  import org.hibernate.search.annotations.Field;
32  import org.hl7.fhir.r4.model.DateTimeType;
33  
34  import javax.persistence.*;
35  import java.util.Date;
36  
37  @Embeddable
38  @Entity
39  @Table(name = "HFJ_SPIDX_DATE", indexes = {
40  	// We previously had an index called IDX_SP_DATE - Dont reuse
41  	@Index(name = "IDX_SP_DATE_HASH", columnList = "HASH_IDENTITY,SP_VALUE_LOW,SP_VALUE_HIGH"),
42  	@Index(name = "IDX_SP_DATE_UPDATED", columnList = "SP_UPDATED"),
43  	@Index(name = "IDX_SP_DATE_RESID", columnList = "RES_ID")
44  })
45  public class ResourceIndexedSearchParamDate extends BaseResourceIndexedSearchParam {
46  
47  	private static final long serialVersionUID = 1L;
48  	@Column(name = "SP_VALUE_HIGH", nullable = true)
49  	@Temporal(TemporalType.TIMESTAMP)
50  	@Field
51  	public Date myValueHigh;
52  	@Column(name = "SP_VALUE_LOW", nullable = true)
53  	@Temporal(TemporalType.TIMESTAMP)
54  	@Field
55  	public Date myValueLow;
56  	@Transient
57  	private transient String myOriginalValue;
58  	@Id
59  	@SequenceGenerator(name = "SEQ_SPIDX_DATE", sequenceName = "SEQ_SPIDX_DATE")
60  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_DATE")
61  	@Column(name = "SP_ID")
62  	private Long myId;
63  	/**
64  	 * @since 3.5.0 - At some point this should be made not-null
65  	 */
66  	@Column(name = "HASH_IDENTITY", nullable = true)
67  	private Long myHashIdentity;
68  
69  	/**
70  	 * Constructor
71  	 */
72  	public ResourceIndexedSearchParamDate() {
73  		super();
74  	}
75  
76  	/**
77  	 * Constructor
78  	 */
79  	public ResourceIndexedSearchParamDate(String theName, Date theLow, Date theHigh, String theOriginalValue) {
80  		setParamName(theName);
81  		setValueLow(theLow);
82  		setValueHigh(theHigh);
83  		myOriginalValue = theOriginalValue;
84  	}
85  
86  	@PrePersist
87  	public void calculateHashes() {
88  		if (myHashIdentity == null) {
89  			String resourceType = getResourceType();
90  			String paramName = getParamName();
91  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
92  		}
93  	}
94  
95  	@Override
96  	protected void clearHashes() {
97  		myHashIdentity = null;
98  	}
99  
100 	@Override
101 	public boolean equals(Object theObj) {
102 		if (this == theObj) {
103 			return true;
104 		}
105 		if (theObj == null) {
106 			return false;
107 		}
108 		if (!(theObj instanceof ResourceIndexedSearchParamDate)) {
109 			return false;
110 		}
111 		ResourceIndexedSearchParamDate obj = (ResourceIndexedSearchParamDate) theObj;
112 		EqualsBuilder b = new EqualsBuilder();
113 		b.append(getParamName(), obj.getParamName());
114 
115 		b.append(getResource(), obj.getResource());
116 		b.append(getTimeFromDate(getValueHigh()), getTimeFromDate(obj.getValueHigh()));
117 		b.append(getTimeFromDate(getValueLow()), getTimeFromDate(obj.getValueLow()));
118 		b.append(getHashIdentity(), obj.getHashIdentity());
119 		return b.isEquals();
120 	}
121 
122 	public Long getHashIdentity() {
123 		return myHashIdentity;
124 	}
125 
126 	public void setHashIdentity(Long theHashIdentity) {
127 		myHashIdentity = theHashIdentity;
128 	}
129 
130 	@Override
131 	protected Long getId() {
132 		return myId;
133 	}
134 
135 	protected Long getTimeFromDate(Date date) {
136 		if (date != null) {
137 			return date.getTime();
138 		}
139 		return null;
140 	}
141 
142 	public Date getValueHigh() {
143 		return myValueHigh;
144 	}
145 
146 	public void setValueHigh(Date theValueHigh) {
147 		myValueHigh = theValueHigh;
148 	}
149 
150 	public Date getValueLow() {
151 		return myValueLow;
152 	}
153 
154 	public void setValueLow(Date theValueLow) {
155 		myValueLow = theValueLow;
156 	}
157 
158 	@Override
159 	public int hashCode() {
160 		HashCodeBuilder b = new HashCodeBuilder();
161 		b.append(getParamName());
162 		b.append(getResource());
163 		b.append(getTimeFromDate(getValueHigh()));
164 		b.append(getTimeFromDate(getValueLow()));
165 		return b.toHashCode();
166 	}
167 
168 	@Override
169 	public IQueryParameterType toQueryParameterType() {
170 		DateTimeType value = new DateTimeType(myOriginalValue);
171 		if (value.getPrecision().ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
172 			value.setTimeZoneZulu(true);
173 		}
174 		return new DateParam(value.getValueAsString());
175 	}
176 
177 	@Override
178 	public String toString() {
179 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
180 		b.append("paramName", getParamName());
181 		b.append("resourceId", getResource().getId()); // TODO: add a field so we don't need to resolve this
182 		b.append("valueLow", new InstantDt(getValueLow()));
183 		b.append("valueHigh", new InstantDt(getValueHigh()));
184 		return b.build();
185 	}
186 }