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.rest.param.TokenParam;
25  import org.apache.commons.lang3.StringUtils;
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  
32  import javax.persistence.*;
33  
34  @Embeddable
35  @Entity
36  @Table(name = "HFJ_SPIDX_TOKEN", indexes = {
37  	@Index(name = "IDX_SP_TOKEN", columnList = "RES_TYPE,SP_NAME,SP_SYSTEM,SP_VALUE"),
38  	@Index(name = "IDX_SP_TOKEN_UNQUAL", columnList = "RES_TYPE,SP_NAME,SP_VALUE"),
39  	@Index(name = "IDX_SP_TOKEN_UPDATED", columnList = "SP_UPDATED"),
40  	@Index(name = "IDX_SP_TOKEN_RESID", columnList = "RES_ID")
41  })
42  public class ResourceIndexedSearchParamToken extends BaseResourceIndexedSearchParam {
43  
44  	public static final int MAX_LENGTH = 200;
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	@Field()
49  	@Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
50  	public String mySystem;
51  	@Field()
52  	@Column(name = "SP_VALUE", nullable = true, length = MAX_LENGTH)
53  	public String myValue;
54  	@Id
55  	@SequenceGenerator(name = "SEQ_SPIDX_TOKEN", sequenceName = "SEQ_SPIDX_TOKEN")
56  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_TOKEN")
57  	@Column(name = "SP_ID")
58  	private Long myId;
59  	/**
60  	 * @since 3.4.0 - At some point this should be made not-null
61  	 */
62  	@Column(name = "HASH_SYS", nullable = true)
63  	private Long myHashSystem;
64  	/**
65  	 * @since 3.4.0 - At some point this should be made not-null
66  	 */
67  	@Column(name = "HASH_SYS_AND_VALUE", nullable = true)
68  	private Long myHashSystemAndValue;
69  	/**
70  	 * @since 3.4.0 - At some point this should be made not-null
71  	 */
72  	@Column(name = "HASH_VALUE", nullable = true)
73  	private Long myHashValue;
74  
75  
76  	/**
77  	 * Constructor
78  	 */
79  	public ResourceIndexedSearchParamToken() {
80  		super();
81  	}
82  
83  	/**
84  	 * Constructor
85  	 */
86  	public ResourceIndexedSearchParamToken(String theName, String theSystem, String theValue) {
87  		super();
88  		setParamName(theName);
89  		setSystem(theSystem);
90  		setValue(theValue);
91  	}
92  
93  
94  	@PrePersist
95  	public void calculateHashes() {
96  		if (myHashSystem == null) {
97  			setHashSystem(hash(getResourceType(), getParamName(), getSystem()));
98  			setHashSystemAndValue(hash(getResourceType(), getParamName(), getSystem(), getValue()));
99  			setHashValue(hash(getResourceType(), getParamName(), getValue()));
100 		}
101 	}
102 
103 
104 	@Override
105 	protected void clearHashes() {
106 		myHashSystem = null;
107 		myHashSystemAndValue = null;
108 		myHashValue = null;
109 	}
110 
111 	@Override
112 	public boolean equals(Object theObj) {
113 		if (this == theObj) {
114 			return true;
115 		}
116 		if (theObj == null) {
117 			return false;
118 		}
119 		if (!(theObj instanceof ResourceIndexedSearchParamToken)) {
120 			return false;
121 		}
122 		ResourceIndexedSearchParamToken obj = (ResourceIndexedSearchParamToken) theObj;
123 		EqualsBuilder b = new EqualsBuilder();
124 		b.append(getParamName(), obj.getParamName());
125 		b.append(getResource(), obj.getResource());
126 		b.append(getSystem(), obj.getSystem());
127 		b.append(getValue(), obj.getValue());
128 		b.append(getHashSystem(), obj.getHashSystem());
129 		b.append(getHashSystemAndValue(), obj.getHashSystemAndValue());
130 		b.append(getHashValue(), obj.getHashValue());
131 		return b.isEquals();
132 	}
133 
134 	public Long getHashSystem() {
135 		calculateHashes();
136 		return myHashSystem;
137 	}
138 
139 	public void setHashSystem(Long theHashSystem) {
140 		myHashSystem = theHashSystem;
141 	}
142 
143 	public Long getHashSystemAndValue() {
144 		calculateHashes();
145 		return myHashSystemAndValue;
146 	}
147 
148 	public void setHashSystemAndValue(Long theHashSystemAndValue) {
149 		calculateHashes();
150 		myHashSystemAndValue = theHashSystemAndValue;
151 	}
152 
153 	public Long getHashValue() {
154 		calculateHashes();
155 		return myHashValue;
156 	}
157 
158 	public void setHashValue(Long theHashValue) {
159 		myHashValue = theHashValue;
160 	}
161 
162 	@Override
163 	protected Long getId() {
164 		return myId;
165 	}
166 
167 	public String getSystem() {
168 		return mySystem;
169 	}
170 
171 	public void setSystem(String theSystem) {
172 		clearHashes();
173 		mySystem = StringUtils.defaultIfBlank(theSystem, null);
174 	}
175 
176 	public String getValue() {
177 		return myValue;
178 	}
179 
180 	public void setValue(String theValue) {
181 		clearHashes();
182 		myValue = StringUtils.defaultIfBlank(theValue, null);
183 	}
184 
185 	@Override
186 	public int hashCode() {
187 		HashCodeBuilder b = new HashCodeBuilder();
188 		b.append(getParamName());
189 		b.append(getResource());
190 		b.append(getSystem());
191 		b.append(getValue());
192 		b.append(getHashSystem());
193 		b.append(getHashSystemAndValue());
194 		b.append(getHashValue());
195 		return b.toHashCode();
196 	}
197 
198 
199 	@Override
200 	public IQueryParameterType toQueryParameterType() {
201 		return new TokenParam(getSystem(), getValue());
202 	}
203 
204 	@Override
205 	public String toString() {
206 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
207 		b.append("paramName", getParamName());
208 		b.append("resourceId", getResourcePid());
209 		b.append("system", getSystem());
210 		b.append("value", getValue());
211 		return b.build();
212 	}
213 }