|
| 1 | +/* |
| 2 | + * Copyright 2020 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.item.database.builder; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import javax.persistence.EntityManagerFactory; |
| 21 | + |
| 22 | +import org.springframework.batch.item.ExecutionContext; |
| 23 | +import org.springframework.batch.item.ItemStreamSupport; |
| 24 | +import org.springframework.batch.item.database.JpaCursorItemReader; |
| 25 | +import org.springframework.batch.item.database.orm.JpaQueryProvider; |
| 26 | +import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Builder for {@link JpaCursorItemReader}. |
| 31 | + * |
| 32 | + * @author Mahmoud Ben Hassine |
| 33 | + * |
| 34 | + * @since 4.3 |
| 35 | + */ |
| 36 | +public class JpaCursorItemReaderBuilder<T> { |
| 37 | + |
| 38 | + private EntityManagerFactory entityManagerFactory; |
| 39 | + private String queryString; |
| 40 | + private JpaQueryProvider queryProvider; |
| 41 | + private Map<String, Object> parameterValues; |
| 42 | + private boolean saveState = true; |
| 43 | + private String name; |
| 44 | + private int maxItemCount = Integer.MAX_VALUE; |
| 45 | + private int currentItemCount; |
| 46 | + |
| 47 | + /** |
| 48 | + * Configure if the state of the {@link ItemStreamSupport} |
| 49 | + * should be persisted within the {@link ExecutionContext} |
| 50 | + * for restart purposes. |
| 51 | + * |
| 52 | + * @param saveState defaults to true |
| 53 | + * @return The current instance of the builder. |
| 54 | + */ |
| 55 | + public JpaCursorItemReaderBuilder<T> saveState(boolean saveState) { |
| 56 | + this.saveState = saveState; |
| 57 | + |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The name used to calculate the key within the {@link ExecutionContext}. |
| 63 | + * Required if {@link #saveState(boolean)} is set to true. |
| 64 | + * |
| 65 | + * @param name name of the reader instance |
| 66 | + * @return The current instance of the builder. |
| 67 | + * @see ItemStreamSupport#setName(String) |
| 68 | + */ |
| 69 | + public JpaCursorItemReaderBuilder<T> name(String name) { |
| 70 | + this.name = name; |
| 71 | + |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Configure the max number of items to be read. |
| 77 | + * |
| 78 | + * @param maxItemCount the max items to be read |
| 79 | + * @return The current instance of the builder. |
| 80 | + * @see AbstractItemCountingItemStreamItemReader#setMaxItemCount(int) |
| 81 | + */ |
| 82 | + public JpaCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) { |
| 83 | + this.maxItemCount = maxItemCount; |
| 84 | + |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Index for the current item. Used on restarts to indicate where to start from. |
| 90 | + * |
| 91 | + * @param currentItemCount current index |
| 92 | + * @return this instance for method chaining |
| 93 | + * @see AbstractItemCountingItemStreamItemReader#setCurrentItemCount(int) |
| 94 | + */ |
| 95 | + public JpaCursorItemReaderBuilder<T> currentItemCount(int currentItemCount) { |
| 96 | + this.currentItemCount = currentItemCount; |
| 97 | + |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * A map of parameter values to be set on the query. The key of the map is |
| 103 | + * the name of the parameter to be set with the value being the value to be set. |
| 104 | + * |
| 105 | + * @param parameterValues map of values |
| 106 | + * @return this instance for method chaining |
| 107 | + * @see JpaCursorItemReader#setParameterValues(Map) |
| 108 | + */ |
| 109 | + public JpaCursorItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) { |
| 110 | + this.parameterValues = parameterValues; |
| 111 | + |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * A query provider. This should be set only if {@link #queryString(String)} |
| 117 | + * have not been set. |
| 118 | + * |
| 119 | + * @param queryProvider the query provider |
| 120 | + * @return this instance for method chaining |
| 121 | + * @see JpaCursorItemReader#setQueryProvider(JpaQueryProvider) |
| 122 | + */ |
| 123 | + public JpaCursorItemReaderBuilder<T> queryProvider(JpaQueryProvider queryProvider) { |
| 124 | + this.queryProvider = queryProvider; |
| 125 | + |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The JPQL query string to execute. This should only be set if |
| 131 | + * {@link #queryProvider(JpaQueryProvider)} has not been set. |
| 132 | + * |
| 133 | + * @param queryString the JPQL query |
| 134 | + * @return this instance for method chaining |
| 135 | + * @see JpaCursorItemReader#setQueryString(String) |
| 136 | + */ |
| 137 | + public JpaCursorItemReaderBuilder<T> queryString(String queryString) { |
| 138 | + this.queryString = queryString; |
| 139 | + |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * The {@link EntityManagerFactory} to be used for executing the configured |
| 145 | + * {@link #queryString}. |
| 146 | + * |
| 147 | + * @param entityManagerFactory {@link EntityManagerFactory} used to create |
| 148 | + * {@link javax.persistence.EntityManager} |
| 149 | + * @return this instance for method chaining |
| 150 | + */ |
| 151 | + public JpaCursorItemReaderBuilder<T> entityManagerFactory(EntityManagerFactory entityManagerFactory) { |
| 152 | + this.entityManagerFactory = entityManagerFactory; |
| 153 | + |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns a fully constructed {@link JpaCursorItemReader}. |
| 159 | + * |
| 160 | + * @return a new {@link JpaCursorItemReader} |
| 161 | + */ |
| 162 | + public JpaCursorItemReader<T> build() { |
| 163 | + Assert.notNull(this.entityManagerFactory, "An EntityManagerFactory is required"); |
| 164 | + if (this.saveState) { |
| 165 | + Assert.hasText(this.name, "A name is required when saveState is set to true"); |
| 166 | + } |
| 167 | + if (this.queryProvider == null) { |
| 168 | + Assert.hasLength(this.queryString, "Query string is required when queryProvider is null"); |
| 169 | + } |
| 170 | + |
| 171 | + JpaCursorItemReader<T> reader = new JpaCursorItemReader<>(); |
| 172 | + reader.setEntityManagerFactory(this.entityManagerFactory); |
| 173 | + reader.setQueryProvider(this.queryProvider); |
| 174 | + reader.setQueryString(this.queryString); |
| 175 | + reader.setParameterValues(this.parameterValues); |
| 176 | + reader.setCurrentItemCount(this.currentItemCount); |
| 177 | + reader.setMaxItemCount(this.maxItemCount); |
| 178 | + reader.setSaveState(this.saveState); |
| 179 | + reader.setName(this.name); |
| 180 | + return reader; |
| 181 | + } |
| 182 | +} |
0 commit comments