001/* 002 * Copyright 2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.item.data.builder; 018 019import org.springframework.batch.item.data.MongoItemWriter; 020import org.springframework.data.mongodb.core.MongoOperations; 021import org.springframework.util.Assert; 022 023/** 024 * A builder implementation for the {@link MongoItemWriter} 025 * 026 * @author Glenn Renfro 027 * @since 4.0 028 * @see MongoItemWriter 029 */ 030public class MongoItemWriterBuilder<T> { 031 032 private MongoOperations template; 033 034 private String collection; 035 036 private boolean delete = false; 037 038 /** 039 * Indicates if the items being passed to the writer are to be saved or removed from 040 * the data store. If set to false (default), the items will be saved. If set to true, 041 * the items will be removed. 042 * 043 * @param delete removal indicator 044 * @return The current instance of the builder 045 * @see MongoItemWriter#setDelete(boolean) 046 */ 047 public MongoItemWriterBuilder<T> delete(boolean delete) { 048 this.delete = delete; 049 050 return this; 051 } 052 053 /** 054 * Set the {@link MongoOperations} to be used to save items to be written. 055 * 056 * @param template the template implementation to be used. 057 * @return The current instance of the builder 058 * @see MongoItemWriter#setTemplate(MongoOperations) 059 */ 060 public MongoItemWriterBuilder<T> template(MongoOperations template) { 061 this.template = template; 062 063 return this; 064 } 065 066 /** 067 * Set the name of the Mongo collection to be written to. 068 * 069 * @param collection the name of the collection. 070 * @return The current instance of the builder 071 * @see MongoItemWriter#setCollection(String) 072 * 073 */ 074 public MongoItemWriterBuilder<T> collection(String collection) { 075 this.collection = collection; 076 077 return this; 078 } 079 080 /** 081 * Validates and builds a {@link MongoItemWriter}. 082 * 083 * @return a {@link MongoItemWriter} 084 */ 085 public MongoItemWriter<T> build() { 086 Assert.notNull(this.template, "template is required."); 087 088 MongoItemWriter<T> writer = new MongoItemWriter<>(); 089 writer.setTemplate(this.template); 090 writer.setDelete(this.delete); 091 writer.setCollection(this.collection); 092 093 return writer; 094 } 095 096}