001/*
002 * #%L
003 * HAPI FHIR JPA Model
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.model.entity;
021
022import jakarta.persistence.Column;
023import jakarta.persistence.Entity;
024import jakarta.persistence.EnumType;
025import jakarta.persistence.Enumerated;
026import jakarta.persistence.FetchType;
027import jakarta.persistence.GeneratedValue;
028import jakarta.persistence.GenerationType;
029import jakarta.persistence.Id;
030import jakarta.persistence.Index;
031import jakarta.persistence.OneToMany;
032import jakarta.persistence.SequenceGenerator;
033import jakarta.persistence.Table;
034import jakarta.persistence.Transient;
035import org.apache.commons.lang3.StringUtils;
036import org.apache.commons.lang3.builder.EqualsBuilder;
037import org.apache.commons.lang3.builder.HashCodeBuilder;
038import org.apache.commons.lang3.builder.ToStringBuilder;
039import org.apache.commons.lang3.builder.ToStringStyle;
040import org.hibernate.annotations.JdbcTypeCode;
041import org.hibernate.type.SqlTypes;
042
043import java.io.Serializable;
044import java.util.Collection;
045
046@Entity
047@Table(
048                name = "HFJ_TAG_DEF",
049                indexes = {
050                        @Index(
051                                        name = "IDX_TAG_DEF_TP_CD_SYS",
052                                        columnList = "TAG_TYPE, TAG_CODE, TAG_SYSTEM, TAG_ID, TAG_VERSION, TAG_USER_SELECTED"),
053                })
054public class TagDefinition implements Serializable {
055
056        private static final long serialVersionUID = 1L;
057
058        @Column(name = "TAG_CODE", length = 200)
059        private String myCode;
060
061        @Column(name = "TAG_DISPLAY", length = 200)
062        private String myDisplay;
063
064        @Id
065        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_TAGDEF_ID")
066        @SequenceGenerator(name = "SEQ_TAGDEF_ID", sequenceName = "SEQ_TAGDEF_ID")
067        @Column(name = "TAG_ID")
068        private Long myId;
069
070        @OneToMany(
071                        cascade = {},
072                        fetch = FetchType.LAZY,
073                        mappedBy = "myTag")
074        private Collection<ResourceTag> myResources;
075
076        @OneToMany(
077                        cascade = {},
078                        fetch = FetchType.LAZY,
079                        mappedBy = "myTag")
080        private Collection<ResourceHistoryTag> myResourceVersions;
081
082        @Column(name = "TAG_SYSTEM", length = 200)
083        private String mySystem;
084
085        @Column(name = "TAG_TYPE", nullable = false)
086        @Enumerated(EnumType.ORDINAL)
087        @JdbcTypeCode(SqlTypes.INTEGER)
088        private TagTypeEnum myTagType;
089
090        @Column(name = "TAG_VERSION", length = 30)
091        private String myVersion;
092
093        @Column(name = "TAG_USER_SELECTED")
094        private Boolean myUserSelected;
095
096        @Transient
097        private transient Integer myHashCode;
098
099        /**
100         * Constructor
101         */
102        public TagDefinition() {
103                super();
104        }
105
106        public TagDefinition(TagTypeEnum theTagType, String theSystem, String theCode, String theDisplay) {
107                setTagType(theTagType);
108                setCode(theCode);
109                setSystem(theSystem);
110                setDisplay(theDisplay);
111        }
112
113        public String getCode() {
114                return myCode;
115        }
116
117        public void setCode(String theCode) {
118                myCode = theCode;
119                myHashCode = null;
120        }
121
122        public String getDisplay() {
123                return myDisplay;
124        }
125
126        public void setDisplay(String theDisplay) {
127                myDisplay = theDisplay;
128        }
129
130        public Long getId() {
131                return myId;
132        }
133
134        public void setId(Long theId) {
135                myId = theId;
136        }
137
138        public String getSystem() {
139                return mySystem;
140        }
141
142        public void setSystem(String theSystem) {
143                mySystem = theSystem;
144                myHashCode = null;
145        }
146
147        public TagTypeEnum getTagType() {
148                return myTagType;
149        }
150
151        public void setTagType(TagTypeEnum theTagType) {
152                myTagType = theTagType;
153                myHashCode = null;
154        }
155
156        public String getVersion() {
157                return myVersion;
158        }
159
160        public void setVersion(String theVersion) {
161                setVersionAfterTrim(theVersion);
162        }
163
164        private void setVersionAfterTrim(String theVersion) {
165                if (theVersion != null) {
166                        myVersion = StringUtils.truncate(theVersion, 30);
167                }
168        }
169
170        /**
171         * Warning - this is nullable, while IBaseCoding getUserSelected isn't.
172         * todo maybe rename?
173         */
174        public Boolean getUserSelected() {
175                return myUserSelected;
176        }
177
178        public void setUserSelected(Boolean theUserSelected) {
179                myUserSelected = theUserSelected;
180        }
181
182        @Override
183        public boolean equals(Object obj) {
184                if (this == obj) {
185                        return true;
186                }
187                if (!(obj instanceof TagDefinition)) {
188                        return false;
189                }
190                TagDefinition other = (TagDefinition) obj;
191
192                EqualsBuilder b = new EqualsBuilder();
193
194                if (myId != null && other.myId != null) {
195                        b.append(myId, other.myId);
196                } else {
197                        b.append(myTagType, other.myTagType);
198                        b.append(mySystem, other.mySystem);
199                        b.append(myCode, other.myCode);
200                        b.append(myVersion, other.myVersion);
201                        b.append(myUserSelected, other.myUserSelected);
202                }
203
204                return b.isEquals();
205        }
206
207        @Override
208        public int hashCode() {
209                if (myHashCode == null) {
210                        HashCodeBuilder b = new HashCodeBuilder();
211                        b.append(myTagType);
212                        b.append(mySystem);
213                        b.append(myCode);
214                        b.append(myVersion);
215                        b.append(myUserSelected);
216                        myHashCode = b.toHashCode();
217                }
218                return myHashCode;
219        }
220
221        @Override
222        public String toString() {
223                ToStringBuilder retVal = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
224                retVal.append("id", myId);
225                retVal.append("system", mySystem);
226                retVal.append("code", myCode);
227                retVal.append("display", myDisplay);
228                retVal.append("version", myVersion);
229                retVal.append("userSelected", myUserSelected);
230                return retVal.build();
231        }
232}