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