001package ca.uhn.fhir.jpa.model.entity;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Model
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.apache.commons.lang3.builder.ToStringBuilder;
024import org.apache.commons.lang3.builder.ToStringStyle;
025import org.hibernate.annotations.ColumnDefault;
026
027import javax.persistence.Column;
028import javax.persistence.Entity;
029import javax.persistence.FetchType;
030import javax.persistence.ForeignKey;
031import javax.persistence.GeneratedValue;
032import javax.persistence.GenerationType;
033import javax.persistence.Id;
034import javax.persistence.Index;
035import javax.persistence.JoinColumn;
036import javax.persistence.OneToOne;
037import javax.persistence.SequenceGenerator;
038import javax.persistence.Table;
039import javax.persistence.UniqueConstraint;
040
041@Entity()
042@Table(name = ForcedId.HFJ_FORCED_ID, uniqueConstraints = {
043        @UniqueConstraint(name = "IDX_FORCEDID_RESID", columnNames = {"RESOURCE_PID"}),
044        /*
045         * This index is called IDX_FORCEDID_TYPE_FID and guarantees
046         * uniqueness of RESOURCE_TYPE,FORCED_ID. This doesn't make sense
047         * for partitioned servers, so we replace it on those servers
048         * with IDX_FORCEDID_TYPE_PFID covering
049         * PARTITION_ID,RESOURCE_TYPE,FORCED_ID
050         */
051        @UniqueConstraint(name = ForcedId.IDX_FORCEDID_TYPE_FID, columnNames = {"RESOURCE_TYPE", "FORCED_ID"})
052}, indexes = {
053        /*
054         * NB: We previously had indexes named
055         * - IDX_FORCEDID_TYPE_FORCEDID
056         * - IDX_FORCEDID_TYPE_RESID
057         * so don't reuse these names
058         */
059        @Index(name = "IDX_FORCEID_FID", columnList = "FORCED_ID"),
060        //@Index(name = "IDX_FORCEID_RESID", columnList = "RESOURCE_PID"),
061        //TODO GGG potentiall add a type + res_id index here, specifically for deletion?
062})
063public class ForcedId extends BasePartitionable {
064
065        public static final int MAX_FORCED_ID_LENGTH = 100;
066        public static final String IDX_FORCEDID_TYPE_FID = "IDX_FORCEDID_TYPE_FID";
067        public static final String HFJ_FORCED_ID = "HFJ_FORCED_ID";
068
069        @Column(name = "FORCED_ID", nullable = false, length = MAX_FORCED_ID_LENGTH, updatable = false)
070        private String myForcedId;
071
072        @SequenceGenerator(name = "SEQ_FORCEDID_ID", sequenceName = "SEQ_FORCEDID_ID")
073        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_FORCEDID_ID")
074        @Id
075        @Column(name = "PID")
076        private Long myId;
077
078        @JoinColumn(name = "RESOURCE_PID", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "FK_FORCEDID_RESOURCE"))
079        @OneToOne(fetch = FetchType.LAZY)
080        private ResourceTable myResource;
081
082        @Column(name = "RESOURCE_PID", nullable = false, updatable = false, insertable = false)
083        private Long myResourcePid;
084
085        // This is updatable=true because it was added in 1.6 and needs to be set.. At some
086        // point we should remove the default and make it not updatable
087        @ColumnDefault("''")
088        @Column(name = "RESOURCE_TYPE", nullable = true, length = 100, updatable = true)
089        private String myResourceType;
090
091        /**
092         * Constructor
093         */
094        public ForcedId() {
095                super();
096        }
097
098        public String getForcedId() {
099                return myForcedId;
100        }
101
102        public void setForcedId(String theForcedId) {
103                myForcedId = theForcedId;
104        }
105
106        public void setResource(ResourceTable theResource) {
107                myResource = theResource;
108        }
109
110        public String getResourceType() {
111                return myResourceType;
112        }
113
114        public void setResourceType(String theResourceType) {
115                myResourceType = theResourceType;
116        }
117
118        public Long getId() {
119                return myId;
120        }
121
122        public Long getResourceId() {
123                return myResourcePid;
124        }
125
126        @Override
127        public String toString() {
128                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
129                b.append("pid", myId);
130                if (getPartitionId() != null) {
131                        b.append("partitionId", getPartitionId().getPartitionId());
132                }
133                b.append("resourceType", myResourceType);
134                b.append("forcedId", myForcedId);
135                b.append("resourcePid", myResourcePid);
136                return b.toString();
137        }
138}