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