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_RESTYPE_NAME", columnList = "RES_TYPE,SP_NAME"),
38  	@Index(name = "IDX_SP_URI_UPDATED", columnList = "SP_UPDATED"),
39  	@Index(name = "IDX_SP_URI_COORDS", columnList = "RES_ID")
40  })
41  public class ResourceIndexedSearchParamUri extends BaseResourceIndexedSearchParam {
42  
43  	/*
44  	 * Note that MYSQL chokes on unique indexes for lengths > 255 so be careful here
45  	 */
46  	public static final int MAX_LENGTH = 255;
47  
48  	private static final long serialVersionUID = 1L;
49  	@Column(name = "SP_URI", nullable = true, length = MAX_LENGTH)
50  	@Field()
51  	public String myUri;
52  	@Id
53  	@SequenceGenerator(name = "SEQ_SPIDX_URI", sequenceName = "SEQ_SPIDX_URI")
54  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_URI")
55  	@Column(name = "SP_ID")
56  	private Long myId;
57  	/**
58  	 * @since 3.4.0 - At some point this should be made not-null
59  	 */
60  	@Column(name = "HASH_URI", nullable = true)
61  	private Long myHashUri;
62  
63  	/**
64  	 * Constructor
65  	 */
66  	public ResourceIndexedSearchParamUri() {
67  	}
68  
69  	/**
70  	 * Constructor
71  	 */
72  	public ResourceIndexedSearchParamUri(String theName, String theUri) {
73  		setParamName(theName);
74  		setUri(theUri);
75  	}
76  
77  	@PrePersist
78  	public void calculateHashes() {
79  		if (myHashUri == null) {
80  			setHashUri(hash(getResourceType(), getParamName(), getUri()));
81  		}
82  	}
83  
84  	@Override
85  	protected void clearHashes() {
86  		myHashUri = null;
87  	}
88  
89  	@Override
90  	public boolean equals(Object theObj) {
91  		if (this == theObj) {
92  			return true;
93  		}
94  		if (theObj == null) {
95  			return false;
96  		}
97  		if (!(theObj instanceof ResourceIndexedSearchParamUri)) {
98  			return false;
99  		}
100 		ResourceIndexedSearchParamUri obj = (ResourceIndexedSearchParamUri) theObj;
101 		EqualsBuilder b = new EqualsBuilder();
102 		b.append(getParamName(), obj.getParamName());
103 		b.append(getResource(), obj.getResource());
104 		b.append(getUri(), obj.getUri());
105 		b.append(getHashUri(), obj.getHashUri());
106 		return b.isEquals();
107 	}
108 
109 	public Long getHashUri() {
110 		calculateHashes();
111 		return myHashUri;
112 	}
113 
114 	public void setHashUri(Long theHashUri) {
115 		myHashUri = theHashUri;
116 	}
117 
118 	@Override
119 	protected Long getId() {
120 		return myId;
121 	}
122 
123 	public String getUri() {
124 		return myUri;
125 	}
126 
127 	public void setUri(String theUri) {
128 		myUri = StringUtils.defaultIfBlank(theUri, null);
129 	}
130 
131 	@Override
132 	public int hashCode() {
133 		HashCodeBuilder b = new HashCodeBuilder();
134 		b.append(getParamName());
135 		b.append(getResource());
136 		b.append(getUri());
137 		b.append(getHashUri());
138 		return b.toHashCode();
139 	}
140 
141 	@Override
142 	public IQueryParameterType toQueryParameterType() {
143 		return new UriParam(getUri());
144 	}
145 
146 	@Override
147 	public String toString() {
148 		ToStringBuilder b = new ToStringBuilder(this);
149 		b.append("id", getId());
150 		b.append("resourceId", getResourcePid());
151 		b.append("paramName", getParamName());
152 		b.append("uri", myUri);
153 		return b.toString();
154 	}
155 
156 }