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 ca.uhn.fhir.util.ValidateUtil;
023import jakarta.annotation.Nonnull;
024import jakarta.persistence.Column;
025import jakarta.persistence.Entity;
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.apache.commons.lang3.builder.ToStringBuilder;
037import org.apache.commons.lang3.builder.ToStringStyle;
038
039import java.io.Serializable;
040
041import static org.apache.commons.lang3.StringUtils.left;
042import static org.apache.commons.lang3.StringUtils.length;
043
044@Entity
045@Table(
046                name = "TRM_CONCEPT_DESIG",
047                uniqueConstraints = {},
048                indexes = {
049                        // must have same name that indexed FK or SchemaMigrationTest complains because H2 sets this index
050                        // automatically
051                        @Index(name = "FK_CONCEPTDESIG_CONCEPT", columnList = "CONCEPT_PID", unique = false),
052                        @Index(name = "FK_CONCEPTDESIG_CSV", columnList = "CS_VER_PID")
053                })
054public class TermConceptDesignation implements Serializable {
055        private static final long serialVersionUID = 1L;
056
057        public static final int MAX_LENGTH = 500;
058        public static final int MAX_VAL_LENGTH = 2000;
059
060        @ManyToOne(fetch = FetchType.LAZY)
061        @JoinColumn(
062                        name = "CONCEPT_PID",
063                        referencedColumnName = "PID",
064                        foreignKey = @ForeignKey(name = "FK_CONCEPTDESIG_CONCEPT"))
065        private TermConcept myConcept;
066
067        @Id()
068        @SequenceGenerator(name = "SEQ_CONCEPT_DESIG_PID", sequenceName = "SEQ_CONCEPT_DESIG_PID")
069        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_DESIG_PID")
070        @Column(name = "PID")
071        private Long myId;
072
073        @Column(name = "LANG", nullable = true, length = MAX_LENGTH)
074        private String myLanguage;
075
076        @Column(name = "USE_SYSTEM", nullable = true, length = MAX_LENGTH)
077        private String myUseSystem;
078
079        @Column(name = "USE_CODE", nullable = true, length = MAX_LENGTH)
080        private String myUseCode;
081
082        @Column(name = "USE_DISPLAY", nullable = true, length = MAX_LENGTH)
083        private String myUseDisplay;
084
085        @Column(name = "VAL", nullable = false, length = MAX_VAL_LENGTH)
086        private String myValue;
087        /**
088         * TODO: Make this non-null
089         *
090         * @since 3.5.0
091         */
092        @ManyToOne(fetch = FetchType.LAZY)
093        @JoinColumn(
094                        name = "CS_VER_PID",
095                        nullable = true,
096                        referencedColumnName = "PID",
097                        foreignKey = @ForeignKey(name = "FK_CONCEPTDESIG_CSV"))
098        private TermCodeSystemVersion myCodeSystemVersion;
099
100        public String getLanguage() {
101                return myLanguage;
102        }
103
104        public TermConceptDesignation setLanguage(String theLanguage) {
105                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
106                                theLanguage,
107                                MAX_LENGTH,
108                                "Language exceeds maximum length (" + MAX_LENGTH + "): " + length(theLanguage));
109                myLanguage = theLanguage;
110                return this;
111        }
112
113        public String getUseCode() {
114                return myUseCode;
115        }
116
117        public TermConceptDesignation setUseCode(String theUseCode) {
118                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
119                                theUseCode, MAX_LENGTH, "Use code exceeds maximum length (" + MAX_LENGTH + "): " + length(theUseCode));
120                myUseCode = theUseCode;
121                return this;
122        }
123
124        public String getUseDisplay() {
125                return myUseDisplay;
126        }
127
128        public TermConceptDesignation setUseDisplay(String theUseDisplay) {
129                myUseDisplay = left(theUseDisplay, MAX_LENGTH);
130                return this;
131        }
132
133        public String getUseSystem() {
134                return myUseSystem;
135        }
136
137        public TermConceptDesignation setUseSystem(String theUseSystem) {
138                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
139                                theUseSystem,
140                                MAX_LENGTH,
141                                "Use system exceeds maximum length (" + MAX_LENGTH + "): " + length(theUseSystem));
142                myUseSystem = theUseSystem;
143                return this;
144        }
145
146        public String getValue() {
147                return myValue;
148        }
149
150        public TermConceptDesignation setValue(@Nonnull String theValue) {
151                ValidateUtil.isNotBlankOrThrowIllegalArgument(theValue, "theValue must not be null or empty");
152                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
153                                theValue, MAX_VAL_LENGTH, "Value exceeds maximum length (" + MAX_VAL_LENGTH + "): " + length(theValue));
154                myValue = theValue;
155                return this;
156        }
157
158        public TermConceptDesignation setCodeSystemVersion(TermCodeSystemVersion theCodeSystemVersion) {
159                myCodeSystemVersion = theCodeSystemVersion;
160                return this;
161        }
162
163        public TermConceptDesignation setConcept(TermConcept theConcept) {
164                myConcept = theConcept;
165                return this;
166        }
167
168        public Long getPid() {
169                return myId;
170        }
171
172        @Override
173        public String toString() {
174                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
175                                .append("conceptPid", myConcept.getId())
176                                .append("pid", myId)
177                                .append("language", myLanguage)
178                                .append("useSystem", myUseSystem)
179                                .append("useCode", myUseCode)
180                                .append("useDisplay", myUseDisplay)
181                                .append("value", myValue)
182                                .toString();
183        }
184}