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(vsc.PID, CONCAT(' ', vscd.PID)) AS PID, "
046                                + "       vsc.PID                         AS CONCEPT_PID, "
047                                + "       vsc.VALUESET_PID                AS CONCEPT_VALUESET_PID, "
048                                + "       vsc.VALUESET_ORDER              AS CONCEPT_VALUESET_ORDER, "
049                                + "       vsc.SYSTEM_URL                  AS CONCEPT_SYSTEM_URL, "
050                                + "       vsc.CODEVAL                     AS CONCEPT_CODEVAL, "
051                                + "       vsc.DISPLAY                     AS CONCEPT_DISPLAY, "
052                                + "       vsc.SYSTEM_VER                  AS SYSTEM_VER, "
053                                + "       vsc.SOURCE_PID                  AS SOURCE_PID, "
054                                + "       vsc.SOURCE_DIRECT_PARENT_PIDS   AS SOURCE_DIRECT_PARENT_PIDS, "
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 TermValueSetConceptViewOracle 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        @Override
117        public Long getConceptPid() {
118                return myConceptPid;
119        }
120
121        @Override
122        public String getConceptSystemUrl() {
123                return myConceptSystemUrl;
124        }
125
126        @Override
127        public String getConceptCode() {
128                return myConceptCode;
129        }
130
131        @Override
132        public String getConceptDisplay() {
133                return myConceptDisplay;
134        }
135
136        @Override
137        public String getConceptSystemVersion() {
138                return myConceptSystemVersion;
139        }
140
141        @Override
142        public Long getSourceConceptPid() {
143                return mySourceConceptPid;
144        }
145
146        @Override
147        public String getSourceConceptDirectParentPids() {
148                if (mySourceConceptDirectParentPids != null) {
149                        try (Reader characterStream = mySourceConceptDirectParentPids.getCharacterStream()) {
150                                return IOUtils.toString(characterStream);
151                        } catch (IOException | SQLException e) {
152                                throw new InternalErrorException(Msg.code(829) + e);
153                        }
154                }
155                return null;
156        }
157
158        @Override
159        public Long getDesignationPid() {
160                return myDesignationPid;
161        }
162
163        @Override
164        public String getDesignationLang() {
165                return myDesignationLang;
166        }
167
168        @Override
169        public String getDesignationUseSystem() {
170                return myDesignationUseSystem;
171        }
172
173        @Override
174        public String getDesignationUseCode() {
175                return myDesignationUseCode;
176        }
177
178        @Override
179        public String getDesignationUseDisplay() {
180                return myDesignationUseDisplay;
181        }
182
183        @Override
184        public String getDesignationVal() {
185                return myDesignationVal;
186        }
187}