|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.experimental.core.repository.dao; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.springframework.batch.core.JobExecution; |
| 22 | +import org.springframework.batch.core.StepExecution; |
| 23 | +import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
| 24 | +import org.springframework.batch.experimental.core.repository.persistence.converter.JobExecutionConverter; |
| 25 | +import org.springframework.batch.item.ExecutionContext; |
| 26 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 27 | +import org.springframework.data.mongodb.core.query.Query; |
| 28 | +import org.springframework.data.mongodb.core.query.Update; |
| 29 | + |
| 30 | +import static org.springframework.data.mongodb.core.query.Criteria.where; |
| 31 | +import static org.springframework.data.mongodb.core.query.Query.query; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Mahmoud Ben Hassine |
| 35 | + */ |
| 36 | +public class MongoExecutionContextDao implements ExecutionContextDao { |
| 37 | + |
| 38 | + private static final String STEP_EXECUTIONS_COLLECTION_NAME = "BATCH_STEP_EXECUTION"; |
| 39 | + |
| 40 | + private static final String JOB_EXECUTIONS_COLLECTION_NAME = "BATCH_JOB_EXECUTION"; |
| 41 | + |
| 42 | + private final JobExecutionConverter jobExecutionConverter = new JobExecutionConverter(); |
| 43 | + |
| 44 | + private final MongoOperations mongoOperations; |
| 45 | + |
| 46 | + public MongoExecutionContextDao(MongoOperations mongoOperations) { |
| 47 | + this.mongoOperations = mongoOperations; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public ExecutionContext getExecutionContext(JobExecution jobExecution) { |
| 52 | + org.springframework.batch.experimental.core.repository.persistence.JobExecution execution = this.mongoOperations.findById( |
| 53 | + jobExecution.getId(), org.springframework.batch.experimental.core.repository.persistence.JobExecution.class, |
| 54 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 55 | + if (execution == null) { |
| 56 | + return new ExecutionContext(); |
| 57 | + } |
| 58 | + return new ExecutionContext(execution.getExecutionContext().map()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public ExecutionContext getExecutionContext(StepExecution stepExecution) { |
| 63 | + org.springframework.batch.experimental.core.repository.persistence.StepExecution execution = this.mongoOperations.findById( |
| 64 | + stepExecution.getId(), org.springframework.batch.experimental.core.repository.persistence.StepExecution.class, |
| 65 | + STEP_EXECUTIONS_COLLECTION_NAME); |
| 66 | + if (execution == null) { |
| 67 | + return new ExecutionContext(); |
| 68 | + } |
| 69 | + return new ExecutionContext(execution.getExecutionContext().map()); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void saveExecutionContext(JobExecution jobExecution) { |
| 74 | + ExecutionContext executionContext = jobExecution.getExecutionContext(); |
| 75 | + Query query = query(where("_id").is(jobExecution.getId())); |
| 76 | + |
| 77 | + // TODO use ExecutionContext#toMap introduced in v5.1 |
| 78 | + Map<String, Object> map = Map.ofEntries(executionContext.entrySet().toArray(new Map.Entry[0])); |
| 79 | + |
| 80 | + Update update = Update.update("executionContext", |
| 81 | + new org.springframework.batch.experimental.core.repository.persistence.ExecutionContext(map, executionContext.isDirty())); |
| 82 | + this.mongoOperations.updateFirst(query, update, |
| 83 | + org.springframework.batch.experimental.core.repository.persistence.JobExecution.class, |
| 84 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void saveExecutionContext(StepExecution stepExecution) { |
| 89 | + ExecutionContext executionContext = stepExecution.getExecutionContext(); |
| 90 | + Query query = query(where("_id").is(stepExecution.getId())); |
| 91 | + |
| 92 | + // TODO use ExecutionContext#toMap introduced in v5.1 |
| 93 | + Map<String, Object> map = Map.ofEntries(executionContext.entrySet().toArray(new Map.Entry[0])); |
| 94 | + |
| 95 | + Update update = Update.update("executionContext", |
| 96 | + new org.springframework.batch.experimental.core.repository.persistence.ExecutionContext(map, executionContext.isDirty())); |
| 97 | + this.mongoOperations.updateFirst(query, update, |
| 98 | + org.springframework.batch.experimental.core.repository.persistence.StepExecution.class, |
| 99 | + STEP_EXECUTIONS_COLLECTION_NAME); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void saveExecutionContexts(Collection<StepExecution> stepExecutions) { |
| 105 | + for (StepExecution stepExecution : stepExecutions) { |
| 106 | + saveExecutionContext(stepExecution); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void updateExecutionContext(JobExecution jobExecution) { |
| 112 | + saveExecutionContext(jobExecution); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void updateExecutionContext(StepExecution stepExecution) { |
| 117 | + saveExecutionContext(stepExecution); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments