View Javadoc
1   /*
2    * Copyright 2010-2022 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.mybatis.spring.batch.builder;
17  
18  import java.util.Map;
19  import java.util.Optional;
20  import java.util.function.Supplier;
21  
22  import org.apache.ibatis.session.SqlSessionFactory;
23  import org.mybatis.spring.batch.MyBatisCursorItemReader;
24  
25  /**
26   * A builder for the {@link MyBatisCursorItemReader}.
27   *
28   * @author Kazuki Shimizu
29   *
30   * @since 2.0.0
31   *
32   * @see MyBatisCursorItemReader
33   */
34  public class MyBatisCursorItemReaderBuilder<T> {
35  
36    private SqlSessionFactory sqlSessionFactory;
37    private String queryId;
38    private Map<String, Object> parameterValues;
39    private Supplier<Map<String, Object>> parameterValuesSupplier;
40    private Boolean saveState;
41    private Integer maxItemCount;
42  
43    /**
44     * Set the {@link SqlSessionFactory} to be used by reader for database access.
45     *
46     * @param sqlSessionFactory
47     *          the {@link SqlSessionFactory} to be used by writer for database access
48     *
49     * @return this instance for method chaining
50     *
51     * @see MyBatisCursorItemReader#setSqlSessionFactory(SqlSessionFactory)
52     */
53    public MyBatisCursorItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
54      this.sqlSessionFactory = sqlSessionFactory;
55      return this;
56    }
57  
58    /**
59     * Set the query id identifying the statement in the SqlMap configuration file.
60     *
61     * @param queryId
62     *          the id for the query
63     *
64     * @return this instance for method chaining
65     *
66     * @see MyBatisCursorItemReader#setQueryId(String)
67     */
68    public MyBatisCursorItemReaderBuilder<T> queryId(String queryId) {
69      this.queryId = queryId;
70      return this;
71    }
72  
73    /**
74     * Set the parameter values to be used for the query execution.
75     *
76     * @param parameterValues
77     *          the parameter values to be used for the query execution
78     *
79     * @return this instance for method chaining
80     *
81     * @see MyBatisCursorItemReader#setParameterValues(Map)
82     */
83    public MyBatisCursorItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
84      this.parameterValues = parameterValues;
85      return this;
86    }
87  
88    /**
89     * Set the parameter supplier to be used to get parameters for the query execution.
90     *
91     * @param parameterValuesSupplier
92     *          the parameter supplier to be used to get parameters for the query execution
93     *
94     * @return this instance for method chaining
95     *
96     * @see MyBatisCursorItemReader#setParameterValuesSupplier(Supplier)
97     *
98     * @since 2.1.0
99     */
100   public MyBatisCursorItemReaderBuilder<T> parameterValuesSupplier(
101       Supplier<Map<String, Object>> parameterValuesSupplier) {
102     this.parameterValuesSupplier = parameterValuesSupplier;
103     return this;
104   }
105 
106   /**
107    * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should be persisted within
108    * the {@link org.springframework.batch.item.ExecutionContext} for restart purposes.
109    *
110    * @param saveState
111    *          defaults to true
112    *
113    * @return The current instance of the builder.
114    *
115    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
116    */
117   public MyBatisCursorItemReaderBuilder<T> saveState(boolean saveState) {
118     this.saveState = saveState;
119     return this;
120   }
121 
122   /**
123    * Configure the max number of items to be read.
124    *
125    * @param maxItemCount
126    *          the max items to be read
127    *
128    * @return The current instance of the builder.
129    *
130    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
131    */
132   public MyBatisCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) {
133     this.maxItemCount = maxItemCount;
134     return this;
135   }
136 
137   /**
138    * Returns a fully built {@link MyBatisCursorItemReader}.
139    *
140    * @return the reader
141    */
142   public MyBatisCursorItemReader<T> build() {
143     MyBatisCursorItemReader<T> reader = new MyBatisCursorItemReader<>();
144     reader.setSqlSessionFactory(this.sqlSessionFactory);
145     reader.setQueryId(this.queryId);
146     reader.setParameterValues(this.parameterValues);
147     reader.setParameterValuesSupplier(this.parameterValuesSupplier);
148     Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
149     Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
150     return reader;
151   }
152 
153 }