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.ConstraintMode;
025import jakarta.persistence.Entity;
026import jakarta.persistence.FetchType;
027import jakarta.persistence.ForeignKey;
028import jakarta.persistence.GeneratedValue;
029import jakarta.persistence.GenerationType;
030import jakarta.persistence.Id;
031import jakarta.persistence.IdClass;
032import jakarta.persistence.Index;
033import jakarta.persistence.JoinColumn;
034import jakarta.persistence.JoinColumns;
035import jakarta.persistence.ManyToOne;
036import jakarta.persistence.PrePersist;
037import jakarta.persistence.Table;
038import jakarta.persistence.UniqueConstraint;
039import org.apache.commons.lang3.builder.EqualsBuilder;
040import org.apache.commons.lang3.builder.HashCodeBuilder;
041import org.apache.commons.lang3.builder.ToStringBuilder;
042import org.apache.commons.lang3.builder.ToStringStyle;
043import org.hibernate.annotations.GenericGenerator;
044
045@Entity
046@Table(
047                name = "HFJ_RES_TAG",
048                indexes = {
049                        @Index(name = "IDX_RES_TAG_RES_TAG", columnList = "RES_ID, TAG_ID, PARTITION_ID"),
050                        @Index(name = "IDX_RES_TAG_TAG_RES", columnList = "TAG_ID, RES_ID, PARTITION_ID")
051                },
052                uniqueConstraints = {
053                        @UniqueConstraint(
054                                        name = "IDX_RESTAG_TAGID",
055                                        columnNames = {"PARTITION_ID", "RES_ID", "TAG_ID"})
056                })
057@IdClass(IdAndPartitionId.class)
058public class ResourceTag extends BaseTag {
059
060        private static final long serialVersionUID = 1L;
061
062        @GenericGenerator(name = "SEQ_RESTAG_ID", type = ca.uhn.fhir.jpa.model.dialect.HapiSequenceStyleGenerator.class)
063        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESTAG_ID")
064        @Id
065        @Column(name = "PID")
066        private Long myId;
067
068        @ManyToOne(
069                        cascade = {},
070                        fetch = FetchType.LAZY)
071        @JoinColumns(
072                        value = {
073                                @JoinColumn(
074                                                name = "RES_ID",
075                                                referencedColumnName = "RES_ID",
076                                                insertable = false,
077                                                updatable = false,
078                                                nullable = true),
079                                @JoinColumn(
080                                                name = "PARTITION_ID",
081                                                referencedColumnName = "PARTITION_ID",
082                                                insertable = false,
083                                                updatable = false,
084                                                nullable = true)
085                        },
086                        foreignKey = @ForeignKey(name = "FK_RESTAG_RESOURCE"))
087        private ResourceTable myResource;
088
089        @Column(name = "RES_TYPE", length = ResourceTable.RESTYPE_LEN, nullable = false)
090        private String myResourceType;
091
092        @Column(name = "RES_ID", updatable = false, nullable = true)
093        private Long myResourceId;
094
095        @ManyToOne(fetch = FetchType.LAZY)
096        @JoinColumn(
097                        name = "RES_TYPE_ID",
098                        referencedColumnName = "RES_TYPE_ID",
099                        foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
100                        insertable = false,
101                        updatable = false,
102                        nullable = true)
103        private ResourceTypeEntity myResourceTypeEntity;
104
105        @Column(name = "RES_TYPE_ID", nullable = true)
106        private Short myResourceTypeId;
107
108        /**
109         * Constructor
110         */
111        public ResourceTag() {
112                super();
113        }
114
115        /**
116         * Constructor
117         */
118        public ResourceTag(
119                        ResourceTable theResourceTable, TagDefinition theTag, PartitionablePartitionId theRequestPartitionId) {
120                setTag(theTag);
121                setResource(theResourceTable);
122                setResourceId(theResourceTable.getId().getId());
123                setResourceType(theResourceTable.getResourceType());
124                setResourceTypeId(theResourceTable.getResourceTypeId());
125                setPartitionId(theRequestPartitionId);
126        }
127
128        public JpaPid getResourceId() {
129                return JpaPid.fromId(myResourceId, myPartitionIdValue);
130        }
131
132        public void setResourceId(Long theResourceId) {
133                myResourceId = theResourceId;
134        }
135
136        public ResourceTable getResource() {
137                return myResource;
138        }
139
140        public void setResource(ResourceTable theResource) {
141                myResource = theResource;
142                myResourceId = theResource.getId().getId();
143                myPartitionIdValue = theResource.getPartitionId().getPartitionId();
144        }
145
146        public String getResourceType() {
147                return myResourceType;
148        }
149
150        public void setResourceType(String theResourceType) {
151                myResourceType = theResourceType;
152        }
153
154        public Short getResourceTypeId() {
155                return myResourceTypeId;
156        }
157
158        public void setResourceTypeId(Short theResourceTypeId) {
159                myResourceTypeId = theResourceTypeId;
160        }
161
162        public ResourceTypeEntity getResourceTypeEntity() {
163                return myResourceTypeEntity;
164        }
165
166        @PrePersist
167        public void prePersist() {
168                myResourceId = myResource.getId().getId();
169                myPartitionIdValue = myResource.getId().getPartitionId();
170        }
171
172        @Override
173        public boolean equals(Object obj) {
174                if (this == obj) {
175                        return true;
176                }
177                if (obj == null) {
178                        return false;
179                }
180                if (!(obj instanceof ResourceTag)) {
181                        return false;
182                }
183                ResourceTag other = (ResourceTag) obj;
184                EqualsBuilder b = new EqualsBuilder();
185                b.append(getResourceId(), other.getResourceId());
186                b.append(getTag(), other.getTag());
187                return b.isEquals();
188        }
189
190        @Override
191        public int hashCode() {
192                HashCodeBuilder b = new HashCodeBuilder();
193                b.append(getResourceId());
194                b.append(getTag());
195                return b.toHashCode();
196        }
197
198        @Override
199        public String toString() {
200                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
201                if (getPartitionId().getPartitionId() != null) {
202                        b.append("partition", getPartitionId().getPartitionId());
203                }
204                b.append("resId", getResourceId());
205                b.append("resTypeId", getResourceTypeId());
206                b.append("tag", getTagId());
207                return b.build();
208        }
209}