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.ForeignKey;
027import jakarta.persistence.GeneratedValue;
028import jakarta.persistence.GenerationType;
029import jakarta.persistence.Id;
030import jakarta.persistence.Index;
031import jakarta.persistence.JoinColumn;
032import jakarta.persistence.ManyToOne;
033import jakarta.persistence.OneToMany;
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;
040import java.util.ArrayList;
041import java.util.List;
042
043import static org.apache.commons.lang3.StringUtils.length;
044
045@Entity
046@Table(
047                name = "TRM_CONCEPT_MAP_GROUP",
048                indexes = {@Index(name = "FK_TCMGROUP_CONCEPTMAP", columnList = "CONCEPT_MAP_PID")})
049public class TermConceptMapGroup implements Serializable {
050        private static final long serialVersionUID = 1L;
051
052        @Id()
053        @SequenceGenerator(name = "SEQ_CONCEPT_MAP_GROUP_PID", sequenceName = "SEQ_CONCEPT_MAP_GROUP_PID")
054        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_MAP_GROUP_PID")
055        @Column(name = "PID")
056        private Long myId;
057
058        @ManyToOne()
059        @JoinColumn(
060                        name = "CONCEPT_MAP_PID",
061                        nullable = false,
062                        referencedColumnName = "PID",
063                        foreignKey = @ForeignKey(name = "FK_TCMGROUP_CONCEPTMAP"))
064        private TermConceptMap myConceptMap;
065
066        @Column(name = "SOURCE_URL", nullable = false, length = TermCodeSystem.MAX_URL_LENGTH)
067        private String mySource;
068
069        @Column(name = "SOURCE_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
070        private String mySourceVersion;
071
072        @Column(name = "TARGET_URL", nullable = false, length = TermCodeSystem.MAX_URL_LENGTH)
073        private String myTarget;
074
075        @Column(name = "TARGET_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
076        private String myTargetVersion;
077
078        @OneToMany(mappedBy = "myConceptMapGroup")
079        private List<TermConceptMapGroupElement> myConceptMapGroupElements;
080
081        @Column(name = "CONCEPT_MAP_URL", nullable = true, length = TermConceptMap.MAX_URL_LENGTH)
082        private String myConceptMapUrl;
083
084        @Column(name = "SOURCE_VS", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
085        private String mySourceValueSet;
086
087        @Column(name = "TARGET_VS", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
088        private String myTargetValueSet;
089
090        public TermConceptMap getConceptMap() {
091                return myConceptMap;
092        }
093
094        public TermConceptMapGroup setConceptMap(TermConceptMap theTermConceptMap) {
095                myConceptMap = theTermConceptMap;
096                return this;
097        }
098
099        public List<TermConceptMapGroupElement> getConceptMapGroupElements() {
100                if (myConceptMapGroupElements == null) {
101                        myConceptMapGroupElements = new ArrayList<>();
102                }
103
104                return myConceptMapGroupElements;
105        }
106
107        public String getConceptMapUrl() {
108                if (myConceptMapUrl == null) {
109                        myConceptMapUrl = getConceptMap().getUrl();
110                }
111                return myConceptMapUrl;
112        }
113
114        public Long getId() {
115                return myId;
116        }
117
118        public String getSource() {
119                return mySource;
120        }
121
122        public TermConceptMapGroup setSource(@Nonnull String theSource) {
123                ValidateUtil.isNotBlankOrThrowIllegalArgument(theSource, "theSource must not be null or empty");
124                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
125                                theSource,
126                                TermCodeSystem.MAX_URL_LENGTH,
127                                "Source exceeds maximum length (" + TermCodeSystem.MAX_URL_LENGTH + "): " + length(theSource));
128                this.mySource = theSource;
129                return this;
130        }
131
132        public String getSourceValueSet() {
133                if (mySourceValueSet == null) {
134                        mySourceValueSet = getConceptMap().getSource();
135                }
136                return mySourceValueSet;
137        }
138
139        public String getSourceVersion() {
140                return mySourceVersion;
141        }
142
143        public TermConceptMapGroup setSourceVersion(String theSourceVersion) {
144                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
145                                theSourceVersion,
146                                TermCodeSystemVersion.MAX_VERSION_LENGTH,
147                                "Source version ID exceeds maximum length (" + TermCodeSystemVersion.MAX_VERSION_LENGTH + "): "
148                                                + length(theSourceVersion));
149                mySourceVersion = theSourceVersion;
150                return this;
151        }
152
153        public String getTarget() {
154                return myTarget;
155        }
156
157        public TermConceptMapGroup setTarget(@Nonnull String theTarget) {
158                ValidateUtil.isNotBlankOrThrowIllegalArgument(theTarget, "theTarget must not be null or empty");
159                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
160                                theTarget,
161                                TermCodeSystem.MAX_URL_LENGTH,
162                                "Target exceeds maximum length (" + TermCodeSystem.MAX_URL_LENGTH + "): " + length(theTarget));
163                this.myTarget = theTarget;
164                return this;
165        }
166
167        public String getTargetValueSet() {
168                if (myTargetValueSet == null) {
169                        myTargetValueSet = getConceptMap().getTarget();
170                }
171                return myTargetValueSet;
172        }
173
174        public String getTargetVersion() {
175                return myTargetVersion;
176        }
177
178        public TermConceptMapGroup setTargetVersion(String theTargetVersion) {
179                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
180                                theTargetVersion,
181                                TermCodeSystemVersion.MAX_VERSION_LENGTH,
182                                "Target version ID exceeds maximum length (" + TermCodeSystemVersion.MAX_VERSION_LENGTH + "): "
183                                                + length(theTargetVersion));
184                myTargetVersion = theTargetVersion;
185                return this;
186        }
187
188        @Override
189        public String toString() {
190                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
191                                .append("myId", myId)
192                                .append(myConceptMap != null ? ("myConceptMap - id=" + myConceptMap.getId()) : ("myConceptMap=(null)"))
193                                .append("mySource", mySource)
194                                .append("mySourceVersion", mySourceVersion)
195                                .append("myTarget", myTarget)
196                                .append("myTargetVersion", myTargetVersion)
197                                .append(
198                                                myConceptMapGroupElements != null
199                                                                ? ("myConceptMapGroupElements - size=" + myConceptMapGroupElements.size())
200                                                                : ("myConceptMapGroupElements=(null)"))
201                                .append("myConceptMapUrl", this.getConceptMapUrl())
202                                .append("mySourceValueSet", this.getSourceValueSet())
203                                .append("myTargetValueSet", this.getTargetValueSet())
204                                .toString();
205        }
206}