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  	@PrePersist
86  	public void calculateHashes() {
87  		if (myHashUri == null) {
88  			String resourceType = getResourceType();
89  			String paramName = getParamName();
90  			String uri = getUri();
91  			setHashIdentity(calculateHashIdentity(resourceType, paramName));
92  			setHashUri(calculateHashUri(resourceType, paramName, uri));
93  		}
94  	}
95  
96  	@Override
97  	protected void clearHashes() {
98  		myHashUri = 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 ResourceIndexedSearchParamUri)) {
110 			return false;
111 		}
112 		ResourceIndexedSearchParamUri obj = (ResourceIndexedSearchParamUri) theObj;
113 		EqualsBuilder b = new EqualsBuilder();
114 		b.append(getParamName(), obj.getParamName());
115 		b.append(getResource(), obj.getResource());
116 		b.append(getUri(), obj.getUri());
117 		b.append(getHashUri(), obj.getHashUri());
118 		b.append(getHashIdentity(), obj.getHashIdentity());
119 		return b.isEquals();
120 	}
121 
122 	private Long getHashIdentity() {
123 		return myHashIdentity;
124 	}
125 
126 	private void setHashIdentity(long theHashIdentity) {
127 		myHashIdentity = theHashIdentity;
128 	}
129 
130 	public Long getHashUri() {
131 		calculateHashes();
132 		return myHashUri;
133 	}
134 
135 	public void setHashUri(Long theHashUri) {
136 		myHashUri = theHashUri;
137 	}
138 
139 	@Override
140 	protected Long getId() {
141 		return myId;
142 	}
143 
144 	public String getUri() {
145 		return myUri;
146 	}
147 
148 	public void setUri(String theUri) {
149 		myUri = StringUtils.defaultIfBlank(theUri, null);
150 	}
151 
152 	@Override
153 	public int hashCode() {
154 		HashCodeBuilder b = new HashCodeBuilder();
155 		b.append(getParamName());
156 		b.append(getResource());
157 		b.append(getUri());
158 		b.append(getHashUri());
159 		return b.toHashCode();
160 	}
161 
162 	@Override
163 	public IQueryParameterType toQueryParameterType() {
164 		return new UriParam(getUri());
165 	}
166 
167 	@Override
168 	public String toString() {
169 		ToStringBuilder b = new ToStringBuilder(this);
170 		b.append("id", getId());
171 		b.append("resourceId", getResourcePid());
172 		b.append("paramName", getParamName());
173 		b.append("uri", myUri);
174 		return b.toString();
175 	}
176 
177 	public static long calculateHashUri(String theResourceType, String theParamName, String theUri) {
178 		return hash(theResourceType, theParamName, theUri);
179 	}
180 
181 }