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 java.io.Serializable;
24  import java.util.Date;
25  
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.FetchType;
29  import javax.persistence.ForeignKey;
30  import javax.persistence.GeneratedValue;
31  import javax.persistence.GenerationType;
32  import javax.persistence.Id;
33  import javax.persistence.Index;
34  import javax.persistence.JoinColumn;
35  import javax.persistence.ManyToOne;
36  import javax.persistence.SequenceGenerator;
37  import javax.persistence.Table;
38  import javax.persistence.Temporal;
39  import javax.persistence.TemporalType;
40  
41  import org.apache.commons.lang3.Validate;
42  import org.apache.commons.lang3.builder.EqualsBuilder;
43  import org.apache.commons.lang3.builder.HashCodeBuilder;
44  import org.hibernate.annotations.ColumnDefault;
45  import org.hibernate.search.annotations.Field;
46  import org.hl7.fhir.instance.model.api.IIdType;
47  
48  @Entity
49  @Table(name = "HFJ_RES_LINK" , indexes= {
50  	@Index(name="IDX_RL_TPATHRES", columnList= "SRC_PATH,TARGET_RESOURCE_ID"), 
51  	@Index(name="IDX_RL_SRC", columnList= "SRC_RESOURCE_ID"), 
52  	@Index(name="IDX_RL_DEST", columnList= "TARGET_RESOURCE_ID")
53  })
54  public class ResourceLink implements Serializable {
55  
56  	private static final long serialVersionUID = 1L;
57  
58  	@SequenceGenerator(name = "SEQ_RESLINK_ID", sequenceName = "SEQ_RESLINK_ID")
59  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESLINK_ID")
60  	@Id
61  	@Column(name = "PID")
62  	private Long myId;
63  
64  	@Column(name = "SRC_PATH", length = 100, nullable = false)
65  	private String mySourcePath;
66  
67  	@ManyToOne(optional = false, fetch=FetchType.LAZY)
68  	@JoinColumn(name = "SRC_RESOURCE_ID", referencedColumnName = "RES_ID", nullable = false, foreignKey=@ForeignKey(name="FK_RESLINK_SOURCE"))
69  //	@ContainedIn()
70  	private ResourceTable mySourceResource;
71  
72  	@Column(name = "SRC_RESOURCE_ID", insertable = false, updatable = false, nullable = false)
73  	private Long mySourceResourcePid;
74  
75  	@Column(name = "SOURCE_RESOURCE_TYPE", nullable=false, length=ResourceTable.RESTYPE_LEN)
76  	@ColumnDefault("''") // TODO: remove this (it's only here for simplifying upgrades of 1.3 -> 1.4)
77  	@Field()
78  	private String mySourceResourceType;
79  
80  	@ManyToOne(optional = true, fetch=FetchType.LAZY)
81  	@JoinColumn(name = "TARGET_RESOURCE_ID", referencedColumnName = "RES_ID", nullable = true, foreignKey=@ForeignKey(name="FK_RESLINK_TARGET"))
82  	private ResourceTable myTargetResource;
83  
84  	@Column(name = "TARGET_RESOURCE_ID", insertable = false, updatable = false, nullable = true)
85  	@Field()
86  	private Long myTargetResourcePid;
87  
88  	@Column(name = "TARGET_RESOURCE_TYPE", nullable=false, length=ResourceTable.RESTYPE_LEN)
89  	@ColumnDefault("''") // TODO: remove this (it's only here for simplifying upgrades of 1.3 -> 1.4)
90  	@Field()
91  	private String myTargetResourceType;
92  
93  	@Column(name = "TARGET_RESOURCE_URL", length=200, nullable = true)
94  	@Field()
95  	private String myTargetResourceUrl;
96  
97  	@Field()
98  	@Column(name = "SP_UPDATED", nullable = true) // TODO: make this false after HAPI 2.3
99  	@Temporal(TemporalType.TIMESTAMP)
100 	private Date myUpdated;
101 
102 	public ResourceLink() {
103 		super();
104 	}
105 
106 	public ResourceLink(String theSourcePath, ResourceTable theSourceResource, IIdType theTargetResourceUrl, Date theUpdated) {
107 		super();
108 		setSourcePath(theSourcePath);
109 		setSourceResource(theSourceResource);
110 		setTargetResourceUrl(theTargetResourceUrl);
111 		setUpdated(theUpdated);
112 	}
113 
114 	public ResourceLink(String theSourcePath, ResourceTable theSourceResource, ResourceTable theTargetResource, Date theUpdated) {
115 		super();
116 		setSourcePath(theSourcePath);
117 		setSourceResource(theSourceResource);
118 		setTargetResource(theTargetResource);
119 		setUpdated(theUpdated);
120 	}
121 
122 	@Override
123 	public boolean equals(Object theObj) {
124 		if (this == theObj) {
125 			return true;
126 		}
127 		if (theObj == null) {
128 			return false;
129 		}
130 		if (!(theObj instanceof ResourceLink)) {
131 			return false;
132 		}
133 		ResourceLink obj = (ResourceLink) theObj;
134 		EqualsBuilder b = new EqualsBuilder();
135 		b.append(mySourcePath, obj.mySourcePath);
136 		b.append(mySourceResource, obj.mySourceResource);
137 		b.append(myTargetResourcePid, obj.myTargetResourcePid);
138 		b.append(myTargetResourceUrl, obj.myTargetResourceUrl);
139 		return b.isEquals();
140 	}
141 
142 	public String getSourcePath() {
143 		return mySourcePath;
144 	}
145 
146 	public ResourceTable getSourceResource() {
147 		return mySourceResource;
148 	}
149 
150 	public Long getSourceResourcePid() {
151 		return mySourceResourcePid;
152 	}
153 
154 	public ResourceTable getTargetResource() {
155 		return myTargetResource;
156 	}
157 
158 	public Long getTargetResourcePid() {
159 		return myTargetResourcePid;
160 	}
161 
162 	public String getTargetResourceUrl() {
163 		return myTargetResourceUrl;
164 	}
165 
166 	public Date getUpdated() {
167 		return myUpdated;
168 	}
169 
170 	@Override
171 	public int hashCode() {
172 		HashCodeBuilder b = new HashCodeBuilder();
173 		b.append(mySourcePath);
174 		b.append(mySourceResource);
175 		b.append(myTargetResourcePid);
176 		b.append(myTargetResourceUrl);
177 		return b.toHashCode();
178 	}
179 
180 	public void setSourcePath(String theSourcePath) {
181 		mySourcePath = theSourcePath;
182 	}
183 
184 	public void setSourceResource(ResourceTable theSourceResource) {
185 		mySourceResource = theSourceResource;
186 		mySourceResourcePid = theSourceResource.getId();
187 		mySourceResourceType = theSourceResource.getResourceType();
188 	}
189 
190 	public void setTargetResource(ResourceTable theTargetResource) {
191 		Validate.notNull(theTargetResource);
192 		myTargetResource = theTargetResource;
193 		myTargetResourcePid = theTargetResource.getId();
194 		myTargetResourceType = theTargetResource.getResourceType();
195 	}
196 
197 	public void setTargetResourceUrl(IIdType theTargetResourceUrl) {
198 		Validate.isTrue(theTargetResourceUrl.hasBaseUrl());
199 		Validate.isTrue(theTargetResourceUrl.hasResourceType());
200 
201 		if (theTargetResourceUrl.hasIdPart()) {
202 			// do nothing
203 		} else {
204 			// Must have set an url like http://example.org/something
205 			// We treat 'something' as the resource type because of fix for #659. Prior to #659 fix, 'something' was
206 			// treated as the id and 'example.org' was treated as the resource type
207 			// TODO: log a warning?
208 		}
209 
210 		myTargetResourceType = theTargetResourceUrl.getResourceType();
211 		myTargetResourceUrl = theTargetResourceUrl.getValue();
212 	}
213 
214 	public void setUpdated(Date theUpdated) {
215 		myUpdated = theUpdated;
216 	}
217 
218 	@Override
219 	public String toString() {
220 		StringBuilder b = new StringBuilder();
221 		b.append("ResourceLink[");
222 		b.append("path=").append(mySourcePath);
223 		b.append(", src=").append(mySourceResourcePid);
224 		b.append(", target=").append(myTargetResourcePid);
225 		b.append(", targetUrl=").append(myTargetResourceUrl);
226 
227 		b.append("]");
228 		return b.toString();
229 	}
230 
231 }