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.rest.api.Constants;
023import jakarta.persistence.Column;
024import jakarta.persistence.Entity;
025import jakarta.persistence.FetchType;
026import jakarta.persistence.ForeignKey;
027import jakarta.persistence.Id;
028import jakarta.persistence.IdClass;
029import jakarta.persistence.Index;
030import jakarta.persistence.JoinColumn;
031import jakarta.persistence.JoinColumns;
032import jakarta.persistence.ManyToOne;
033import jakarta.persistence.Table;
034import org.apache.commons.lang3.builder.ToStringBuilder;
035import org.apache.commons.lang3.builder.ToStringStyle;
036
037import static ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable.SOURCE_URI_LENGTH;
038
039/**
040 * This entity is deprecated - It stores the source URI and Request ID
041 * fields so that they can be indexed and searched discretely. In
042 * HAPI FHIR 6.8.0 we added equivalent columns to {@link ResourceHistoryTable}
043 * and started populating both those columns and the ones in this table.
044 * As of HAPI FHIR 8.0.0 we are no longer using this table unless
045 * the "AccessMetaSourceInformationFromProvenanceTable" on JpaStorageSettings
046 * is enabled (it's disabled by default). In the future we will remove
047 * this table entirely.
048 */
049@Table(
050                name = "HFJ_RES_VER_PROV",
051                indexes = {
052                        @Index(name = "IDX_RESVERPROV_SOURCEURI", columnList = "SOURCE_URI"),
053                        @Index(name = "IDX_RESVERPROV_REQUESTID", columnList = "REQUEST_ID"),
054                        @Index(name = "IDX_RESVERPROV_RES_PID", columnList = "RES_PID")
055                })
056@Entity
057@IdClass(IdAndPartitionId.class)
058public class ResourceHistoryProvenanceEntity extends BasePartitionable {
059
060        @Id
061        @Column(name = "RES_VER_PID")
062        private Long myId;
063
064        @ManyToOne(fetch = FetchType.LAZY)
065        @JoinColumns(
066                        value = {
067                                @JoinColumn(
068                                                name = "RES_PID",
069                                                referencedColumnName = "RES_ID",
070                                                insertable = false,
071                                                updatable = false,
072                                                nullable = false),
073                                @JoinColumn(
074                                                name = "PARTITION_ID",
075                                                referencedColumnName = "PARTITION_ID",
076                                                insertable = false,
077                                                updatable = false,
078                                                nullable = false)
079                        },
080                        foreignKey = @ForeignKey(name = "FK_RESVERPROV_RES_PID"))
081        private ResourceTable myResourceTable;
082
083        @Column(name = "RES_PID", nullable = false)
084        private Long myResourceId;
085
086        @Column(name = "SOURCE_URI", length = SOURCE_URI_LENGTH, nullable = true)
087        private String mySourceUri;
088
089        @Column(name = "REQUEST_ID", length = Constants.REQUEST_ID_LENGTH, nullable = true)
090        private String myRequestId;
091
092        /**
093         * Constructor
094         */
095        public ResourceHistoryProvenanceEntity() {
096                super();
097        }
098
099        @Override
100        public String toString() {
101                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
102                b.append("resourceId", myResourceTable.getId());
103                b.append("sourceUri", mySourceUri);
104                b.append("requestId", myRequestId);
105                return b.toString();
106        }
107
108        public void setResourceTable(ResourceTable theResourceTable) {
109                myResourceTable = theResourceTable;
110                myResourceId = theResourceTable.getId().getId();
111                myPartitionIdValue = theResourceTable.getPartitionId().getPartitionId();
112        }
113
114        public void setResourceHistoryTable(ResourceHistoryTable theResourceHistoryTable) {
115                myId = theResourceHistoryTable.getId().getId();
116                assert myId != null;
117        }
118
119        public String getSourceUri() {
120                return mySourceUri;
121        }
122
123        public void setSourceUri(String theSourceUri) {
124                mySourceUri = theSourceUri;
125        }
126
127        public String getRequestId() {
128                return myRequestId;
129        }
130
131        public void setRequestId(String theRequestId) {
132                myRequestId = theRequestId;
133        }
134
135        public Long getId() {
136                return myId;
137        }
138}