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