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