001package ca.uhn.fhir.jpa.entity;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.model.entity.ResourceTable;
024import ca.uhn.fhir.util.ValidateUtil;
025import org.apache.commons.lang3.builder.EqualsBuilder;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029
030import javax.annotation.Nonnull;
031import javax.persistence.*;
032import java.io.Serializable;
033
034import static org.apache.commons.lang3.StringUtils.left;
035import static org.apache.commons.lang3.StringUtils.length;
036
037//@formatter:off
038@Table(name = "TRM_CODESYSTEM", uniqueConstraints = {
039        @UniqueConstraint(name = "IDX_CS_CODESYSTEM", columnNames = {"CODE_SYSTEM_URI"})
040})
041@Entity()
042//@formatter:on
043public class TermCodeSystem implements Serializable {
044        public static final int MAX_URL_LENGTH = 200;
045        private static final long serialVersionUID = 1L;
046        private static final int MAX_NAME_LENGTH = 200;
047        @Column(name = "CODE_SYSTEM_URI", nullable = false, length = MAX_URL_LENGTH)
048        private String myCodeSystemUri;
049
050        @OneToOne(fetch = FetchType.LAZY)
051        @JoinColumn(name = "CURRENT_VERSION_PID", referencedColumnName = "PID", nullable = true, foreignKey = @ForeignKey(name = "FK_TRMCODESYSTEM_CURVER"))
052        private TermCodeSystemVersion myCurrentVersion;
053        @Column(name = "CURRENT_VERSION_PID", nullable = true, insertable = false, updatable = false)
054        private Long myCurrentVersionPid;
055        @Id()
056        @SequenceGenerator(name = "SEQ_CODESYSTEM_PID", sequenceName = "SEQ_CODESYSTEM_PID")
057        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CODESYSTEM_PID")
058        @Column(name = "PID")
059        private Long myPid;
060        @OneToOne(fetch = FetchType.LAZY)
061        @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, updatable = true, foreignKey = @ForeignKey(name = "FK_TRMCODESYSTEM_RES"))
062        private ResourceTable myResource;
063        @Column(name = "RES_ID", insertable = false, updatable = false)
064        private Long myResourcePid;
065        @Column(name = "CS_NAME", nullable = true, length = MAX_NAME_LENGTH)
066        private String myName;
067
068        /**
069         * Constructor
070         */
071        public TermCodeSystem() {
072                super();
073        }
074
075        @Override
076        public boolean equals(Object theO) {
077                if (this == theO) {
078                        return true;
079                }
080
081                if (theO == null || getClass() != theO.getClass()) {
082                        return false;
083                }
084
085                TermCodeSystem that = (TermCodeSystem) theO;
086
087                EqualsBuilder b = new EqualsBuilder();
088                b.append(myCodeSystemUri, that.myCodeSystemUri);
089                return b.isEquals();
090        }
091
092        @Override
093        public int hashCode() {
094                HashCodeBuilder b = new HashCodeBuilder(17, 37);
095                b.append(myCodeSystemUri);
096                return b.toHashCode();
097        }
098
099        public String getCodeSystemUri() {
100                return myCodeSystemUri;
101        }
102
103        public TermCodeSystem setCodeSystemUri(@Nonnull String theCodeSystemUri) {
104                ValidateUtil.isNotBlankOrThrowIllegalArgument(theCodeSystemUri, "theCodeSystemUri must not be null or empty");
105                ValidateUtil.isNotTooLongOrThrowIllegalArgument(theCodeSystemUri, MAX_URL_LENGTH,
106                        "URI exceeds maximum length (" + MAX_URL_LENGTH + "): " + length(theCodeSystemUri));
107                myCodeSystemUri = theCodeSystemUri;
108                return this;
109        }
110
111        public String getName() {
112                return myName;
113        }
114
115        public TermCodeSystem setName(String theName) {
116                myName = left(theName, MAX_NAME_LENGTH);
117                return this;
118        }
119
120        public TermCodeSystemVersion getCurrentVersion() {
121                return myCurrentVersion;
122        }
123
124        public TermCodeSystem setCurrentVersion(TermCodeSystemVersion theCurrentVersion) {
125                myCurrentVersion = theCurrentVersion;
126                return this;
127        }
128
129        public Long getPid() {
130                return myPid;
131        }
132
133        public ResourceTable getResource() {
134                return myResource;
135        }
136
137        public TermCodeSystem setResource(ResourceTable theResource) {
138                myResource = theResource;
139                return this;
140        }
141
142        @Override
143        public String toString() {
144                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
145                b.append("pid", myPid);
146                b.append("codeSystemUri", myCodeSystemUri);
147                b.append("currentVersionPid", myCurrentVersionPid);
148                b.append("resourcePid", myResourcePid);
149                b.append("name", myName);
150                return b
151                        .toString();
152        }
153}