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.jpa.model.entity.ResourceTable;
023import ca.uhn.fhir.util.ValidateUtil;
024import jakarta.annotation.Nonnull;
025import jakarta.persistence.Column;
026import jakarta.persistence.Entity;
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.OneToMany;
034import jakarta.persistence.OneToOne;
035import jakarta.persistence.SequenceGenerator;
036import jakarta.persistence.Table;
037import jakarta.persistence.UniqueConstraint;
038import org.apache.commons.lang3.builder.ToStringBuilder;
039import org.apache.commons.lang3.builder.ToStringStyle;
040
041import java.io.Serializable;
042import java.util.ArrayList;
043import java.util.List;
044
045import static org.apache.commons.lang3.StringUtils.length;
046
047@Entity
048@Table(
049                name = "TRM_CONCEPT_MAP",
050                uniqueConstraints = {
051                        @UniqueConstraint(
052                                        name = "IDX_CONCEPT_MAP_URL",
053                                        columnNames = {"URL", "VER"})
054                },
055                indexes = {
056                        // must have same name that indexed FK or SchemaMigrationTest complains because H2 sets this index
057                        // automatically
058                        @Index(name = "FK_TRMCONCEPTMAP_RES", columnList = "RES_ID")
059                })
060public class TermConceptMap implements Serializable {
061        private static final long serialVersionUID = 1L;
062
063        static final int MAX_URL_LENGTH = 200;
064        public static final int MAX_VER_LENGTH = 200;
065
066        /**
067         * Constructor
068         */
069        public TermConceptMap() {
070                super();
071        }
072
073        @Id()
074        @SequenceGenerator(name = "SEQ_CONCEPT_MAP_PID", sequenceName = "SEQ_CONCEPT_MAP_PID")
075        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_MAP_PID")
076        @Column(name = "PID")
077        private Long myId;
078
079        @OneToOne()
080        @JoinColumn(
081                        name = "RES_ID",
082                        referencedColumnName = "RES_ID",
083                        nullable = false,
084                        updatable = false,
085                        foreignKey = @ForeignKey(name = "FK_TRMCONCEPTMAP_RES"))
086        private ResourceTable myResource;
087
088        @Column(name = "RES_ID", insertable = false, updatable = false)
089        private Long myResourcePid;
090
091        @Column(name = "SOURCE_URL", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
092        private String mySource;
093
094        @Column(name = "TARGET_URL", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
095        private String myTarget;
096
097        @Column(name = "URL", nullable = false, length = MAX_URL_LENGTH)
098        private String myUrl;
099
100        @Column(name = "VER", nullable = true, length = MAX_VER_LENGTH)
101        private String myVersion;
102
103        @OneToMany(mappedBy = "myConceptMap")
104        private List<TermConceptMapGroup> myConceptMapGroups;
105
106        public List<TermConceptMapGroup> getConceptMapGroups() {
107                if (myConceptMapGroups == null) {
108                        myConceptMapGroups = new ArrayList<>();
109                }
110
111                return myConceptMapGroups;
112        }
113
114        public Long getId() {
115                return myId;
116        }
117
118        public ResourceTable getResource() {
119                return myResource;
120        }
121
122        public TermConceptMap setResource(ResourceTable theResource) {
123                myResource = theResource;
124                return this;
125        }
126
127        public Long getResourcePid() {
128                return myResourcePid;
129        }
130
131        public TermConceptMap setResourcePid(Long theResourcePid) {
132                myResourcePid = theResourcePid;
133                return this;
134        }
135
136        public String getSource() {
137                return mySource;
138        }
139
140        public TermConceptMap setSource(String theSource) {
141                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
142                                theSource,
143                                TermValueSet.MAX_URL_LENGTH,
144                                "Source exceeds maximum length (" + TermValueSet.MAX_URL_LENGTH + "): " + length(theSource));
145                mySource = theSource;
146                return this;
147        }
148
149        public String getTarget() {
150                return myTarget;
151        }
152
153        public TermConceptMap setTarget(String theTarget) {
154                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
155                                theTarget,
156                                TermValueSet.MAX_URL_LENGTH,
157                                "Target exceeds maximum length (" + TermValueSet.MAX_URL_LENGTH + "): " + length(theTarget));
158                myTarget = theTarget;
159                return this;
160        }
161
162        public String getUrl() {
163                return myUrl;
164        }
165
166        public TermConceptMap setUrl(@Nonnull String theUrl) {
167                ValidateUtil.isNotBlankOrThrowIllegalArgument(theUrl, "theUrl must not be null or empty");
168                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
169                                theUrl, MAX_URL_LENGTH, "URL exceeds maximum length (" + MAX_URL_LENGTH + "): " + length(theUrl));
170                myUrl = theUrl;
171                return this;
172        }
173
174        public String getVersion() {
175                return myVersion;
176        }
177
178        public TermConceptMap setVersion(String theVersion) {
179                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
180                                theVersion,
181                                MAX_VER_LENGTH,
182                                "Version exceeds maximum length (" + MAX_VER_LENGTH + "): " + length(theVersion));
183                myVersion = theVersion;
184                return this;
185        }
186
187        @Override
188        public String toString() {
189                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
190                                .append("myId", myId)
191                                .append(myResource != null ? ("myResource=" + myResource.toString()) : ("myResource=(null)"))
192                                .append("myResourcePid", myResourcePid)
193                                .append("mySource", mySource)
194                                .append("myTarget", myTarget)
195                                .append("myUrl", myUrl)
196                                .append("myVersion", myVersion)
197                                .append(
198                                                myConceptMapGroups != null
199                                                                ? ("myConceptMapGroups - size=" + myConceptMapGroups.size())
200                                                                : ("myConceptMapGroups=(null)"))
201                                .toString();
202        }
203}