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