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.batch2.model.WorkChunkStatusEnum;
023import jakarta.persistence.Basic;
024import jakarta.persistence.Column;
025import jakarta.persistence.Entity;
026import jakarta.persistence.EnumType;
027import jakarta.persistence.Enumerated;
028import jakarta.persistence.FetchType;
029import jakarta.persistence.ForeignKey;
030import jakarta.persistence.Id;
031import jakarta.persistence.Index;
032import jakarta.persistence.JoinColumn;
033import jakarta.persistence.Lob;
034import jakarta.persistence.ManyToOne;
035import jakarta.persistence.Table;
036import jakarta.persistence.Temporal;
037import jakarta.persistence.TemporalType;
038import jakarta.persistence.Version;
039import org.apache.commons.lang3.builder.ToStringBuilder;
040import org.apache.commons.lang3.builder.ToStringStyle;
041import org.hibernate.Length;
042
043import java.io.Serializable;
044import java.util.Date;
045
046import static ca.uhn.fhir.batch2.model.JobDefinition.ID_MAX_LENGTH;
047import static ca.uhn.fhir.jpa.entity.Batch2JobInstanceEntity.STATUS_MAX_LENGTH;
048import static org.apache.commons.lang3.StringUtils.left;
049
050@Entity
051@Table(
052                name = "BT2_WORK_CHUNK",
053                indexes = {@Index(name = "IDX_BT2WC_II_SEQ", columnList = "INSTANCE_ID,SEQ")})
054public class Batch2WorkChunkEntity implements Serializable {
055
056        public static final int ERROR_MSG_MAX_LENGTH = 500;
057        public static final int WARNING_MSG_MAX_LENGTH = 4000;
058        private static final long serialVersionUID = -6202771941965780558L;
059
060        @Id
061        @Column(name = "ID", length = ID_MAX_LENGTH)
062        private String myId;
063
064        @Column(name = "SEQ", nullable = false)
065        private int mySequence;
066
067        @Column(name = "CREATE_TIME", nullable = false)
068        @Temporal(TemporalType.TIMESTAMP)
069        private Date myCreateTime;
070
071        @Column(name = "START_TIME", nullable = true)
072        @Temporal(TemporalType.TIMESTAMP)
073        private Date myStartTime;
074
075        @Column(name = "END_TIME", nullable = true)
076        @Temporal(TemporalType.TIMESTAMP)
077        private Date myEndTime;
078
079        @Version
080        @Column(name = "UPDATE_TIME", nullable = true)
081        @Temporal(TemporalType.TIMESTAMP)
082        private Date myUpdateTime;
083
084        @Column(name = "RECORDS_PROCESSED", nullable = true)
085        private Integer myRecordsProcessed;
086
087        @Column(name = "DEFINITION_ID", length = ID_MAX_LENGTH, nullable = false)
088        private String myJobDefinitionId;
089
090        @Column(name = "DEFINITION_VER", length = ID_MAX_LENGTH, nullable = false)
091        private int myJobDefinitionVersion;
092
093        @Column(name = "TGT_STEP_ID", length = ID_MAX_LENGTH, nullable = false)
094        private String myTargetStepId;
095
096        @Lob // TODO: VC column added in 7.2.0 - Remove non-VC column later
097        @Basic(fetch = FetchType.LAZY)
098        @Column(name = "CHUNK_DATA", nullable = true, length = Integer.MAX_VALUE - 1)
099        private String mySerializedData;
100
101        @Column(name = "CHUNK_DATA_VC", nullable = true, length = Length.LONG32)
102        private String mySerializedDataVc;
103
104        @Column(name = "STAT", length = STATUS_MAX_LENGTH, nullable = false)
105        @Enumerated(EnumType.STRING)
106        private WorkChunkStatusEnum myStatus;
107
108        @ManyToOne(fetch = FetchType.LAZY)
109        @JoinColumn(
110                        name = "INSTANCE_ID",
111                        insertable = false,
112                        updatable = false,
113                        foreignKey = @ForeignKey(name = "FK_BT2WC_INSTANCE"))
114        private Batch2JobInstanceEntity myInstance;
115
116        @Column(name = "INSTANCE_ID", length = ID_MAX_LENGTH, nullable = false)
117        private String myInstanceId;
118
119        @Column(name = "ERROR_MSG", length = ERROR_MSG_MAX_LENGTH, nullable = true)
120        private String myErrorMessage;
121
122        @Column(name = "ERROR_COUNT", nullable = false)
123        private int myErrorCount;
124
125        @Column(name = "WARNING_MSG", length = WARNING_MSG_MAX_LENGTH, nullable = true)
126        private String myWarningMessage;
127
128        /**
129         * Default constructor for Hibernate.
130         */
131        public Batch2WorkChunkEntity() {}
132
133        /**
134         * Projection constructor for no-data path.
135         */
136        public Batch2WorkChunkEntity(
137                        String theId,
138                        int theSequence,
139                        String theJobDefinitionId,
140                        int theJobDefinitionVersion,
141                        String theInstanceId,
142                        String theTargetStepId,
143                        WorkChunkStatusEnum theStatus,
144                        Date theCreateTime,
145                        Date theStartTime,
146                        Date theUpdateTime,
147                        Date theEndTime,
148                        String theErrorMessage,
149                        int theErrorCount,
150                        Integer theRecordsProcessed,
151                        String theWarningMessage) {
152                myId = theId;
153                mySequence = theSequence;
154                myJobDefinitionId = theJobDefinitionId;
155                myJobDefinitionVersion = theJobDefinitionVersion;
156                myInstanceId = theInstanceId;
157                myTargetStepId = theTargetStepId;
158                myStatus = theStatus;
159                myCreateTime = theCreateTime;
160                myStartTime = theStartTime;
161                myUpdateTime = theUpdateTime;
162                myEndTime = theEndTime;
163                myErrorMessage = theErrorMessage;
164                myErrorCount = theErrorCount;
165                myRecordsProcessed = theRecordsProcessed;
166                myWarningMessage = theWarningMessage;
167        }
168
169        public int getErrorCount() {
170                return myErrorCount;
171        }
172
173        public void setErrorCount(int theErrorCount) {
174                myErrorCount = theErrorCount;
175        }
176
177        public String getErrorMessage() {
178                return myErrorMessage;
179        }
180
181        public void setErrorMessage(String theErrorMessage) {
182                myErrorMessage = left(theErrorMessage, ERROR_MSG_MAX_LENGTH);
183        }
184
185        public String getWarningMessage() {
186                return myWarningMessage;
187        }
188
189        public void setWarningMessage(String theWarningMessage) {
190                myWarningMessage = theWarningMessage;
191        }
192
193        public int getSequence() {
194                return mySequence;
195        }
196
197        public void setSequence(int theSequence) {
198                mySequence = theSequence;
199        }
200
201        public Date getCreateTime() {
202                return myCreateTime;
203        }
204
205        public void setCreateTime(Date theCreateTime) {
206                myCreateTime = theCreateTime;
207        }
208
209        public Date getStartTime() {
210                return myStartTime;
211        }
212
213        public void setStartTime(Date theStartTime) {
214                myStartTime = theStartTime;
215        }
216
217        public Date getEndTime() {
218                return myEndTime;
219        }
220
221        public void setEndTime(Date theEndTime) {
222                myEndTime = theEndTime;
223        }
224
225        public Date getUpdateTime() {
226                return myUpdateTime;
227        }
228
229        public Integer getRecordsProcessed() {
230                return myRecordsProcessed;
231        }
232
233        public void setRecordsProcessed(Integer theRecordsProcessed) {
234                myRecordsProcessed = theRecordsProcessed;
235        }
236
237        public Batch2JobInstanceEntity getInstance() {
238                return myInstance;
239        }
240
241        public void setInstance(Batch2JobInstanceEntity theInstance) {
242                myInstance = theInstance;
243        }
244
245        public String getJobDefinitionId() {
246                return myJobDefinitionId;
247        }
248
249        public void setJobDefinitionId(String theJobDefinitionId) {
250                myJobDefinitionId = theJobDefinitionId;
251        }
252
253        public int getJobDefinitionVersion() {
254                return myJobDefinitionVersion;
255        }
256
257        public void setJobDefinitionVersion(int theJobDefinitionVersion) {
258                myJobDefinitionVersion = theJobDefinitionVersion;
259        }
260
261        public String getTargetStepId() {
262                return myTargetStepId;
263        }
264
265        public void setTargetStepId(String theTargetStepId) {
266                myTargetStepId = theTargetStepId;
267        }
268
269        public String getSerializedData() {
270                return mySerializedDataVc != null ? mySerializedDataVc : mySerializedData;
271        }
272
273        public void setSerializedData(String theSerializedData) {
274                mySerializedData = null;
275                mySerializedDataVc = theSerializedData;
276        }
277
278        public WorkChunkStatusEnum getStatus() {
279                return myStatus;
280        }
281
282        public void setStatus(WorkChunkStatusEnum theStatus) {
283                myStatus = theStatus;
284        }
285
286        public String getId() {
287                return myId;
288        }
289
290        public void setId(String theId) {
291                myId = theId;
292        }
293
294        public String getInstanceId() {
295                return myInstanceId;
296        }
297
298        public void setInstanceId(String theInstanceId) {
299                myInstanceId = theInstanceId;
300        }
301
302        @Override
303        public String toString() {
304                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
305                                .append("id", myId)
306                                .append("instanceId", myInstanceId)
307                                .append("sequence", mySequence)
308                                .append("errorCount", myErrorCount)
309                                .append("jobDefinitionId", myJobDefinitionId)
310                                .append("jobDefinitionVersion", myJobDefinitionVersion)
311                                .append("createTime", myCreateTime)
312                                .append("startTime", myStartTime)
313                                .append("endTime", myEndTime)
314                                .append("updateTime", myUpdateTime)
315                                .append("recordsProcessed", myRecordsProcessed)
316                                .append("targetStepId", myTargetStepId)
317                                .append("serializedData", getSerializedData())
318                                .append("status", myStatus)
319                                .append("errorMessage", myErrorMessage)
320                                .append("warningMessage", myWarningMessage)
321                                .toString();
322        }
323}