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.ToStringBuilder;
040import org.apache.commons.lang3.builder.ToStringStyle;
041import org.hibernate.annotations.GenericGenerator;
042
043import java.io.Serializable;
044
045@Entity
046@Table(
047                name = "HFJ_HISTORY_TAG",
048                uniqueConstraints = {
049                        @UniqueConstraint(
050                                        name = "IDX_RESHISTTAG_TAGID",
051                                        columnNames = {"PARTITION_ID", "RES_VER_PID", "TAG_ID"}),
052                },
053                indexes = {@Index(name = "IDX_RESHISTTAG_RESID", columnList = "RES_ID")})
054@IdClass(IdAndPartitionId.class)
055public class ResourceHistoryTag extends BaseTag implements Serializable {
056
057        private static final long serialVersionUID = 1L;
058
059        @GenericGenerator(name = "SEQ_HISTORYTAG_ID", type = ca.uhn.fhir.jpa.model.dialect.HapiSequenceStyleGenerator.class)
060        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_HISTORYTAG_ID")
061        @Id
062        @Column(name = "PID")
063        private Long myId;
064
065        @ManyToOne()
066        @JoinColumns(
067                        value = {
068                                @JoinColumn(
069                                                name = "RES_VER_PID",
070                                                referencedColumnName = "PID",
071                                                nullable = false,
072                                                insertable = false,
073                                                updatable = false),
074                                @JoinColumn(
075                                                name = "PARTITION_ID",
076                                                referencedColumnName = "PARTITION_ID",
077                                                nullable = false,
078                                                insertable = false,
079                                                updatable = false)
080                        },
081                        foreignKey = @ForeignKey(name = "FK_HISTORYTAG_HISTORY"))
082        private ResourceHistoryTable myResourceHistory;
083
084        @Column(name = "RES_VER_PID", updatable = false, nullable = false)
085        private Long myResourceHistoryPid;
086
087        @Column(name = "RES_TYPE", length = ResourceTable.RESTYPE_LEN, nullable = false)
088        private String myResourceType;
089
090        @Column(name = "RES_ID", nullable = false)
091        private Long myResourceId;
092
093        @ManyToOne(fetch = FetchType.LAZY)
094        @JoinColumn(
095                        name = "RES_TYPE_ID",
096                        referencedColumnName = "RES_TYPE_ID",
097                        foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
098                        insertable = false,
099                        updatable = false,
100                        nullable = true)
101        private ResourceTypeEntity myResourceTypeEntity;
102
103        @Column(name = "RES_TYPE_ID", nullable = true)
104        private Short myResourceTypeId;
105        /**
106         * Constructor
107         */
108        public ResourceHistoryTag() {
109                super();
110        }
111
112        /**
113         * Constructor
114         */
115        public ResourceHistoryTag(
116                        ResourceHistoryTable theResourceHistoryTable,
117                        TagDefinition theTag,
118                        PartitionablePartitionId theRequestPartitionId) {
119                this();
120                setTag(theTag);
121                setResource(theResourceHistoryTable);
122                setResourceId(theResourceHistoryTable.getResourceId().getId());
123                setResourceType(theResourceHistoryTable.getResourceType());
124                setResourceTypeId(theResourceHistoryTable.getResourceTypeId());
125                setPartitionId(theRequestPartitionId);
126        }
127
128        public String getResourceType() {
129                return myResourceType;
130        }
131
132        public void setResourceType(String theResourceType) {
133                myResourceType = theResourceType;
134        }
135
136        public Long getResourceId() {
137                return myResourceId;
138        }
139
140        public void setResourceId(Long theResourceId) {
141                myResourceId = theResourceId;
142        }
143
144        public Short getResourceTypeId() {
145                return myResourceTypeId;
146        }
147
148        public void setResourceTypeId(Short theResourceTypeId) {
149                myResourceTypeId = theResourceTypeId;
150        }
151
152        public ResourceTypeEntity getResourceTypeEntity() {
153                return myResourceTypeEntity;
154        }
155
156        public ResourceHistoryTable getResourceHistory() {
157                return myResourceHistory;
158        }
159
160        public void setResource(ResourceHistoryTable theResourceHistory) {
161                myResourceHistory = theResourceHistory;
162                myResourceHistoryPid = theResourceHistory.getId().getId();
163                myPartitionIdValue = theResourceHistory.getResourceId().getPartitionId();
164        }
165
166        @PrePersist
167        public void prePersist() {
168                myResourceHistoryPid = myResourceHistory.getId().getId();
169                myPartitionIdValue = myResourceHistory.getResourceId().getPartitionId();
170        }
171
172        public Long getId() {
173                return myId;
174        }
175
176        public JpaPid getResourcePid() {
177                return JpaPid.fromId(myResourceId, myPartitionIdValue);
178        }
179
180        @Override
181        public String toString() {
182                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
183                b.append("id", getId());
184                if (getPartitionId().getPartitionId() != null) {
185                        b.append("partId", getPartitionId().getPartitionId());
186                }
187                b.append("versionId", myResourceHistoryPid);
188                b.append("resId", getResourceId());
189                b.append("resTypeId", getResourceTypeId());
190                b.append("tag", getTagId());
191                return b.build();
192        }
193}