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.FetchType;
029import jakarta.persistence.ForeignKey;
030import jakarta.persistence.GeneratedValue;
031import jakarta.persistence.GenerationType;
032import jakarta.persistence.Id;
033import jakarta.persistence.IdClass;
034import jakarta.persistence.Index;
035import jakarta.persistence.JoinColumn;
036import jakarta.persistence.JoinColumns;
037import jakarta.persistence.ManyToOne;
038import jakarta.persistence.SequenceGenerator;
039import jakarta.persistence.Table;
040import jakarta.persistence.Transient;
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;
045
046import java.io.Serializable;
047
048import static org.apache.commons.lang3.StringUtils.left;
049import static org.apache.commons.lang3.StringUtils.length;
050
051@Table(
052                name = "TRM_VALUESET_C_DESIGNATION",
053                indexes = {
054                        // must have same name that indexed FK or SchemaMigrationTest complains because H2 sets this index
055                        // automatically
056                        @Index(name = "FK_TRM_VALUESET_CONCEPT_PID", columnList = "VALUESET_CONCEPT_PID", unique = false),
057                        @Index(name = "FK_TRM_VSCD_VS_PID", columnList = "VALUESET_PID")
058                })
059@Entity()
060@IdClass(IdAndPartitionId.class)
061public class TermValueSetConceptDesignation extends BasePartitionable implements Serializable {
062        private static final long serialVersionUID = 1L;
063
064        /** Constructor */
065        public TermValueSetConceptDesignation() {
066                super();
067        }
068
069        @Id()
070        @SequenceGenerator(name = "SEQ_VALUESET_C_DSGNTN_PID", sequenceName = "SEQ_VALUESET_C_DSGNTN_PID")
071        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_VALUESET_C_DSGNTN_PID")
072        @Column(name = "PID")
073        private Long myId;
074
075        @ManyToOne(fetch = FetchType.LAZY)
076        @JoinColumns(
077                        value = {
078                                @JoinColumn(
079                                                name = "VALUESET_CONCEPT_PID",
080                                                referencedColumnName = "PID",
081                                                insertable = true,
082                                                updatable = false,
083                                                nullable = false),
084                                @JoinColumn(
085                                                name = "PARTITION_ID",
086                                                referencedColumnName = "PARTITION_ID",
087                                                insertable = true,
088                                                updatable = false,
089                                                nullable = false)
090                        },
091                        foreignKey = @ForeignKey(name = "FK_TRM_VALUESET_CONCEPT_PID"))
092        private TermValueSetConcept myConcept;
093
094        @Column(name = "VALUESET_CONCEPT_PID", insertable = false, updatable = false, nullable = false)
095        private Long myConceptPid;
096
097        @ManyToOne(fetch = FetchType.LAZY)
098        @JoinColumns(
099                        value = {
100                                @JoinColumn(
101                                                name = "VALUESET_PID",
102                                                referencedColumnName = "PID",
103                                                insertable = false,
104                                                updatable = false,
105                                                nullable = false),
106                                @JoinColumn(
107                                                name = "PARTITION_ID",
108                                                referencedColumnName = "PARTITION_ID",
109                                                insertable = false,
110                                                updatable = false,
111                                                nullable = false)
112                        },
113                        foreignKey = @ForeignKey(name = "FK_TRM_VSCD_VS_PID"))
114        private TermValueSet myValueSet;
115
116        @Column(name = "VALUESET_PID", insertable = true, updatable = true, nullable = false)
117        private Long myValueSetPid;
118
119        @Transient
120        private String myValueSetUrl;
121
122        @Transient
123        private String myValueSetName;
124
125        @Column(name = "LANG", nullable = true, length = TermConceptDesignation.MAX_LENGTH)
126        private String myLanguage;
127
128        @Column(name = "USE_SYSTEM", nullable = true, length = TermConceptDesignation.MAX_LENGTH)
129        private String myUseSystem;
130
131        @Column(name = "USE_CODE", nullable = true, length = TermConceptDesignation.MAX_LENGTH)
132        private String myUseCode;
133
134        @Column(name = "USE_DISPLAY", nullable = true, length = TermConceptDesignation.MAX_LENGTH)
135        private String myUseDisplay;
136
137        @Column(name = "VAL", nullable = false, length = TermConceptDesignation.MAX_VAL_LENGTH)
138        private String myValue;
139
140        @Transient
141        private transient Integer myHashCode;
142
143        public Long getId() {
144                return myId;
145        }
146
147        public IdAndPartitionId getPartitionedId() {
148                return IdAndPartitionId.forId(myId, this);
149        }
150
151        public TermValueSetConcept getConcept() {
152                return myConcept;
153        }
154
155        public TermValueSetConceptDesignation setConcept(TermValueSetConcept theConcept) {
156                myConcept = theConcept;
157                setPartitionId(theConcept.getPartitionId());
158                return this;
159        }
160
161        public TermValueSet getValueSet() {
162                return myValueSet;
163        }
164
165        public TermValueSetConceptDesignation setValueSet(TermValueSet theValueSet) {
166                myValueSet = theValueSet;
167                myValueSetPid = theValueSet.getId();
168                assert myValueSetPid != null;
169                return this;
170        }
171
172        public String getValueSetUrl() {
173                if (myValueSetUrl == null) {
174                        myValueSetUrl = getValueSet().getUrl();
175                }
176
177                return myValueSetUrl;
178        }
179
180        public String getValueSetName() {
181                if (myValueSetName == null) {
182                        myValueSetName = getValueSet().getName();
183                }
184
185                return myValueSetName;
186        }
187
188        public String getLanguage() {
189                return myLanguage;
190        }
191
192        public TermValueSetConceptDesignation setLanguage(String theLanguage) {
193                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
194                                theLanguage,
195                                TermConceptDesignation.MAX_LENGTH,
196                                "Language exceeds maximum length (" + TermConceptDesignation.MAX_LENGTH + "): " + length(theLanguage));
197                myLanguage = theLanguage;
198                return this;
199        }
200
201        public String getUseSystem() {
202                return myUseSystem;
203        }
204
205        public TermValueSetConceptDesignation setUseSystem(String theUseSystem) {
206                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
207                                theUseSystem,
208                                TermConceptDesignation.MAX_LENGTH,
209                                "Use system exceeds maximum length (" + TermConceptDesignation.MAX_LENGTH + "): "
210                                                + length(theUseSystem));
211                myUseSystem = theUseSystem;
212                return this;
213        }
214
215        public String getUseCode() {
216                return myUseCode;
217        }
218
219        public TermValueSetConceptDesignation setUseCode(String theUseCode) {
220                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
221                                theUseCode,
222                                TermConceptDesignation.MAX_LENGTH,
223                                "Use code exceeds maximum length (" + TermConceptDesignation.MAX_LENGTH + "): " + length(theUseCode));
224                myUseCode = theUseCode;
225                return this;
226        }
227
228        public String getUseDisplay() {
229                return myUseDisplay;
230        }
231
232        public TermValueSetConceptDesignation setUseDisplay(String theUseDisplay) {
233                myUseDisplay = left(theUseDisplay, TermConceptDesignation.MAX_LENGTH);
234                return this;
235        }
236
237        public String getValue() {
238                return myValue;
239        }
240
241        public TermValueSetConceptDesignation setValue(@Nonnull String theValue) {
242                ValidateUtil.isNotBlankOrThrowIllegalArgument(theValue, "theValue must not be null or empty");
243                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
244                                theValue,
245                                TermConceptDesignation.MAX_VAL_LENGTH,
246                                "Value exceeds maximum length (" + TermConceptDesignation.MAX_VAL_LENGTH + "): " + length(theValue));
247                myValue = theValue;
248                return this;
249        }
250
251        @Override
252        public boolean equals(Object theO) {
253                if (this == theO) return true;
254
255                if (!(theO instanceof TermValueSetConceptDesignation)) return false;
256
257                TermValueSetConceptDesignation that = (TermValueSetConceptDesignation) theO;
258
259                return new EqualsBuilder()
260                                .append(getLanguage(), that.getLanguage())
261                                .append(getUseSystem(), that.getUseSystem())
262                                .append(getUseCode(), that.getUseCode())
263                                .append(getUseDisplay(), that.getUseDisplay())
264                                .append(getValue(), that.getValue())
265                                .isEquals();
266        }
267
268        @Override
269        public int hashCode() {
270                if (myHashCode == null) {
271                        myHashCode = new HashCodeBuilder(17, 37)
272                                        .append(getLanguage())
273                                        .append(getUseSystem())
274                                        .append(getUseCode())
275                                        .append(getUseDisplay())
276                                        .append(getValue())
277                                        .toHashCode();
278                }
279                return myHashCode;
280        }
281
282        @Override
283        public String toString() {
284                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
285                                .append("myId", myId)
286                                .append(myConcept != null ? ("myConcept - id=" + myConcept.getId()) : ("myConcept=(null)"))
287                                .append("myConceptPid", myConceptPid)
288                                .append(myValueSet != null ? ("myValueSet - id=" + myValueSet.getId()) : ("myValueSet=(null)"))
289                                .append("myValueSetPid", myValueSetPid)
290                                .append("myValueSetUrl", this.getValueSetUrl())
291                                .append("myValueSetName", this.getValueSetName())
292                                .append("myLanguage", myLanguage)
293                                .append("myUseSystem", myUseSystem)
294                                .append("myUseCode", myUseCode)
295                                .append("myUseDisplay", myUseDisplay)
296                                .append("myValue", myValue)
297                                .toString();
298        }
299}