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