001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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 org.apache.commons.lang3.builder.EqualsBuilder;
025import org.apache.commons.lang3.builder.ToStringBuilder;
026import org.apache.commons.lang3.builder.ToStringStyle;
027import org.hibernate.annotations.ColumnDefault;
028
029import javax.annotation.Nonnull;
030import javax.persistence.Column;
031import javax.persistence.Entity;
032import javax.persistence.EnumType;
033import javax.persistence.Enumerated;
034import javax.persistence.FetchType;
035import javax.persistence.ForeignKey;
036import javax.persistence.GeneratedValue;
037import javax.persistence.GenerationType;
038import javax.persistence.Id;
039import javax.persistence.JoinColumn;
040import javax.persistence.OneToMany;
041import javax.persistence.OneToOne;
042import javax.persistence.SequenceGenerator;
043import javax.persistence.Table;
044import javax.persistence.Temporal;
045import javax.persistence.TemporalType;
046import javax.persistence.Transient;
047import javax.persistence.UniqueConstraint;
048import java.io.Serializable;
049import java.util.ArrayList;
050import java.util.Date;
051import java.util.List;
052
053import static org.apache.commons.lang3.StringUtils.left;
054import static org.apache.commons.lang3.StringUtils.length;
055
056@Table(name = "TRM_VALUESET", uniqueConstraints = {
057        @UniqueConstraint(name = "IDX_VALUESET_URL", columnNames = {"URL", "VER"})
058})
059@Entity()
060public class TermValueSet implements Serializable {
061        public static final int MAX_EXPANSION_STATUS_LENGTH = 50;
062        public static final int MAX_NAME_LENGTH = 200;
063        public static final int MAX_URL_LENGTH = 200;
064        public static final int MAX_VER_LENGTH = 200;
065        private static final long serialVersionUID = 1L;
066        @Id()
067        @SequenceGenerator(name = "SEQ_VALUESET_PID", sequenceName = "SEQ_VALUESET_PID")
068        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_VALUESET_PID")
069        @Column(name = "PID")
070        private Long myId;
071
072        @Column(name = "URL", nullable = false, length = MAX_URL_LENGTH)
073        private String myUrl;
074
075        @Column(name = "VER", nullable = true, length = MAX_VER_LENGTH)
076        private String myVersion;
077
078        @OneToOne()
079        @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "FK_TRMVALUESET_RES"))
080        private ResourceTable myResource;
081
082        @Column(name = "RES_ID", insertable = false, updatable = false)
083        private Long myResourcePid;
084
085        @Column(name = "VSNAME", nullable = true, length = MAX_NAME_LENGTH)
086        private String myName;
087
088        @OneToMany(mappedBy = "myValueSet", fetch = FetchType.LAZY)
089        private List<TermValueSetConcept> myConcepts;
090
091        @Column(name = "TOTAL_CONCEPTS", nullable = false)
092        @ColumnDefault("0")
093        private Long myTotalConcepts;
094
095        @Column(name = "TOTAL_CONCEPT_DESIGNATIONS", nullable = false)
096        @ColumnDefault("0")
097        private Long myTotalConceptDesignations;
098
099        @Enumerated(EnumType.STRING)
100        @Column(name = "EXPANSION_STATUS", nullable = false, length = MAX_EXPANSION_STATUS_LENGTH)
101        private TermValueSetPreExpansionStatusEnum myExpansionStatus;
102
103        @Temporal(TemporalType.TIMESTAMP)
104        @Column(name = "EXPANDED_AT", nullable = true)
105        private Date myExpansionTimestamp;
106
107        @Transient
108        private transient Integer myHashCode;
109
110        public TermValueSet() {
111                super();
112                myExpansionStatus = TermValueSetPreExpansionStatusEnum.NOT_EXPANDED;
113                myTotalConcepts = 0L;
114                myTotalConceptDesignations = 0L;
115        }
116
117        public Date getExpansionTimestamp() {
118                return myExpansionTimestamp;
119        }
120
121        public void setExpansionTimestamp(Date theExpansionTimestamp) {
122                myExpansionTimestamp = theExpansionTimestamp;
123        }
124
125        public Long getId() {
126                return myId;
127        }
128
129        public String getUrl() {
130                return myUrl;
131        }
132
133        public TermValueSet setUrl(@Nonnull String theUrl) {
134                ValidateUtil.isNotBlankOrThrowIllegalArgument(theUrl, "theUrl must not be null or empty");
135                ValidateUtil.isNotTooLongOrThrowIllegalArgument(theUrl, MAX_URL_LENGTH,
136                        "URL exceeds maximum length (" + MAX_URL_LENGTH + "): " + length(theUrl));
137                myUrl = theUrl;
138                return this;
139        }
140
141        public ResourceTable getResource() {
142                return myResource;
143        }
144
145        public TermValueSet setResource(ResourceTable theResource) {
146                myResource = theResource;
147                return this;
148        }
149
150        public String getName() {
151                return myName;
152        }
153
154        public TermValueSet setName(String theName) {
155                myName = left(theName, MAX_NAME_LENGTH);
156                return this;
157        }
158
159        public List<TermValueSetConcept> getConcepts() {
160                if (myConcepts == null) {
161                        myConcepts = new ArrayList<>();
162                }
163
164                return myConcepts;
165        }
166
167        public Long getTotalConcepts() {
168                return myTotalConcepts;
169        }
170
171        public TermValueSet setTotalConcepts(Long theTotalConcepts) {
172                myTotalConcepts = theTotalConcepts;
173                return this;
174        }
175
176        public TermValueSet decrementTotalConcepts() {
177                if (myTotalConcepts > 0) {
178                        myTotalConcepts--;
179                }
180                return this;
181        }
182
183        public TermValueSet incrementTotalConcepts() {
184                myTotalConcepts++;
185                return this;
186        }
187
188        public Long getTotalConceptDesignations() {
189                return myTotalConceptDesignations;
190        }
191
192        public TermValueSet setTotalConceptDesignations(Long theTotalConceptDesignations) {
193                myTotalConceptDesignations = theTotalConceptDesignations;
194                return this;
195        }
196
197        public TermValueSet decrementTotalConceptDesignations() {
198                if (myTotalConceptDesignations > 0) {
199                        myTotalConceptDesignations--;
200                }
201                return this;
202        }
203
204        public TermValueSet incrementTotalConceptDesignations() {
205                myTotalConceptDesignations++;
206                return this;
207        }
208
209        public TermValueSetPreExpansionStatusEnum getExpansionStatus() {
210                return myExpansionStatus;
211        }
212
213        public void setExpansionStatus(TermValueSetPreExpansionStatusEnum theExpansionStatus) {
214                myExpansionStatus = theExpansionStatus;
215        }
216
217        public String getVersion() {
218                return myVersion;
219        }
220
221        public TermValueSet setVersion(String theVersion) {
222                ValidateUtil.isNotTooLongOrThrowIllegalArgument(theVersion, MAX_VER_LENGTH,
223                        "Version exceeds maximum length (" + MAX_VER_LENGTH + "): " + length(theVersion));
224                myVersion = theVersion;
225                return this;
226        }
227
228        @Override
229        public boolean equals(Object theO) {
230                if (this == theO) return true;
231
232                if (!(theO instanceof TermValueSet)) return false;
233
234                TermValueSet that = (TermValueSet) theO;
235
236                return new EqualsBuilder()
237                        .append(getUrl(), that.getUrl())
238                        .isEquals();
239        }
240
241        @Override
242        public int hashCode() {
243                if (myHashCode == null) {
244                        myHashCode = getUrl().hashCode();
245                }
246                return myHashCode;
247        }
248
249        @Override
250        public String toString() {
251                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
252                        .append("id", myId)
253                        .append("url", myUrl)
254                        .append(myResource != null ? ("resource=" + myResource.toString()) : ("resource=(null)"))
255                        .append("resourcePid", myResourcePid)
256                        .append("name", myName)
257                        .append(myConcepts != null ? ("concepts - size=" + myConcepts.size()) : ("concepts=(null)"))
258                        .append("totalConcepts", myTotalConcepts)
259                        .append("totalConceptDesignations", myTotalConceptDesignations)
260                        .append("expansionStatus", myExpansionStatus)
261                        .toString();
262        }
263}