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.util.ValidateUtil;
023import jakarta.annotation.Nonnull;
024import jakarta.persistence.Column;
025import jakarta.persistence.Entity;
026import jakarta.persistence.ForeignKey;
027import jakarta.persistence.GeneratedValue;
028import jakarta.persistence.GenerationType;
029import jakarta.persistence.Id;
030import jakarta.persistence.Index;
031import jakarta.persistence.JoinColumn;
032import jakarta.persistence.ManyToOne;
033import jakarta.persistence.OneToMany;
034import jakarta.persistence.SequenceGenerator;
035import jakarta.persistence.Table;
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;
040
041import java.io.Serializable;
042import java.util.ArrayList;
043import java.util.List;
044
045import static org.apache.commons.lang3.StringUtils.left;
046import static org.apache.commons.lang3.StringUtils.length;
047
048@Entity
049@Table(
050                name = "TRM_CONCEPT_MAP_GRP_ELEMENT",
051                indexes = {
052                        @Index(name = "IDX_CNCPT_MAP_GRP_CD", columnList = "SOURCE_CODE"),
053                        @Index(name = "FK_TCMGELEMENT_GROUP", columnList = "CONCEPT_MAP_GROUP_PID")
054                })
055public class TermConceptMapGroupElement implements Serializable {
056        private static final long serialVersionUID = 1L;
057
058        @Id()
059        @SequenceGenerator(name = "SEQ_CONCEPT_MAP_GRP_ELM_PID", sequenceName = "SEQ_CONCEPT_MAP_GRP_ELM_PID")
060        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_MAP_GRP_ELM_PID")
061        @Column(name = "PID")
062        private Long myId;
063
064        @ManyToOne()
065        @JoinColumn(
066                        name = "CONCEPT_MAP_GROUP_PID",
067                        nullable = false,
068                        referencedColumnName = "PID",
069                        foreignKey = @ForeignKey(name = "FK_TCMGELEMENT_GROUP"))
070        private TermConceptMapGroup myConceptMapGroup;
071
072        @Column(name = "SOURCE_CODE", nullable = false, length = TermConcept.MAX_CODE_LENGTH)
073        private String myCode;
074
075        @Column(name = "SOURCE_DISPLAY", length = TermConcept.MAX_DISP_LENGTH)
076        private String myDisplay;
077
078        @OneToMany(mappedBy = "myConceptMapGroupElement")
079        private List<TermConceptMapGroupElementTarget> myConceptMapGroupElementTargets;
080
081        @Column(name = "CONCEPT_MAP_URL", nullable = true, length = TermConceptMap.MAX_URL_LENGTH)
082        private String myConceptMapUrl;
083
084        @Column(name = "SYSTEM_URL", nullable = true, length = TermCodeSystem.MAX_URL_LENGTH)
085        private String mySystem;
086
087        @Column(name = "SYSTEM_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
088        private String mySystemVersion;
089
090        @Column(name = "VALUESET_URL", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
091        private String myValueSet;
092
093        public String getCode() {
094                return myCode;
095        }
096
097        public TermConceptMapGroupElement setCode(@Nonnull String theCode) {
098                ValidateUtil.isNotBlankOrThrowIllegalArgument(theCode, "theCode must not be null or empty");
099                ValidateUtil.isNotTooLongOrThrowIllegalArgument(
100                                theCode,
101                                TermConcept.MAX_CODE_LENGTH,
102                                "Code exceeds maximum length (" + TermConcept.MAX_CODE_LENGTH + "): " + length(theCode));
103                myCode = theCode;
104                return this;
105        }
106
107        public TermConceptMapGroup getConceptMapGroup() {
108                return myConceptMapGroup;
109        }
110
111        public TermConceptMapGroupElement setConceptMapGroup(TermConceptMapGroup theTermConceptMapGroup) {
112                myConceptMapGroup = theTermConceptMapGroup;
113                return this;
114        }
115
116        public List<TermConceptMapGroupElementTarget> getConceptMapGroupElementTargets() {
117                if (myConceptMapGroupElementTargets == null) {
118                        myConceptMapGroupElementTargets = new ArrayList<>();
119                }
120
121                return myConceptMapGroupElementTargets;
122        }
123
124        public String getConceptMapUrl() {
125                if (myConceptMapUrl == null) {
126                        myConceptMapUrl = getConceptMapGroup().getConceptMap().getUrl();
127                }
128                return myConceptMapUrl;
129        }
130
131        public String getDisplay() {
132                return myDisplay;
133        }
134
135        public TermConceptMapGroupElement setDisplay(String theDisplay) {
136                myDisplay = left(theDisplay, TermConcept.MAX_DISP_LENGTH);
137                return this;
138        }
139
140        public Long getId() {
141                return myId;
142        }
143
144        public String getSystem() {
145                if (mySystem == null) {
146                        mySystem = getConceptMapGroup().getSource();
147                }
148                return mySystem;
149        }
150
151        public String getSystemVersion() {
152                if (mySystemVersion == null) {
153                        mySystemVersion = getConceptMapGroup().getSourceVersion();
154                }
155                return mySystemVersion;
156        }
157
158        public String getValueSet() {
159                if (myValueSet == null) {
160                        myValueSet = getConceptMapGroup().getSourceValueSet();
161                }
162                return myValueSet;
163        }
164
165        @Override
166        public boolean equals(Object o) {
167                if (this == o) return true;
168
169                if (!(o instanceof TermConceptMapGroupElement)) return false;
170
171                TermConceptMapGroupElement that = (TermConceptMapGroupElement) o;
172
173                return new EqualsBuilder()
174                                .append(getCode(), that.getCode())
175                                .append(getSystem(), that.getSystem())
176                                .append(getSystemVersion(), that.getSystemVersion())
177                                .isEquals();
178        }
179
180        @Override
181        public int hashCode() {
182                return new HashCodeBuilder(17, 37)
183                                .append(getCode())
184                                .append(getSystem())
185                                .append(getSystemVersion())
186                                .toHashCode();
187        }
188
189        @Override
190        public String toString() {
191                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
192                                .append("myId", myId)
193                                .append(
194                                                myConceptMapGroup != null
195                                                                ? ("myConceptMapGroup - id=" + myConceptMapGroup.getId())
196                                                                : ("myConceptMapGroup=(null)"))
197                                .append("myCode", myCode)
198                                .append("myDisplay", myDisplay)
199                                .append(
200                                                myConceptMapGroupElementTargets != null
201                                                                ? ("myConceptMapGroupElementTargets - size=" + myConceptMapGroupElementTargets.size())
202                                                                : ("myConceptMapGroupElementTargets=(null)"))
203                                .append("myConceptMapUrl", this.getConceptMapUrl())
204                                .append("mySystem", this.getSystem())
205                                .append("mySystemVersion", this.getSystemVersion())
206                                .append("myValueSet", this.getValueSet())
207                                .toString();
208        }
209}