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.FetchType;
028import jakarta.persistence.ForeignKey;
029import jakarta.persistence.GeneratedValue;
030import jakarta.persistence.GenerationType;
031import jakarta.persistence.Id;
032import jakarta.persistence.Index;
033import jakarta.persistence.JoinColumn;
034import jakarta.persistence.OneToOne;
035import jakarta.persistence.SequenceGenerator;
036import jakarta.persistence.Table;
037import jakarta.persistence.UniqueConstraint;
038import org.apache.commons.lang3.builder.EqualsBuilder;
039import org.apache.commons.lang3.builder.HashCodeBuilder;
040import org.apache.commons.lang3.builder.ToStringBuilder;
041import org.apache.commons.lang3.builder.ToStringStyle;
042
043import java.io.Serializable;
044
045import static org.apache.commons.lang3.StringUtils.left;
046import static org.apache.commons.lang3.StringUtils.length;
047
048// @formatter:off
049@Table(
050                name = "TRM_CODESYSTEM",
051                uniqueConstraints = {
052                        @UniqueConstraint(
053                                        name = "IDX_CS_CODESYSTEM",
054                                        columnNames = {"CODE_SYSTEM_URI"})
055                },
056                indexes = {
057                        @Index(name = "FK_TRMCODESYSTEM_RES", columnList = "RES_ID"),
058                        @Index(name = "FK_TRMCODESYSTEM_CURVER", columnList = "CURRENT_VERSION_PID")
059                })
060@Entity()
061// @formatter:on
062public class TermCodeSystem implements Serializable {
063        public static final int MAX_URL_LENGTH = 200;
064        private static final long serialVersionUID = 1L;
065        private static final int MAX_NAME_LENGTH = 200;
066
067        @Column(name = "CODE_SYSTEM_URI", nullable = false, length = MAX_URL_LENGTH)
068        private String myCodeSystemUri;
069
070        @OneToOne(fetch = FetchType.LAZY)
071        @JoinColumn(
072                        name = "CURRENT_VERSION_PID",
073                        referencedColumnName = "PID",
074                        nullable = true,
075                        foreignKey = @ForeignKey(name = "FK_TRMCODESYSTEM_CURVER"))
076        private TermCodeSystemVersion myCurrentVersion;
077
078        @Column(name = "CURRENT_VERSION_PID", nullable = true, insertable = false, updatable = false)
079        private Long myCurrentVersionPid;
080
081        @Id()
082        @SequenceGenerator(name = "SEQ_CODESYSTEM_PID", sequenceName = "SEQ_CODESYSTEM_PID")
083        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CODESYSTEM_PID")
084        @Column(name = "PID")
085        private Long myPid;
086
087        @OneToOne(fetch = FetchType.LAZY)
088        @JoinColumn(
089                        name = "RES_ID",
090                        referencedColumnName = "RES_ID",
091                        nullable = false,
092                        updatable = true,
093                        foreignKey = @ForeignKey(name = "FK_TRMCODESYSTEM_RES"))
094        private ResourceTable myResource;
095
096        @Column(name = "RES_ID", insertable = false, updatable = false)
097        private Long myResourcePid;
098
099        @Column(name = "CS_NAME", nullable = true, length = MAX_NAME_LENGTH)
100        private String myName;
101
102        /**
103         * Constructor
104         */
105        public TermCodeSystem() {
106                super();
107        }
108
109        @Override
110        public boolean equals(Object theO) {
111                if (this == theO) {
112                        return true;
113                }
114
115                if (theO == null || getClass() != theO.getClass()) {
116                        return false;
117                }
118
119                TermCodeSystem that = (TermCodeSystem) theO;
120
121                EqualsBuilder b = new EqualsBuilder();
122                b.append(myCodeSystemUri, that.myCodeSystemUri);
123                return b.isEquals();
124        }
125
126        @Override
127        public int hashCode() {
128                HashCodeBuilder b = new HashCodeBuilder(17, 37);
129                b.append(myCodeSystemUri);
130                return b.toHashCode();
131        }
132
133        public String getCodeSystemUri() {
134                return myCodeSystemUri;
135        }
136
137        public TermCodeSystem setCodeSystemUri(@Nonnull String theCodeSystemUri) {
138                ValidateUtil.isNotBlankOrThrowIllegalArgument(theCodeSystemUri, "theCodeSystemUri must not be null or empty");
139                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
140                                theCodeSystemUri,
141                                MAX_URL_LENGTH,
142                                "URI exceeds maximum length (" + MAX_URL_LENGTH + "): " + length(theCodeSystemUri));
143                myCodeSystemUri = theCodeSystemUri;
144                return this;
145        }
146
147        public String getName() {
148                return myName;
149        }
150
151        public TermCodeSystem setName(String theName) {
152                myName = left(theName, MAX_NAME_LENGTH);
153                return this;
154        }
155
156        public TermCodeSystemVersion getCurrentVersion() {
157                return myCurrentVersion;
158        }
159
160        public TermCodeSystem setCurrentVersion(TermCodeSystemVersion theCurrentVersion) {
161                myCurrentVersion = theCurrentVersion;
162                return this;
163        }
164
165        public Long getPid() {
166                return myPid;
167        }
168
169        public ResourceTable getResource() {
170                return myResource;
171        }
172
173        public TermCodeSystem setResource(ResourceTable theResource) {
174                myResource = theResource;
175                return this;
176        }
177
178        @Override
179        public String toString() {
180                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
181                b.append("pid", myPid);
182                b.append("codeSystemUri", myCodeSystemUri);
183                b.append("currentVersionPid", myCurrentVersionPid);
184                b.append("resourcePid", myResourcePid);
185                b.append("name", myName);
186                return b.toString();
187        }
188}