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.UriParam;
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.hibernate.search.annotations.Field;
30  
31  import javax.persistence.*;
32  
33  @Embeddable
34  @Entity
35  @Table(name = "HFJ_SPIDX_URI", indexes = {
36  	@Index(name = "IDX_SP_URI", columnList = "RES_TYPE,SP_NAME,SP_URI"),
37  	@Index(name = "IDX_SP_URI_HASH_IDENTITY", columnList = "HASH_IDENTITY,SP_URI"),
38  	@Index(name = "IDX_SP_URI_HASH_URI", columnList = "HASH_URI"),
39  	@Index(name = "IDX_SP_URI_RESTYPE_NAME", columnList = "RES_TYPE,SP_NAME"),
40  	@Index(name = "IDX_SP_URI_UPDATED", columnList = "SP_UPDATED"),
41  	@Index(name = "IDX_SP_URI_COORDS", columnList = "RES_ID")
42  })
43  public class ResourceIndexedSearchParamUri extends BaseResourceIndexedSearchParam {
44  
45  	/*
46  	 * Note that MYSQL chokes on unique indexes for lengths > 255 so be careful here
47  	 */
48  	public static final int MAX_LENGTH = 255;
49  
50  	private static final long serialVersionUID = 1L;
51  	@Column(name = "SP_URI", nullable = true, length = MAX_LENGTH)
52  	@Field()
53  	public String myUri;
54  	@Id
55  	@SequenceGenerator(name = "SEQ_SPIDX_URI", sequenceName = "SEQ_SPIDX_URI")
56  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_URI")
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_URI", nullable = true)
63  	private Long myHashUri;
64  	/**
65  	 * @since 3.5.0 - At some point this should be made not-null
66  	 */
67  	@Column(name = "HASH_IDENTITY", nullable = true)
68  	private Long myHashIdentity;
69  
70  	/**
71  	 * Constructor
72  	 */
73  	public ResourceIndexedSearchParamUri() {
74  		super();
75  	}
76  
77  	/**
78  	 * Constructor
79  	 */
80  	public ResourceIndexedSearchParamUri(String theName, String theUri) {
81  		setParamName(theName);
82  		setUri(theUri);
83  	}
84  
85  	@Override
86  	@PrePersist
87  	public void calculateHashes() {
88  		if (myHashUri == null) {
89  			String resourceType = getResourceType();
90  			String paramName = getParamName();
91  			String uri = getUri();
92  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
93  			setHashUri(calculateHashUri(resourceType, paramName, uri));
94  		}
95  	}
96  
97  	@Override
98  	protected void clearHashes() {
99  		myHashUri = null;
100 	}
101 
102 	@Override
103 	public boolean equals(Object theObj) {
104 		if (this == theObj) {
105 			return true;
106 		}
107 		if (theObj == null) {
108 			return false;
109 		}
110 		if (!(theObj instanceof ResourceIndexedSearchParamUri)) {
111 			return false;
112 		}
113 		ResourceIndexedSearchParamUri obj = (ResourceIndexedSearchParamUri) theObj;
114 		EqualsBuilder b = new EqualsBuilder();
115 		b.append(getParamName(), obj.getParamName());
116 		b.append(getResource(), obj.getResource());
117 		b.append(getUri(), obj.getUri());
118 		b.append(getHashUri(), obj.getHashUri());
119 		b.append(getHashIdentity(), obj.getHashIdentity());
120 		return b.isEquals();
121 	}
122 
123 	private Long getHashIdentity() {
124 		return myHashIdentity;
125 	}
126 
127 	private void setHashIdentity(long theHashIdentity) {
128 		myHashIdentity = theHashIdentity;
129 	}
130 
131 	public Long getHashUri() {
132 		calculateHashes();
133 		return myHashUri;
134 	}
135 
136 	public void setHashUri(Long theHashUri) {
137 		myHashUri = theHashUri;
138 	}
139 
140 	@Override
141 	protected Long getId() {
142 		return myId;
143 	}
144 
145 	public String getUri() {
146 		return myUri;
147 	}
148 
149 	public void setUri(String theUri) {
150 		myUri = StringUtils.defaultIfBlank(theUri, null);
151 	}
152 
153 	@Override
154 	public int hashCode() {
155 		HashCodeBuilder b = new HashCodeBuilder();
156 		b.append(getParamName());
157 		b.append(getResource());
158 		b.append(getUri());
159 		b.append(getHashUri());
160 		return b.toHashCode();
161 	}
162 
163 	@Override
164 	public IQueryParameterType toQueryParameterType() {
165 		return new UriParam(getUri());
166 	}
167 
168 	@Override
169 	public String toString() {
170 		ToStringBuilder b = new ToStringBuilder(this);
171 		b.append("id", getId());
172 		b.append("resourceId", getResourcePid());
173 		b.append("paramName", getParamName());
174 		b.append("uri", myUri);
175 		return b.toString();
176 	}
177 
178 	public static long calculateHashUri(String theResourceType, String theParamName, String theUri) {
179 		return hash(theResourceType, theParamName, theUri);
180 	}
181 
182 }