001package ca.uhn.fhir.jpa.entity;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2023 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.jpa.bulk.export.model.BulkExportJobStatusEnum;
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.commons.lang3.builder.ToStringStyle;
026import org.hl7.fhir.r5.model.InstantType;
027
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.EnumType;
031import javax.persistence.Enumerated;
032import javax.persistence.FetchType;
033import javax.persistence.GeneratedValue;
034import javax.persistence.GenerationType;
035import javax.persistence.Id;
036import javax.persistence.Index;
037import javax.persistence.OneToMany;
038import javax.persistence.SequenceGenerator;
039import javax.persistence.Table;
040import javax.persistence.Temporal;
041import javax.persistence.TemporalType;
042import javax.persistence.UniqueConstraint;
043import javax.persistence.Version;
044import java.io.Serializable;
045import java.util.ArrayList;
046import java.util.Collection;
047import java.util.Date;
048
049import static org.apache.commons.lang3.StringUtils.isNotBlank;
050import static org.apache.commons.lang3.StringUtils.left;
051
052
053/*
054 * These classes are no longer needed.
055 * Metadata on the job is contained in the job itself
056 * (no separate storage required).
057 *
058 * See the BulkExportAppCtx for job details
059 */
060@Entity
061@Table(name = "HFJ_BLK_EXPORT_JOB", uniqueConstraints = {
062                  @UniqueConstraint(name = "IDX_BLKEX_JOB_ID", columnNames = "JOB_ID")
063}, indexes = {
064                  @Index(name = "IDX_BLKEX_EXPTIME", columnList = "EXP_TIME")
065})
066@Deprecated
067public class BulkExportJobEntity implements Serializable {
068
069        public static final int REQUEST_LENGTH = 1024;
070        public static final int STATUS_MESSAGE_LEN = 500;
071        @Id
072        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_BLKEXJOB_PID")
073        @SequenceGenerator(name = "SEQ_BLKEXJOB_PID", sequenceName = "SEQ_BLKEXJOB_PID")
074        @Column(name = "PID")
075        private Long myId;
076
077        @Column(name = "JOB_ID", length = Search.UUID_COLUMN_LENGTH, nullable = false)
078        private String myJobId;
079
080        @Enumerated(EnumType.STRING)
081        @Column(name = "JOB_STATUS", length = 10, nullable = false)
082        private BulkExportJobStatusEnum myStatus;
083        @Temporal(TemporalType.TIMESTAMP)
084        @Column(name = "CREATED_TIME", nullable = false)
085        private Date myCreated;
086        @Temporal(TemporalType.TIMESTAMP)
087        @Column(name = "STATUS_TIME", nullable = false)
088        private Date myStatusTime;
089        @Temporal(TemporalType.TIMESTAMP)
090        @Column(name = "EXP_TIME", nullable = true)
091        private Date myExpiry;
092        @Column(name = "REQUEST", nullable = false, length = REQUEST_LENGTH)
093        private String myRequest;
094        @OneToMany(fetch = FetchType.LAZY, mappedBy = "myJob", orphanRemoval = false)
095        private Collection<BulkExportCollectionEntity> myCollections;
096        @Version
097        @Column(name = "OPTLOCK", nullable = false)
098        private int myVersion;
099        @Temporal(TemporalType.TIMESTAMP)
100        @Column(name = "EXP_SINCE", nullable = true)
101        private Date mySince;
102        @Column(name = "STATUS_MESSAGE", nullable = true, length = STATUS_MESSAGE_LEN)
103        private String myStatusMessage;
104
105        public Date getCreated() {
106                return myCreated;
107        }
108
109        public void setCreated(Date theCreated) {
110                myCreated = theCreated;
111        }
112
113        public String getStatusMessage() {
114                return myStatusMessage;
115        }
116
117        public void setStatusMessage(String theStatusMessage) {
118                myStatusMessage = left(theStatusMessage, STATUS_MESSAGE_LEN);
119        }
120
121        public String getRequest() {
122                return myRequest;
123        }
124
125        public void setRequest(String theRequest) {
126                myRequest = theRequest;
127        }
128
129        public void setExpiry(Date theExpiry) {
130                myExpiry = theExpiry;
131        }
132
133        public Collection<BulkExportCollectionEntity> getCollections() {
134                if (myCollections == null) {
135                        myCollections = new ArrayList<>();
136                }
137                return myCollections;
138        }
139
140        public String getJobId() {
141                return myJobId;
142        }
143
144        public void setJobId(String theJobId) {
145                myJobId = theJobId;
146        }
147
148        @Override
149        public String toString() {
150                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
151                b.append("pid", myId);
152                if (isNotBlank(myJobId)) {
153                        b.append("jobId", myJobId);
154                }
155                if (myStatus != null) {
156                        b.append("status", myStatus + " " + new InstantType(myStatusTime).getValueAsString());
157                }
158                b.append("created", new InstantType(myCreated).getValueAsString());
159                b.append("expiry", new InstantType(myExpiry).getValueAsString());
160                b.append("request", myRequest);
161                b.append("since", mySince);
162                if (isNotBlank(myStatusMessage)) {
163                        b.append("statusMessage", myStatusMessage);
164                }
165                return b.toString();
166        }
167
168        public BulkExportJobStatusEnum getStatus() {
169                return myStatus;
170        }
171
172        public void setStatus(BulkExportJobStatusEnum theStatus) {
173                if (myStatus != theStatus) {
174                        myStatusTime = new Date();
175                        myStatus = theStatus;
176                }
177        }
178
179        public Date getStatusTime() {
180                return myStatusTime;
181        }
182
183        public int getVersion() {
184                return myVersion;
185        }
186
187        public void setVersion(int theVersion) {
188                myVersion = theVersion;
189        }
190
191        public Long getId() {
192                return myId;
193        }
194
195        public Date getSince() {
196                if (mySince != null) {
197                        return new Date(mySince.getTime());
198                }
199                return null;
200        }
201
202        public void setSince(Date theSince) {
203                mySince = theSince;
204        }
205}