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