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