001/* 002 * Copyright 2012-2018 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; 018 019import java.util.List; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.neo4j.ogm.session.Session; 024import org.neo4j.ogm.session.SessionFactory; 025 026import org.springframework.batch.item.ItemWriter; 027import org.springframework.beans.factory.InitializingBean; 028import org.springframework.util.Assert; 029import org.springframework.util.CollectionUtils; 030 031/** 032 * <p> 033 * A {@link ItemWriter} implementation that writes to a Neo4j database. 034 * </p> 035 * 036 * <p> 037 * This writer is thread-safe once all properties are set (normal singleton 038 * behavior) so it can be used in multiple concurrent transactions. 039 * </p> 040 * 041 * @author Michael Minella 042 * @author Glenn Renfro 043 * @author Mahmoud Ben Hassine 044 * 045 */ 046public class Neo4jItemWriter<T> implements ItemWriter<T>, InitializingBean { 047 048 protected static final Log logger = LogFactory 049 .getLog(Neo4jItemWriter.class); 050 051 private boolean delete = false; 052 053 private SessionFactory sessionFactory; 054 055 /** 056 * Boolean flag indicating whether the writer should save or delete the item at write 057 * time. 058 * @param delete true if write should delete item, false if item should be saved. 059 * Default is false. 060 */ 061 public void setDelete(boolean delete) { 062 this.delete = delete; 063 } 064 065 /** 066 * Establish the session factory that will be used to create {@link Session} instances 067 * for interacting with Neo4j. 068 * @param sessionFactory sessionFactory to be used. 069 */ 070 public void setSessionFactory(SessionFactory sessionFactory) { 071 this.sessionFactory = sessionFactory; 072 } 073 074 /** 075 * Checks mandatory properties 076 * 077 * @see InitializingBean#afterPropertiesSet() 078 */ 079 @Override 080 public void afterPropertiesSet() throws Exception { 081 Assert.state(this.sessionFactory != null, 082 "A SessionFactory is required"); 083 } 084 085 /** 086 * Write all items to the data store. 087 * 088 * @see org.springframework.batch.item.ItemWriter#write(java.util.List) 089 */ 090 @Override 091 public void write(List<? extends T> items) throws Exception { 092 if(!CollectionUtils.isEmpty(items)) { 093 doWrite(items); 094 } 095 } 096 097 /** 098 * Performs the actual write using the template. This can be overridden by 099 * a subclass if necessary. 100 * 101 * @param items the list of items to be persisted. 102 */ 103 protected void doWrite(List<? extends T> items) { 104 if(delete) { 105 delete(items); 106 } 107 else { 108 save(items); 109 } 110 } 111 112 private void delete(List<? extends T> items) { 113 Session session = this.sessionFactory.openSession(); 114 115 for(T item : items) { 116 session.delete(item); 117 } 118 } 119 120 private void save(List<? extends T> items) { 121 Session session = this.sessionFactory.openSession(); 122 123 for (T item : items) { 124 session.save(item); 125 } 126 } 127}