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  	@Index(name = "IDX_SP_DATE", columnList = "RES_TYPE,SP_NAME,SP_VALUE_LOW,SP_VALUE_HIGH"),
41  	@Index(name = "IDX_SP_DATE_UPDATED", columnList = "SP_UPDATED"),
42  	@Index(name = "IDX_SP_DATE_RESID", columnList = "RES_ID")
43  })
44  public class ResourceIndexedSearchParamDate extends BaseResourceIndexedSearchParam {
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	@Transient
49  	private transient String myOriginalValue;
50  
51  	@Column(name = "SP_VALUE_HIGH", nullable = true)
52  	@Temporal(TemporalType.TIMESTAMP)
53  	@Field
54  	public Date myValueHigh;
55  	@Column(name = "SP_VALUE_LOW", nullable = true)
56  	@Temporal(TemporalType.TIMESTAMP)
57  	@Field
58  	public Date myValueLow;
59  	@Id
60  	@SequenceGenerator(name = "SEQ_SPIDX_DATE", sequenceName = "SEQ_SPIDX_DATE")
61  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_DATE")
62  	@Column(name = "SP_ID")
63  	private Long myId;
64  
65  	/**
66  	 * Constructor
67  	 */
68  	public ResourceIndexedSearchParamDate() {
69  		super();
70  	}
71  
72  	/**
73  	 * Constructor
74  	 */
75  	public ResourceIndexedSearchParamDate(String theName, Date theLow, Date theHigh, String theOriginalValue) {
76  		setParamName(theName);
77  		setValueLow(theLow);
78  		setValueHigh(theHigh);
79  		myOriginalValue = theOriginalValue;
80  	}
81  
82  	@Override
83  	public boolean equals(Object theObj) {
84  		if (this == theObj) {
85  			return true;
86  		}
87  		if (theObj == null) {
88  			return false;
89  		}
90  		if (!(theObj instanceof ResourceIndexedSearchParamDate)) {
91  			return false;
92  		}
93  		ResourceIndexedSearchParamDate obj = (ResourceIndexedSearchParamDate) theObj;
94  		EqualsBuilder b = new EqualsBuilder();
95  		b.append(getParamName(), obj.getParamName());
96  
97  		b.append(getResource(), obj.getResource());
98  		b.append(getTimeFromDate(getValueHigh()), getTimeFromDate(obj.getValueHigh()));
99  		b.append(getTimeFromDate(getValueLow()), getTimeFromDate(obj.getValueLow()));
100 		return b.isEquals();
101 	}
102 
103 	protected Long getTimeFromDate(Date date) {
104 		if (date != null) {
105 			return date.getTime();
106 		}
107 		return null;
108 	}
109 
110 	@Override
111 	protected Long getId() {
112 		return myId;
113 	}
114 
115 	public Date getValueHigh() {
116 		return myValueHigh;
117 	}
118 
119 	public void setValueHigh(Date theValueHigh) {
120 		myValueHigh = theValueHigh;
121 	}
122 
123 	public Date getValueLow() {
124 		return myValueLow;
125 	}
126 
127 	public void setValueLow(Date theValueLow) {
128 		myValueLow = theValueLow;
129 	}
130 
131 	@Override
132 	public int hashCode() {
133 		HashCodeBuilder b = new HashCodeBuilder();
134 		b.append(getParamName());
135 		b.append(getResource());
136 		b.append(getTimeFromDate(getValueHigh()));
137 		b.append(getTimeFromDate(getValueLow()));
138 		return b.toHashCode();
139 	}
140 
141 	@Override
142 	public IQueryParameterType toQueryParameterType() {
143 		DateTimeType value = new DateTimeType(myOriginalValue);
144 		if (value.getPrecision().ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
145 			value.setTimeZoneZulu(true);
146 		}
147 		return new DateParam(value.getValueAsString());
148 	}
149 
150 	@Override
151 	public String toString() {
152 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
153 		b.append("paramName", getParamName());
154 		b.append("resourceId", getResource().getId()); // TODO: add a field so we don't need to resolve this
155 		b.append("valueLow", new InstantDt(getValueLow()));
156 		b.append("valueHigh", new InstantDt(getValueHigh()));
157 		return b.build();
158 	}
159 }