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  	@Override
87  	@PrePersist
88  	public void calculateHashes() {
89  		if (myHashIdentity == null) {
90  			String resourceType = getResourceType();
91  			String paramName = getParamName();
92  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
93  		}
94  	}
95  
96  	@Override
97  	protected void clearHashes() {
98  		myHashIdentity = null;
99  	}
100 
101 	@Override
102 	public boolean equals(Object theObj) {
103 		if (this == theObj) {
104 			return true;
105 		}
106 		if (theObj == null) {
107 			return false;
108 		}
109 		if (!(theObj instanceof ResourceIndexedSearchParamDate)) {
110 			return false;
111 		}
112 		ResourceIndexedSearchParamDate obj = (ResourceIndexedSearchParamDate) theObj;
113 		EqualsBuilder b = new EqualsBuilder();
114 		b.append(getParamName(), obj.getParamName());
115 
116 		b.append(getResource(), obj.getResource());
117 		b.append(getTimeFromDate(getValueHigh()), getTimeFromDate(obj.getValueHigh()));
118 		b.append(getTimeFromDate(getValueLow()), getTimeFromDate(obj.getValueLow()));
119 		b.append(getHashIdentity(), obj.getHashIdentity());
120 		return b.isEquals();
121 	}
122 
123 	public Long getHashIdentity() {
124 		return myHashIdentity;
125 	}
126 
127 	public void setHashIdentity(Long theHashIdentity) {
128 		myHashIdentity = theHashIdentity;
129 	}
130 
131 	@Override
132 	protected Long getId() {
133 		return myId;
134 	}
135 
136 	protected Long getTimeFromDate(Date date) {
137 		if (date != null) {
138 			return date.getTime();
139 		}
140 		return null;
141 	}
142 
143 	public Date getValueHigh() {
144 		return myValueHigh;
145 	}
146 
147 	public void setValueHigh(Date theValueHigh) {
148 		myValueHigh = theValueHigh;
149 	}
150 
151 	public Date getValueLow() {
152 		return myValueLow;
153 	}
154 
155 	public void setValueLow(Date theValueLow) {
156 		myValueLow = theValueLow;
157 	}
158 
159 	@Override
160 	public int hashCode() {
161 		HashCodeBuilder b = new HashCodeBuilder();
162 		b.append(getParamName());
163 		b.append(getResource());
164 		b.append(getTimeFromDate(getValueHigh()));
165 		b.append(getTimeFromDate(getValueLow()));
166 		return b.toHashCode();
167 	}
168 
169 	@Override
170 	public IQueryParameterType toQueryParameterType() {
171 		DateTimeType value = new DateTimeType(myOriginalValue);
172 		if (value.getPrecision().ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
173 			value.setTimeZoneZulu(true);
174 		}
175 		return new DateParam(value.getValueAsString());
176 	}
177 
178 	@Override
179 	public String toString() {
180 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
181 		b.append("paramName", getParamName());
182 		b.append("resourceId", getResource().getId()); // TODO: add a field so we don't need to resolve this
183 		b.append("valueLow", new InstantDt(getValueLow()));
184 		b.append("valueHigh", new InstantDt(getValueHigh()));
185 		return b.build();
186 	}
187 }