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.i18n.Msg;
023import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
024import jakarta.persistence.Column;
025import jakarta.persistence.Entity;
026import jakarta.persistence.Id;
027import jakarta.persistence.Lob;
028import org.apache.commons.io.IOUtils;
029import org.hibernate.annotations.Immutable;
030import org.hibernate.annotations.Subselect;
031
032import java.io.IOException;
033import java.io.Reader;
034import java.io.Serializable;
035import java.sql.Clob;
036import java.sql.SQLException;
037
038@Entity
039@Immutable
040@Subselect(
041                /*
042                 * Note about the CONCAT function below- We need a primary key (an @Id) column
043                 * because hibernate won't allow the view the function without it, but
044                 */
045                "SELECT CONCAT_WS(' ', vsc.PID, vscd.PID) AS PID, " + "       vsc.PID                         AS CONCEPT_PID, "
046                                + "       vsc.VALUESET_PID                   AS CONCEPT_VALUESET_PID, "
047                                + "       vsc.VALUESET_ORDER                 AS CONCEPT_VALUESET_ORDER, "
048                                + "       vsc.SYSTEM_URL                     AS CONCEPT_SYSTEM_URL, "
049                                + "       vsc.CODEVAL                        AS CONCEPT_CODEVAL, "
050                                + "       vsc.DISPLAY                        AS CONCEPT_DISPLAY, "
051                                + "       vsc.SYSTEM_VER                     AS SYSTEM_VER, "
052                                + "       vsc.SOURCE_PID                     AS SOURCE_PID, "
053                                + "       vsc.SOURCE_DIRECT_PARENT_PIDS      AS SOURCE_DIRECT_PARENT_PIDS, "
054                                + "       vsc.SOURCE_DIRECT_PARENT_PIDS_VC   AS SOURCE_DIRECT_PARENT_PIDS_VC, "
055                                + "       vscd.PID                           AS DESIGNATION_PID, "
056                                + "       vscd.LANG                          AS DESIGNATION_LANG, "
057                                + "       vscd.USE_SYSTEM                    AS DESIGNATION_USE_SYSTEM, "
058                                + "       vscd.USE_CODE                      AS DESIGNATION_USE_CODE, "
059                                + "       vscd.USE_DISPLAY                   AS DESIGNATION_USE_DISPLAY, "
060                                + "       vscd.VAL                           AS DESIGNATION_VAL "
061                                + "FROM TRM_VALUESET_CONCEPT vsc "
062                                + "LEFT OUTER JOIN TRM_VALUESET_C_DESIGNATION vscd ON vsc.PID = vscd.VALUESET_CONCEPT_PID")
063public class TermValueSetConceptView implements Serializable, ITermValueSetConceptView {
064        private static final long serialVersionUID = 1L;
065
066        @Id
067        @Column(name = "PID", length = 1000 /* length only needed to satisfy JpaEntityTest, it's not used*/)
068        private String id; // still set automatically
069
070        @Column(name = "CONCEPT_PID")
071        private Long myConceptPid;
072
073        @Column(name = "CONCEPT_VALUESET_PID")
074        private Long myConceptValueSetPid;
075
076        @Column(name = "CONCEPT_VALUESET_ORDER")
077        private int myConceptOrder;
078
079        @Column(name = "CONCEPT_SYSTEM_URL", length = TermCodeSystem.MAX_URL_LENGTH)
080        private String myConceptSystemUrl;
081
082        @Column(name = "CONCEPT_CODEVAL", length = TermConcept.MAX_CODE_LENGTH)
083        private String myConceptCode;
084
085        @Column(name = "CONCEPT_DISPLAY", length = TermConcept.MAX_DESC_LENGTH)
086        private String myConceptDisplay;
087
088        @Column(name = "SYSTEM_VER", length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
089        private String myConceptSystemVersion;
090
091        @Column(name = "DESIGNATION_PID")
092        private Long myDesignationPid;
093
094        @Column(name = "DESIGNATION_LANG", length = TermConceptDesignation.MAX_LENGTH)
095        private String myDesignationLang;
096
097        @Column(name = "DESIGNATION_USE_SYSTEM", length = TermConceptDesignation.MAX_LENGTH)
098        private String myDesignationUseSystem;
099
100        @Column(name = "DESIGNATION_USE_CODE", length = TermConceptDesignation.MAX_LENGTH)
101        private String myDesignationUseCode;
102
103        @Column(name = "DESIGNATION_USE_DISPLAY", length = TermConceptDesignation.MAX_LENGTH)
104        private String myDesignationUseDisplay;
105
106        @Column(name = "DESIGNATION_VAL", length = TermConceptDesignation.MAX_VAL_LENGTH)
107        private String myDesignationVal;
108
109        @Column(name = "SOURCE_PID", nullable = true)
110        private Long mySourceConceptPid;
111
112        @Lob
113        @Column(name = "SOURCE_DIRECT_PARENT_PIDS", nullable = true)
114        private Clob mySourceConceptDirectParentPids;
115
116        @Column(name = "SOURCE_DIRECT_PARENT_PIDS_VC", nullable = true)
117        private String mySourceConceptDirectParentPidsVc;
118
119        @Override
120        public Long getSourceConceptPid() {
121                return mySourceConceptPid;
122        }
123
124        @Override
125        public String getSourceConceptDirectParentPids() {
126                String retVal = null;
127
128                if (mySourceConceptDirectParentPids != null) {
129                        try (Reader characterStream = mySourceConceptDirectParentPids.getCharacterStream()) {
130                                retVal = IOUtils.toString(characterStream);
131                        } catch (IOException | SQLException e) {
132                                throw new InternalErrorException(Msg.code(828) + e);
133                        }
134                } else if (mySourceConceptDirectParentPidsVc != null) {
135                        retVal = mySourceConceptDirectParentPidsVc;
136                }
137
138                return retVal;
139        }
140
141        @Override
142        public Long getConceptPid() {
143                return myConceptPid;
144        }
145
146        @Override
147        public String getConceptSystemUrl() {
148                return myConceptSystemUrl;
149        }
150
151        @Override
152        public String getConceptCode() {
153                return myConceptCode;
154        }
155
156        @Override
157        public String getConceptDisplay() {
158                return myConceptDisplay;
159        }
160
161        @Override
162        public Long getDesignationPid() {
163                return myDesignationPid;
164        }
165
166        @Override
167        public String getDesignationLang() {
168                return myDesignationLang;
169        }
170
171        @Override
172        public String getDesignationUseSystem() {
173                return myDesignationUseSystem;
174        }
175
176        @Override
177        public String getDesignationUseCode() {
178                return myDesignationUseCode;
179        }
180
181        @Override
182        public String getDesignationUseDisplay() {
183                return myDesignationUseDisplay;
184        }
185
186        @Override
187        public String getDesignationVal() {
188                return myDesignationVal;
189        }
190
191        @Override
192        public String getConceptSystemVersion() {
193                return myConceptSystemVersion;
194        }
195}