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.context.FhirVersionEnum;
023import ca.uhn.fhir.jpa.model.entity.IBaseResourceEntity;
024import ca.uhn.fhir.jpa.model.entity.PartitionablePartitionId;
025import ca.uhn.fhir.jpa.model.entity.ResourceEncodingEnum;
026import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable;
027import ca.uhn.fhir.jpa.model.entity.ResourceTable;
028import ca.uhn.fhir.model.primitive.IdDt;
029import ca.uhn.fhir.model.primitive.InstantDt;
030import ca.uhn.fhir.rest.api.Constants;
031import jakarta.annotation.Nullable;
032import jakarta.persistence.Column;
033import jakarta.persistence.Entity;
034import jakarta.persistence.EnumType;
035import jakarta.persistence.Enumerated;
036import jakarta.persistence.Id;
037import jakarta.persistence.Lob;
038import jakarta.persistence.Temporal;
039import jakarta.persistence.TemporalType;
040import org.hibernate.annotations.Immutable;
041import org.hibernate.annotations.Subselect;
042
043import java.io.Serializable;
044import java.util.Date;
045
046@SuppressWarnings("SqlDialectInspection")
047@Entity
048@Immutable
049@Subselect("SELECT h.pid               as pid,            "
050                + "               r.res_id            as res_id,         "
051                + "               h.res_type          as res_type,       "
052                + "               h.res_version       as res_version,    "
053                // FHIR version
054                + "               h.res_ver           as res_ver,        "
055                // resource version
056                + "               h.has_tags          as has_tags,       "
057                + "               h.res_deleted_at    as res_deleted_at, "
058                + "               h.res_published     as res_published,  "
059                + "               h.res_updated       as res_updated,    "
060                + "               h.res_text          as res_text,       "
061                + "               h.res_text_vc       as res_text_vc,    "
062                + "               h.res_encoding      as res_encoding,   "
063                + "               h.PARTITION_ID      as PARTITION_ID,   "
064                + "               p.SOURCE_URI        as PROV_SOURCE_URI,"
065                + "               p.REQUEST_ID        as PROV_REQUEST_ID,"
066                + "               r.fhir_id         as FHIR_ID      "
067                + "FROM HFJ_RESOURCE r "
068                + "    INNER JOIN HFJ_RES_VER h ON r.res_id = h.res_id and r.res_ver = h.res_ver"
069                + "    LEFT OUTER JOIN HFJ_RES_VER_PROV p ON p.res_ver_pid = h.pid ")
070public class ResourceSearchView implements IBaseResourceEntity, Serializable {
071
072        private static final long serialVersionUID = 1L;
073
074        @Id
075        @Column(name = "PID")
076        private Long myId;
077
078        @Column(name = "RES_ID")
079        private Long myResourceId;
080
081        @Column(name = "RES_TYPE", length = Constants.MAX_RESOURCE_NAME_LENGTH)
082        private String myResourceType;
083
084        @Column(name = "RES_VERSION")
085        @Enumerated(EnumType.STRING)
086        private FhirVersionEnum myFhirVersion;
087
088        @Column(name = "RES_VER")
089        private Long myResourceVersion;
090
091        @Column(name = "PROV_REQUEST_ID", length = Constants.REQUEST_ID_LENGTH)
092        private String myProvenanceRequestId;
093
094        @Column(name = "PROV_SOURCE_URI", length = ResourceHistoryTable.SOURCE_URI_LENGTH)
095        private String myProvenanceSourceUri;
096
097        @Column(name = "HAS_TAGS")
098        private boolean myHasTags;
099
100        @Column(name = "RES_DELETED_AT")
101        @Temporal(TemporalType.TIMESTAMP)
102        private Date myDeleted;
103
104        @Temporal(TemporalType.TIMESTAMP)
105        @Column(name = "RES_PUBLISHED")
106        private Date myPublished;
107
108        @Temporal(TemporalType.TIMESTAMP)
109        @Column(name = "RES_UPDATED")
110        private Date myUpdated;
111
112        @Column(name = "RES_TEXT")
113        @Lob()
114        private byte[] myResource;
115
116        @Column(name = "RES_TEXT_VC")
117        private String myResourceTextVc;
118
119        @Column(name = "RES_ENCODING")
120        @Enumerated(EnumType.STRING)
121        private ResourceEncodingEnum myEncoding;
122
123        @Column(name = "FHIR_ID", length = ResourceTable.MAX_FORCED_ID_LENGTH)
124        private String myFhirId;
125
126        @Column(name = "PARTITION_ID")
127        private Integer myPartitionId;
128
129        public ResourceSearchView() {
130                // public constructor for Hibernate
131        }
132
133        public String getResourceTextVc() {
134                return myResourceTextVc;
135        }
136
137        public String getProvenanceRequestId() {
138                return myProvenanceRequestId;
139        }
140
141        public String getProvenanceSourceUri() {
142                return myProvenanceSourceUri;
143        }
144
145        @Override
146        public Date getDeleted() {
147                return myDeleted;
148        }
149
150        public void setDeleted(Date theDate) {
151                myDeleted = theDate;
152        }
153
154        @Override
155        public FhirVersionEnum getFhirVersion() {
156                return myFhirVersion;
157        }
158
159        public void setFhirVersion(FhirVersionEnum theFhirVersion) {
160                myFhirVersion = theFhirVersion;
161        }
162
163        public String getFhirId() {
164                return myFhirId;
165        }
166
167        @Override
168        public Long getId() {
169                return myResourceId;
170        }
171
172        @Override
173        public IdDt getIdDt() {
174                if (myFhirId == null) {
175                        Long id = myResourceId;
176                        return new IdDt(myResourceType + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
177                } else {
178                        return new IdDt(getResourceType() + '/' + getFhirId() + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
179                }
180        }
181
182        @Override
183        public InstantDt getPublished() {
184                if (myPublished != null) {
185                        return new InstantDt(myPublished);
186                } else {
187                        return null;
188                }
189        }
190
191        public void setPublished(Date thePublished) {
192                myPublished = thePublished;
193        }
194
195        @Override
196        public Long getResourceId() {
197                return myResourceId;
198        }
199
200        @Override
201        public String getResourceType() {
202                return myResourceType;
203        }
204
205        @Override
206        public InstantDt getUpdated() {
207                return new InstantDt(myUpdated);
208        }
209
210        @Override
211        public Date getUpdatedDate() {
212                return myUpdated;
213        }
214
215        @Override
216        public long getVersion() {
217                return myResourceVersion;
218        }
219
220        @Override
221        public boolean isHasTags() {
222                return myHasTags;
223        }
224
225        @Override
226        @Nullable
227        public PartitionablePartitionId getPartitionId() {
228                if (myPartitionId != null) {
229                        return new PartitionablePartitionId(myPartitionId, null);
230                } else {
231                        return null;
232                }
233        }
234
235        public byte[] getResource() {
236                return myResource;
237        }
238
239        public ResourceEncodingEnum getEncoding() {
240                return myEncoding;
241        }
242}