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