
001/* 002 * #%L 003 * HAPI FHIR JPA Model 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.model.entity; 021 022import ca.uhn.fhir.jpa.model.dao.JpaPid; 023import jakarta.persistence.Column; 024import jakarta.persistence.Entity; 025import jakarta.persistence.FetchType; 026import jakarta.persistence.ForeignKey; 027import jakarta.persistence.GeneratedValue; 028import jakarta.persistence.GenerationType; 029import jakarta.persistence.Id; 030import jakarta.persistence.IdClass; 031import jakarta.persistence.Index; 032import jakarta.persistence.JoinColumn; 033import jakarta.persistence.JoinColumns; 034import jakarta.persistence.ManyToOne; 035import jakarta.persistence.PrePersist; 036import jakarta.persistence.Table; 037import jakarta.persistence.UniqueConstraint; 038import org.apache.commons.lang3.builder.EqualsBuilder; 039import org.apache.commons.lang3.builder.HashCodeBuilder; 040import org.apache.commons.lang3.builder.ToStringBuilder; 041import org.apache.commons.lang3.builder.ToStringStyle; 042import org.hibernate.annotations.GenericGenerator; 043 044@Entity 045@Table( 046 name = "HFJ_RES_TAG", 047 indexes = { 048 @Index(name = "IDX_RES_TAG_RES_TAG", columnList = "RES_ID, TAG_ID, PARTITION_ID"), 049 @Index(name = "IDX_RES_TAG_TAG_RES", columnList = "TAG_ID, RES_ID, PARTITION_ID") 050 }, 051 uniqueConstraints = { 052 @UniqueConstraint( 053 name = "IDX_RESTAG_TAGID", 054 columnNames = {"PARTITION_ID", "RES_ID", "TAG_ID"}) 055 }) 056@IdClass(IdAndPartitionId.class) 057public class ResourceTag extends BaseTag { 058 059 private static final long serialVersionUID = 1L; 060 061 @GenericGenerator(name = "SEQ_RESTAG_ID", type = ca.uhn.fhir.jpa.model.dialect.HapiSequenceStyleGenerator.class) 062 @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESTAG_ID") 063 @Id 064 @Column(name = "PID") 065 private Long myId; 066 067 @ManyToOne( 068 cascade = {}, 069 fetch = FetchType.LAZY) 070 @JoinColumns( 071 value = { 072 @JoinColumn( 073 name = "RES_ID", 074 referencedColumnName = "RES_ID", 075 insertable = false, 076 updatable = false, 077 nullable = true), 078 @JoinColumn( 079 name = "PARTITION_ID", 080 referencedColumnName = "PARTITION_ID", 081 insertable = false, 082 updatable = false, 083 nullable = true) 084 }, 085 foreignKey = @ForeignKey(name = "FK_RESTAG_RESOURCE")) 086 private ResourceTable myResource; 087 088 @Column(name = "RES_TYPE", length = ResourceTable.RESTYPE_LEN, nullable = false) 089 private String myResourceType; 090 091 @Column(name = "RES_ID", updatable = false, nullable = true) 092 private Long myResourceId; 093 094 /** 095 * Constructor 096 */ 097 public ResourceTag() { 098 super(); 099 } 100 101 /** 102 * Constructor 103 */ 104 public ResourceTag( 105 ResourceTable theResourceTable, TagDefinition theTag, PartitionablePartitionId theRequestPartitionId) { 106 setTag(theTag); 107 setResource(theResourceTable); 108 setResourceId(theResourceTable.getId().getId()); 109 setResourceType(theResourceTable.getResourceType()); 110 setPartitionId(theRequestPartitionId); 111 } 112 113 public JpaPid getResourceId() { 114 return JpaPid.fromId(myResourceId, myPartitionIdValue); 115 } 116 117 public void setResourceId(Long theResourceId) { 118 myResourceId = theResourceId; 119 } 120 121 public ResourceTable getResource() { 122 return myResource; 123 } 124 125 public void setResource(ResourceTable theResource) { 126 myResource = theResource; 127 myResourceId = theResource.getId().getId(); 128 myPartitionIdValue = theResource.getPartitionId().getPartitionId(); 129 } 130 131 public String getResourceType() { 132 return myResourceType; 133 } 134 135 public void setResourceType(String theResourceType) { 136 myResourceType = theResourceType; 137 } 138 139 @PrePersist 140 public void prePersist() { 141 myResourceId = myResource.getId().getId(); 142 myPartitionIdValue = myResource.getId().getPartitionId(); 143 } 144 145 @Override 146 public boolean equals(Object obj) { 147 if (this == obj) { 148 return true; 149 } 150 if (obj == null) { 151 return false; 152 } 153 if (!(obj instanceof ResourceTag)) { 154 return false; 155 } 156 ResourceTag other = (ResourceTag) obj; 157 EqualsBuilder b = new EqualsBuilder(); 158 b.append(getResourceId(), other.getResourceId()); 159 b.append(getTag(), other.getTag()); 160 return b.isEquals(); 161 } 162 163 @Override 164 public int hashCode() { 165 HashCodeBuilder b = new HashCodeBuilder(); 166 b.append(getResourceId()); 167 b.append(getTag()); 168 return b.toHashCode(); 169 } 170 171 @Override 172 public String toString() { 173 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 174 if (getPartitionId().getPartitionId() != null) { 175 b.append("partition", getPartitionId().getPartitionId()); 176 } 177 b.append("resId", getResourceId()); 178 b.append("tag", getTagId()); 179 return b.build(); 180 } 181}