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.primitive.IdDt;
24  import ca.uhn.fhir.rest.api.Constants;
25  import org.hibernate.annotations.OptimisticLock;
26  
27  import javax.persistence.*;
28  import java.io.Serializable;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  
32  //@formatter:off
33  @Entity
34  @Table(name = "HFJ_RES_VER", uniqueConstraints = {
35  	@UniqueConstraint(name = ResourceHistoryTable.IDX_RESVER_ID_VER, columnNames = {"RES_ID", "RES_VER"})
36  }, indexes = {
37  	@Index(name = "IDX_RESVER_TYPE_DATE", columnList = "RES_TYPE,RES_UPDATED"),
38  	@Index(name = "IDX_RESVER_ID_DATE", columnList = "RES_ID,RES_UPDATED"),
39  	@Index(name = "IDX_RESVER_DATE", columnList = "RES_UPDATED")
40  })
41  //@formatter:on
42  public class ResourceHistoryTable extends BaseHasResource implements Serializable {
43  
44  	private static final long serialVersionUID = 1L;
45  	public static final String IDX_RESVER_ID_VER = "IDX_RESVER_ID_VER";
46  
47  	@Id
48  	@SequenceGenerator(name = "SEQ_RESOURCE_HISTORY_ID", sequenceName = "SEQ_RESOURCE_HISTORY_ID")
49  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESOURCE_HISTORY_ID")
50  	@Column(name = "PID")
51  	private Long myId;
52  
53  	@Column(name = "RES_ID")
54  	private Long myResourceId;
55  
56  	@Column(name = "RES_TYPE", length = 30, nullable = false)
57  	private String myResourceType;
58  
59  	@Column(name = "RES_VER", nullable = false)
60  	private Long myResourceVersion;
61  
62  	@OneToMany(mappedBy = "myResourceHistory", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
63  	private Collection<ResourceHistoryTag> myTags;
64  
65  	@Column(name = "RES_TEXT", length = Integer.MAX_VALUE - 1, nullable = true)
66  	@Lob()
67  	@OptimisticLock(excluded = true)
68  	private byte[] myResource;
69  
70  	@Column(name = "RES_ENCODING", nullable = false, length = 5)
71  	@Enumerated(EnumType.STRING)
72  	@OptimisticLock(excluded = true)
73  	private ResourceEncodingEnum myEncoding;
74  
75  	public ResourceHistoryTable() {
76  		super();
77  	}
78  
79  	public void addTag(ResourceHistoryTag theTag) {
80  		for (ResourceHistoryTag next : getTags()) {
81  			if (next.getTag().equals(theTag)) {
82  				return;
83  			}
84  		}
85  		getTags().add(theTag);
86  	}
87  
88  	public void addTag(ResourceTag theTag) {
89  		ResourceHistoryTag tag = new ResourceHistoryTag(this, theTag.getTag());
90  		tag.setResourceType(theTag.getResourceType());
91  		getTags().add(tag);
92  	}
93  
94  	@Override
95  	public ResourceHistoryTag addTag(TagDefinition theTag) {
96  		for (ResourceHistoryTag next : getTags()) {
97  			if (next.getTag().equals(theTag)) {
98  				return next;
99  			}
100 		}
101 		ResourceHistoryTag historyTag = new ResourceHistoryTag(this, theTag);
102 		getTags().add(historyTag);
103 		return historyTag;
104 	}
105 
106 	public ResourceEncodingEnum getEncoding() {
107 		return myEncoding;
108 	}
109 
110 	public void setEncoding(ResourceEncodingEnum theEncoding) {
111 		myEncoding = theEncoding;
112 	}
113 
114 	@Override
115 	public Long getId() {
116 		return myId;
117 	}
118 
119 	public void setId(Long theId) {
120 		myId = theId;
121 	}
122 
123 	@Override
124 	public IdDt getIdDt() {
125 		if (getForcedId() == null) {
126 			Long id = myResourceId;
127 			return new IdDt(myResourceType + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
128 		} else {
129 			return new IdDt(getForcedId().getResourceType() + '/' + getForcedId().getForcedId() + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
130 		}
131 	}
132 
133 	public byte[] getResource() {
134 		return myResource;
135 	}
136 
137 	public void setResource(byte[] theResource) {
138 		myResource = theResource;
139 	}
140 
141 	@Override
142 	public Long getResourceId() {
143 		return myResourceId;
144 	}
145 
146 	public void setResourceId(Long theResourceId) {
147 		myResourceId = theResourceId;
148 	}
149 
150 	@Override
151 	public String getResourceType() {
152 		return myResourceType;
153 	}
154 
155 	public void setResourceType(String theResourceType) {
156 		myResourceType = theResourceType;
157 	}
158 
159 	@Override
160 	public Collection<ResourceHistoryTag> getTags() {
161 		if (myTags == null) {
162 			myTags = new ArrayList<ResourceHistoryTag>();
163 		}
164 		return myTags;
165 	}
166 
167 	@Override
168 	public long getVersion() {
169 		return myResourceVersion;
170 	}
171 
172 	public void setVersion(long theVersion) {
173 		myResourceVersion = theVersion;
174 	}
175 
176 	public boolean hasTag(String theTerm, String theScheme) {
177 		for (ResourceHistoryTag next : getTags()) {
178 			if (next.getTag().getSystem().equals(theScheme) && next.getTag().getCode().equals(theTerm)) {
179 				return true;
180 			}
181 		}
182 		return false;
183 	}
184 
185 }