001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.entity;
021
022import jakarta.persistence.Column;
023import jakarta.persistence.Entity;
024import jakarta.persistence.EnumType;
025import jakarta.persistence.Enumerated;
026import jakarta.persistence.FetchType;
027import jakarta.persistence.ForeignKey;
028import jakarta.persistence.GeneratedValue;
029import jakarta.persistence.GenerationType;
030import jakarta.persistence.Id;
031import jakarta.persistence.Index;
032import jakarta.persistence.JoinColumn;
033import jakarta.persistence.ManyToOne;
034import jakarta.persistence.SequenceGenerator;
035import jakarta.persistence.Table;
036import org.hibernate.annotations.JdbcTypeCode;
037import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
038import org.hibernate.type.SqlTypes;
039
040import java.io.Serializable;
041
042@Entity
043@Table(
044                name = "TRM_CONCEPT_PC_LINK",
045                indexes = {
046                        // must have same name that indexed FK or SchemaMigrationTest complains because H2 sets this index
047                        // automatically
048                        @Index(name = "FK_TERM_CONCEPTPC_CHILD", columnList = "CHILD_PID", unique = false),
049                        @Index(name = "FK_TERM_CONCEPTPC_PARENT", columnList = "PARENT_PID", unique = false),
050                        @Index(name = "FK_TERM_CONCEPTPC_CS", columnList = "CODESYSTEM_PID")
051                })
052public class TermConceptParentChildLink implements Serializable {
053        private static final long serialVersionUID = 1L;
054
055        @ManyToOne(fetch = FetchType.LAZY)
056        @JoinColumn(
057                        name = "CHILD_PID",
058                        nullable = false,
059                        referencedColumnName = "PID",
060                        foreignKey = @ForeignKey(name = "FK_TERM_CONCEPTPC_CHILD"))
061        private TermConcept myChild;
062
063        @Column(name = "CHILD_PID", insertable = false, updatable = false)
064        private Long myChildPid;
065
066        @ManyToOne(fetch = FetchType.LAZY)
067        @JoinColumn(name = "CODESYSTEM_PID", nullable = false, foreignKey = @ForeignKey(name = "FK_TERM_CONCEPTPC_CS"))
068        private TermCodeSystemVersion myCodeSystem;
069
070        @Column(name = "CODESYSTEM_PID", insertable = false, updatable = false, nullable = false)
071        @FullTextField(name = "myCodeSystemVersionPid")
072        private long myCodeSystemVersionPid;
073
074        @ManyToOne(
075                        fetch = FetchType.LAZY,
076                        cascade = {})
077        @JoinColumn(
078                        name = "PARENT_PID",
079                        nullable = false,
080                        referencedColumnName = "PID",
081                        foreignKey = @ForeignKey(name = "FK_TERM_CONCEPTPC_PARENT"))
082        private TermConcept myParent;
083
084        @Column(name = "PARENT_PID", insertable = false, updatable = false)
085        private Long myParentPid;
086
087        @Id()
088        @SequenceGenerator(name = "SEQ_CONCEPT_PC_PID", sequenceName = "SEQ_CONCEPT_PC_PID")
089        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_PC_PID")
090        @Column(name = "PID")
091        private Long myPid;
092
093        @Enumerated(EnumType.ORDINAL)
094        @Column(name = "REL_TYPE", length = 5, nullable = true)
095        @JdbcTypeCode(SqlTypes.INTEGER)
096        private RelationshipTypeEnum myRelationshipType;
097
098        @Override
099        public boolean equals(Object obj) {
100                if (this == obj) return true;
101                if (obj == null) return false;
102                if (getClass() != obj.getClass()) return false;
103                TermConceptParentChildLink other = (TermConceptParentChildLink) obj;
104                if (myChild == null) {
105                        if (other.myChild != null) return false;
106                } else if (!myChild.equals(other.myChild)) return false;
107                if (myCodeSystem == null) {
108                        if (other.myCodeSystem != null) return false;
109                } else if (!myCodeSystem.equals(other.myCodeSystem)) return false;
110                if (myParent == null) {
111                        if (other.myParent != null) return false;
112                } else if (!myParent.equals(other.myParent)) return false;
113                if (myRelationshipType != other.myRelationshipType) return false;
114                return true;
115        }
116
117        public TermConcept getChild() {
118                return myChild;
119        }
120
121        public Long getChildPid() {
122                return myChildPid;
123        }
124
125        public TermCodeSystemVersion getCodeSystem() {
126                return myCodeSystem;
127        }
128
129        public Long getId() {
130                return myPid;
131        }
132
133        public TermConcept getParent() {
134                return myParent;
135        }
136
137        public Long getParentPid() {
138                return myParentPid;
139        }
140
141        public RelationshipTypeEnum getRelationshipType() {
142                return myRelationshipType;
143        }
144
145        @Override
146        public int hashCode() {
147                final int prime = 31;
148                int result = 1;
149                result = prime * result + ((myChild == null) ? 0 : myChild.hashCode());
150                result = prime * result + ((myCodeSystem == null) ? 0 : myCodeSystem.hashCode());
151                result = prime * result + ((myParent == null) ? 0 : myParent.hashCode());
152                result = prime * result + ((myRelationshipType == null) ? 0 : myRelationshipType.hashCode());
153                return result;
154        }
155
156        public TermConceptParentChildLink setChild(TermConcept theChild) {
157                myChild = theChild;
158                return this;
159        }
160
161        public TermConceptParentChildLink setCodeSystem(TermCodeSystemVersion theCodeSystem) {
162                myCodeSystem = theCodeSystem;
163                return this;
164        }
165
166        public TermConceptParentChildLink setParent(TermConcept theParent) {
167                myParent = theParent;
168                return this;
169        }
170
171        public TermConceptParentChildLink setRelationshipType(RelationshipTypeEnum theRelationshipType) {
172                myRelationshipType = theRelationshipType;
173                return this;
174        }
175
176        public enum RelationshipTypeEnum {
177                // ********************************************
178                // IF YOU ADD HERE MAKE SURE ORDER IS PRESERVED
179                ISA
180        }
181}