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.builder.ToStringBuilder;
24  import org.apache.commons.lang3.builder.ToStringStyle;
25  
26  import javax.persistence.*;
27  import java.io.Serializable;
28  
29  @Entity
30  @Table(name = "HFJ_RES_PARAM_PRESENT", indexes = {
31  	// We used to have a constraint named IDX_RESPARMPRESENT_SPID_RESID - Don't reuse
32  	@Index(name = "IDX_RESPARMPRESENT_RESID", columnList = "RES_ID"),
33  	@Index(name = "IDX_RESPARMPRESENT_HASHPRES", columnList = "HASH_PRESENCE")
34  })
35  public class SearchParamPresent implements Serializable {
36  
37  	private static final long serialVersionUID = 1L;
38  
39  	@Id
40  	@SequenceGenerator(name = "SEQ_RESPARMPRESENT_ID", sequenceName = "SEQ_RESPARMPRESENT_ID")
41  	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESPARMPRESENT_ID")
42  	@Column(name = "PID")
43  	private Long myId;
44  	@Column(name = "SP_PRESENT", nullable = false)
45  	private boolean myPresent;
46  	@ManyToOne()
47  	@JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, foreignKey = @ForeignKey(name = "FK_RESPARMPRES_RESID"))
48  	private ResourceTable myResource;
49  	@Transient
50  	private transient String myParamName;
51  	@Column(name = "HASH_PRESENCE")
52  	private Long myHashPresence;
53  
54  	/**
55  	 * Constructor
56  	 */
57  	public SearchParamPresent() {
58  		super();
59  	}
60  
61  	@SuppressWarnings("unused")
62  	@PrePersist
63  	public void calculateHashes() {
64  		if (myHashPresence == null) {
65  			String resourceType = getResource().getResourceType();
66  			String paramName = getParamName();
67  			boolean present = myPresent;
68  			setHashPresence(calculateHashPresence(resourceType, paramName, present));
69  		}
70  	}
71  
72  	public Long getHashPresence() {
73  		return myHashPresence;
74  	}
75  
76  	public void setHashPresence(Long theHashPresence) {
77  		myHashPresence = theHashPresence;
78  	}
79  
80  	public String getParamName() {
81  		return myParamName;
82  	}
83  
84  	public void setParamName(String theParamName) {
85  		myParamName = theParamName;
86  	}
87  
88  	public ResourceTable getResource() {
89  		return myResource;
90  	}
91  
92  	public void setResource(ResourceTable theResourceTable) {
93  		myResource = theResourceTable;
94  	}
95  
96  	public boolean isPresent() {
97  		return myPresent;
98  	}
99  
100 	public void setPresent(boolean thePresent) {
101 		myPresent = thePresent;
102 	}
103 
104 	@Override
105 	public String toString() {
106 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
107 
108 		b.append("resPid", myResource.getIdDt().toUnqualifiedVersionless().getValue());
109 		b.append("paramName", myParamName);
110 		b.append("present", myPresent);
111 		return b.build();
112 	}
113 
114 	public static long calculateHashPresence(String theResourceType, String theParamName, Boolean thePresent) {
115 		String string = thePresent != null ? Boolean.toString(thePresent) : Boolean.toString(false);
116 		return BaseResourceIndexedSearchParam.hash(theResourceType, theParamName, string);
117 	}
118 
119 }