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.jpa.bulk.imprt.model.BulkImportJobJson;
023import ca.uhn.fhir.jpa.bulk.imprt.model.BulkImportJobStatusEnum;
024import ca.uhn.fhir.jpa.bulk.imprt.model.JobFileRowProcessingModeEnum;
025import jakarta.persistence.Column;
026import jakarta.persistence.Entity;
027import jakarta.persistence.EnumType;
028import jakarta.persistence.Enumerated;
029import jakarta.persistence.GeneratedValue;
030import jakarta.persistence.GenerationType;
031import jakarta.persistence.Id;
032import jakarta.persistence.SequenceGenerator;
033import jakarta.persistence.Table;
034import jakarta.persistence.Temporal;
035import jakarta.persistence.TemporalType;
036import jakarta.persistence.UniqueConstraint;
037import jakarta.persistence.Version;
038
039import java.io.Serializable;
040import java.util.Date;
041
042import static ca.uhn.fhir.rest.api.Constants.UUID_LENGTH;
043import static org.apache.commons.lang3.StringUtils.left;
044
045@Entity
046@Table(
047                name = BulkImportJobEntity.HFJ_BLK_IMPORT_JOB,
048                uniqueConstraints = {@UniqueConstraint(name = "IDX_BLKIM_JOB_ID", columnNames = "JOB_ID")})
049public class BulkImportJobEntity implements Serializable {
050
051        public static final String HFJ_BLK_IMPORT_JOB = "HFJ_BLK_IMPORT_JOB";
052        public static final String JOB_ID = "JOB_ID";
053
054        @Id
055        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_BLKIMJOB_PID")
056        @SequenceGenerator(name = "SEQ_BLKIMJOB_PID", sequenceName = "SEQ_BLKIMJOB_PID")
057        @Column(name = "PID")
058        private Long myId;
059
060        @Column(name = JOB_ID, length = UUID_LENGTH, nullable = false, updatable = false)
061        private String myJobId;
062
063        @Column(name = "JOB_DESC", nullable = true, length = BulkExportJobEntity.STATUS_MESSAGE_LEN)
064        private String myJobDescription;
065
066        @Enumerated(EnumType.STRING)
067        @Column(name = "JOB_STATUS", length = 10, nullable = false)
068        private BulkImportJobStatusEnum myStatus;
069
070        @Version
071        @Column(name = "OPTLOCK", nullable = false)
072        private int myVersion;
073
074        @Column(name = "FILE_COUNT", nullable = false)
075        private int myFileCount;
076
077        @Temporal(TemporalType.TIMESTAMP)
078        @Column(name = "STATUS_TIME", nullable = false)
079        private Date myStatusTime;
080
081        @Column(name = "STATUS_MESSAGE", nullable = true, length = BulkExportJobEntity.STATUS_MESSAGE_LEN)
082        private String myStatusMessage;
083
084        @Column(name = "ROW_PROCESSING_MODE", length = 20, nullable = false, updatable = false)
085        @Enumerated(EnumType.STRING)
086        private JobFileRowProcessingModeEnum myRowProcessingMode;
087
088        @Column(name = "BATCH_SIZE", nullable = false, updatable = false)
089        private int myBatchSize;
090
091        public String getJobDescription() {
092                return myJobDescription;
093        }
094
095        public void setJobDescription(String theJobDescription) {
096                myJobDescription = left(theJobDescription, BulkExportJobEntity.STATUS_MESSAGE_LEN);
097        }
098
099        public JobFileRowProcessingModeEnum getRowProcessingMode() {
100                return myRowProcessingMode;
101        }
102
103        public void setRowProcessingMode(JobFileRowProcessingModeEnum theRowProcessingMode) {
104                myRowProcessingMode = theRowProcessingMode;
105        }
106
107        public Date getStatusTime() {
108                return myStatusTime;
109        }
110
111        public void setStatusTime(Date theStatusTime) {
112                myStatusTime = theStatusTime;
113        }
114
115        public int getFileCount() {
116                return myFileCount;
117        }
118
119        public void setFileCount(int theFileCount) {
120                myFileCount = theFileCount;
121        }
122
123        public String getJobId() {
124                return myJobId;
125        }
126
127        public void setJobId(String theJobId) {
128                myJobId = theJobId;
129        }
130
131        public BulkImportJobStatusEnum getStatus() {
132                return myStatus;
133        }
134
135        /**
136         * Sets the status, updates the status time, and clears the status message
137         */
138        public void setStatus(BulkImportJobStatusEnum theStatus) {
139                if (myStatus != theStatus) {
140                        myStatus = theStatus;
141                        setStatusTime(new Date());
142                        setStatusMessage(null);
143                }
144        }
145
146        public String getStatusMessage() {
147                return myStatusMessage;
148        }
149
150        public void setStatusMessage(String theStatusMessage) {
151                myStatusMessage = left(theStatusMessage, BulkExportJobEntity.STATUS_MESSAGE_LEN);
152        }
153
154        public BulkImportJobJson toJson() {
155                return new BulkImportJobJson()
156                                .setProcessingMode(getRowProcessingMode())
157                                .setFileCount(getFileCount())
158                                .setJobDescription(getJobDescription());
159        }
160
161        public int getBatchSize() {
162                return myBatchSize;
163        }
164
165        public void setBatchSize(int theBatchSize) {
166                myBatchSize = theBatchSize;
167        }
168}