001/*-
002 * #%L
003 * HAPI FHIR JPA Server
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.entity;
021
022import ca.uhn.fhir.jpa.model.entity.BasePartitionable;
023import ca.uhn.fhir.jpa.model.entity.IdAndPartitionId;
024import ca.uhn.fhir.util.ValidateUtil;
025import jakarta.annotation.Nonnull;
026import jakarta.persistence.Column;
027import jakarta.persistence.Entity;
028import jakarta.persistence.EnumType;
029import jakarta.persistence.Enumerated;
030import jakarta.persistence.ForeignKey;
031import jakarta.persistence.GeneratedValue;
032import jakarta.persistence.GenerationType;
033import jakarta.persistence.Id;
034import jakarta.persistence.IdClass;
035import jakarta.persistence.Index;
036import jakarta.persistence.JoinColumn;
037import jakarta.persistence.JoinColumns;
038import jakarta.persistence.ManyToOne;
039import jakarta.persistence.SequenceGenerator;
040import jakarta.persistence.Table;
041import org.apache.commons.lang3.builder.EqualsBuilder;
042import org.apache.commons.lang3.builder.HashCodeBuilder;
043import org.apache.commons.lang3.builder.ToStringBuilder;
044import org.apache.commons.lang3.builder.ToStringStyle;
045import org.hibernate.annotations.JdbcTypeCode;
046import org.hibernate.type.SqlTypes;
047import org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence;
048
049import java.io.Serializable;
050
051import static org.apache.commons.lang3.StringUtils.length;
052
053@Entity
054@Table(
055                name = "TRM_CONCEPT_MAP_GRP_ELM_TGT",
056                indexes = {
057                        @Index(name = "IDX_CNCPT_MP_GRP_ELM_TGT_CD", columnList = "TARGET_CODE"),
058                        @Index(name = "FK_TCMGETARGET_ELEMENT", columnList = "CONCEPT_MAP_GRP_ELM_PID")
059                })
060@IdClass(IdAndPartitionId.class)
061public class TermConceptMapGroupElementTarget extends BasePartitionable implements Serializable {
062        private static final long serialVersionUID = 1L;
063
064        static final int MAX_EQUIVALENCE_LENGTH = 50;
065
066        @Id()
067        @SequenceGenerator(name = "SEQ_CNCPT_MAP_GRP_ELM_TGT_PID", sequenceName = "SEQ_CNCPT_MAP_GRP_ELM_TGT_PID")
068        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CNCPT_MAP_GRP_ELM_TGT_PID")
069        @Column(name = "PID")
070        private Long myId;
071
072        @ManyToOne()
073        @JoinColumns(
074                        value = {
075                                @JoinColumn(
076                                                name = "CONCEPT_MAP_GRP_ELM_PID",
077                                                insertable = true,
078                                                updatable = false,
079                                                nullable = false,
080                                                referencedColumnName = "PID"),
081                                @JoinColumn(
082                                                name = "PARTITION_ID",
083                                                referencedColumnName = "PARTITION_ID",
084                                                insertable = true,
085                                                updatable = false,
086                                                nullable = false)
087                        },
088                        foreignKey = @ForeignKey(name = "FK_TCMGETARGET_ELEMENT"))
089        private TermConceptMapGroupElement myConceptMapGroupElement;
090
091        @Column(name = "TARGET_CODE", nullable = true, length = TermConcept.MAX_CODE_LENGTH)
092        private String myCode;
093
094        @Column(name = "TARGET_DISPLAY", nullable = true, length = TermConcept.MAX_DISP_LENGTH)
095        private String myDisplay;
096
097        @Enumerated(EnumType.STRING)
098        @JdbcTypeCode(SqlTypes.VARCHAR)
099        @Column(name = "TARGET_EQUIVALENCE", nullable = true, length = MAX_EQUIVALENCE_LENGTH)
100        private ConceptMapEquivalence myEquivalence;
101
102        @Column(name = "CONCEPT_MAP_URL", nullable = true, length = TermConceptMap.MAX_URL_LENGTH)
103        private String myConceptMapUrl;
104
105        @Column(name = "SYSTEM_URL", nullable = true, length = TermCodeSystem.MAX_URL_LENGTH)
106        private String mySystem;
107
108        @Column(name = "SYSTEM_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
109        private String mySystemVersion;
110
111        @Column(name = "VALUESET_URL", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
112        private String myValueSet;
113
114        public String getCode() {
115                return myCode;
116        }
117
118        public TermConceptMapGroupElementTarget setCode(@Nonnull String theCode) {
119                ValidateUtil.isNotBlankOrThrowIllegalArgument(theCode, "theCode must not be null or empty");
120                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
121                                theCode,
122                                TermConcept.MAX_CODE_LENGTH,
123                                "Code exceeds maximum length (" + TermConcept.MAX_CODE_LENGTH + "): " + length(theCode));
124                myCode = theCode;
125                return this;
126        }
127
128        public TermConceptMapGroupElement getConceptMapGroupElement() {
129                return myConceptMapGroupElement;
130        }
131
132        public void setConceptMapGroupElement(TermConceptMapGroupElement theTermConceptMapGroupElement) {
133                myConceptMapGroupElement = theTermConceptMapGroupElement;
134                setPartitionId(theTermConceptMapGroupElement.getPartitionId());
135        }
136
137        public String getConceptMapUrl() {
138                if (myConceptMapUrl == null) {
139                        myConceptMapUrl = getConceptMapGroupElement()
140                                        .getConceptMapGroup()
141                                        .getConceptMap()
142                                        .getUrl();
143                }
144                return myConceptMapUrl;
145        }
146
147        public String getDisplay() {
148                return myDisplay;
149        }
150
151        public TermConceptMapGroupElementTarget setDisplay(String theDisplay) {
152                myDisplay = theDisplay;
153                return this;
154        }
155
156        public ConceptMapEquivalence getEquivalence() {
157                return myEquivalence;
158        }
159
160        public TermConceptMapGroupElementTarget setEquivalence(ConceptMapEquivalence theEquivalence) {
161                myEquivalence = theEquivalence;
162                return this;
163        }
164
165        public Long getId() {
166                return myId;
167        }
168
169        public String getSystem() {
170                if (mySystem == null) {
171                        mySystem = getConceptMapGroupElement().getConceptMapGroup().getTarget();
172                }
173                return mySystem;
174        }
175
176        public String getSystemVersion() {
177                if (mySystemVersion == null) {
178                        mySystemVersion = getConceptMapGroupElement().getConceptMapGroup().getTargetVersion();
179                }
180                return mySystemVersion;
181        }
182
183        public String getValueSet() {
184                if (myValueSet == null) {
185                        myValueSet = getConceptMapGroupElement().getConceptMapGroup().getTargetValueSet();
186                }
187                return myValueSet;
188        }
189
190        @Override
191        public boolean equals(Object o) {
192                if (this == o) return true;
193
194                if (!(o instanceof TermConceptMapGroupElementTarget)) return false;
195
196                TermConceptMapGroupElementTarget that = (TermConceptMapGroupElementTarget) o;
197
198                return new EqualsBuilder()
199                                .append(getCode(), that.getCode())
200                                .append(getEquivalence(), that.getEquivalence())
201                                .append(getSystem(), that.getSystem())
202                                .append(getSystemVersion(), that.getSystemVersion())
203                                .isEquals();
204        }
205
206        @Override
207        public int hashCode() {
208                return new HashCodeBuilder(17, 37)
209                                .append(getCode())
210                                .append(getEquivalence())
211                                .append(getSystem())
212                                .append(getSystemVersion())
213                                .toHashCode();
214        }
215
216        @Override
217        public String toString() {
218                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
219                                .append("myId", myId)
220                                .append(
221                                                myConceptMapGroupElement != null
222                                                                ? ("myConceptMapGroupElement - id=" + myConceptMapGroupElement.getId())
223                                                                : ("myConceptMapGroupElement=(null)"))
224                                .append("myCode", myCode)
225                                .append("myDisplay", myDisplay)
226                                .append("myEquivalence", myEquivalence.toCode())
227                                .append("myConceptMapUrl", this.getConceptMapUrl())
228                                .append("mySystem", this.getSystem())
229                                .append("mySystemVersion", this.getSystemVersion())
230                                .append("myValueSet", this.getValueSet())
231                                .toString();
232        }
233}