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