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