001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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.ForcedId;
024import ca.uhn.fhir.jpa.model.entity.IBaseResourceEntity;
025import ca.uhn.fhir.jpa.model.entity.PartitionablePartitionId;
026import ca.uhn.fhir.jpa.model.entity.ResourceEncodingEnum;
027import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable;
028import ca.uhn.fhir.model.primitive.IdDt;
029import ca.uhn.fhir.model.primitive.InstantDt;
030import ca.uhn.fhir.rest.api.Constants;
031import org.hibernate.annotations.Immutable;
032import org.hibernate.annotations.Subselect;
033
034import java.io.Serializable;
035import java.util.Date;
036import javax.annotation.Nullable;
037import javax.persistence.Column;
038import javax.persistence.Entity;
039import javax.persistence.EnumType;
040import javax.persistence.Enumerated;
041import javax.persistence.Id;
042import javax.persistence.Lob;
043import javax.persistence.Temporal;
044import javax.persistence.TemporalType;
045
046@SuppressWarnings("SqlDialectInspection")
047@Entity
048@Immutable
049@Subselect("SELECT h.pid               as pid,            " + "               r.res_id            as res_id,         "
050                + "               h.res_type          as res_type,       "
051                + "               h.res_version       as res_version,    "
052                + // FHIR version
053                "               h.res_ver           as res_ver,        "
054                + // resource version
055                "               h.has_tags          as has_tags,       "
056                + "               h.res_deleted_at    as res_deleted_at, "
057                + "               h.res_published     as res_published,  "
058                + "               h.res_updated       as res_updated,    "
059                + "               h.res_text          as res_text,       "
060                + "               h.res_text_vc       as res_text_vc,    "
061                + "               h.res_encoding      as res_encoding,   "
062                + "               h.PARTITION_ID      as PARTITION_ID,   "
063                + "               p.SOURCE_URI        as PROV_SOURCE_URI,"
064                + "               p.REQUEST_ID        as PROV_REQUEST_ID,"
065                + "               f.forced_id         as FORCED_PID      "
066                + "FROM HFJ_RES_VER h "
067                + "    LEFT OUTER JOIN HFJ_FORCED_ID f ON f.resource_pid = h.res_id "
068                + "    LEFT OUTER JOIN HFJ_RES_VER_PROV p ON p.res_ver_pid = h.pid "
069                + "    INNER JOIN HFJ_RESOURCE r       ON r.res_id = h.res_id and r.res_ver = h.res_ver")
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 = "FORCED_PID", length = ForcedId.MAX_FORCED_ID_LENGTH)
124        private String myForcedPid;
125
126        @Column(name = "PARTITION_ID")
127        private Integer myPartitionId;
128
129        public ResourceSearchView() {}
130
131        public String getResourceTextVc() {
132                return myResourceTextVc;
133        }
134
135        public String getProvenanceRequestId() {
136                return myProvenanceRequestId;
137        }
138
139        public String getProvenanceSourceUri() {
140                return myProvenanceSourceUri;
141        }
142
143        @Override
144        public Date getDeleted() {
145                return myDeleted;
146        }
147
148        public void setDeleted(Date theDate) {
149                myDeleted = theDate;
150        }
151
152        @Override
153        public FhirVersionEnum getFhirVersion() {
154                return myFhirVersion;
155        }
156
157        public void setFhirVersion(FhirVersionEnum theFhirVersion) {
158                myFhirVersion = theFhirVersion;
159        }
160
161        public String getForcedId() {
162                return myForcedPid;
163        }
164
165        @Override
166        public Long getId() {
167                return myResourceId;
168        }
169
170        @Override
171        public IdDt getIdDt() {
172                if (myForcedPid == null) {
173                        Long id = myResourceId;
174                        return new IdDt(myResourceType + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
175                } else {
176                        return new IdDt(
177                                        getResourceType() + '/' + getForcedId() + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
178                }
179        }
180
181        @Override
182        public InstantDt getPublished() {
183                if (myPublished != null) {
184                        return new InstantDt(myPublished);
185                } else {
186                        return null;
187                }
188        }
189
190        public void setPublished(Date thePublished) {
191                myPublished = thePublished;
192        }
193
194        @Override
195        public Long getResourceId() {
196                return myResourceId;
197        }
198
199        @Override
200        public String getResourceType() {
201                return myResourceType;
202        }
203
204        @Override
205        public InstantDt getUpdated() {
206                return new InstantDt(myUpdated);
207        }
208
209        @Override
210        public Date getUpdatedDate() {
211                return myUpdated;
212        }
213
214        @Override
215        public long getVersion() {
216                return myResourceVersion;
217        }
218
219        @Override
220        public boolean isHasTags() {
221                return myHasTags;
222        }
223
224        @Override
225        @Nullable
226        public PartitionablePartitionId getPartitionId() {
227                if (myPartitionId != null) {
228                        return new PartitionablePartitionId(myPartitionId, null);
229                } else {
230                        return null;
231                }
232        }
233
234        public byte[] getResource() {
235                return myResource;
236        }
237
238        public ResourceEncodingEnum getEncoding() {
239                return myEncoding;
240        }
241}