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.neo4j.ogm.session.Session;
020import org.neo4j.ogm.session.SessionFactory;
021
022import org.springframework.batch.item.data.Neo4jItemWriter;
023import org.springframework.util.Assert;
024
025/**
026 * A builder implementation for the {@link Neo4jItemWriter}
027 *
028 * @author Glenn Renfro
029 * @since 4.0
030 * @see Neo4jItemWriter
031 */
032public class Neo4jItemWriterBuilder<T> {
033
034        private boolean delete = false;
035
036        private SessionFactory sessionFactory;
037
038        /**
039         * Boolean flag indicating whether the writer should save or delete the item at write
040         * time.
041         * @param delete true if write should delete item, false if item should be saved.
042         * Default is false.
043         * @return The current instance of the builder
044         * @see Neo4jItemWriter#setDelete(boolean)
045         */
046        public Neo4jItemWriterBuilder<T> delete(boolean delete) {
047                this.delete = delete;
048
049                return this;
050        }
051
052        /**
053         * Establish the session factory that will be used to create {@link Session} instances
054         * for interacting with Neo4j.
055         * @param sessionFactory sessionFactory to be used.
056         * @return The current instance of the builder
057         * @see Neo4jItemWriter#setSessionFactory(SessionFactory)
058         */
059        public Neo4jItemWriterBuilder<T> sessionFactory(SessionFactory sessionFactory) {
060                this.sessionFactory = sessionFactory;
061
062                return this;
063        }
064
065        /**
066         * Validates and builds a {@link org.springframework.batch.item.data.Neo4jItemWriter}.
067         *
068         * @return a {@link Neo4jItemWriter}
069         */
070        public Neo4jItemWriter<T> build() {
071                Assert.notNull(sessionFactory, "sessionFactory is required.");
072                Neo4jItemWriter<T> writer = new Neo4jItemWriter<>();
073                writer.setDelete(this.delete);
074                writer.setSessionFactory(this.sessionFactory);
075                return writer;
076        }
077}