1 | /* |
2 | * Copyright 2012 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 | * http://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 | |
17 | package org.springframework.batch.item.data; |
18 | |
19 | import java.util.List; |
20 | |
21 | import org.apache.commons.logging.Log; |
22 | import org.apache.commons.logging.LogFactory; |
23 | import org.springframework.batch.item.ItemWriter; |
24 | import org.springframework.beans.factory.InitializingBean; |
25 | import org.springframework.data.neo4j.template.Neo4jOperations; |
26 | import org.springframework.util.Assert; |
27 | import org.springframework.util.CollectionUtils; |
28 | |
29 | /** |
30 | * <p> |
31 | * A {@link ItemWriter} implementation that writes to a Neo4j database using an |
32 | * implementation of Spring Data's {@link Neo4jOperations}. |
33 | * </p> |
34 | * |
35 | * <p> |
36 | * This writer is thread safe once all properties are set (normal singleton |
37 | * behavior) so it can be used in multiple concurrent transactions. |
38 | * </p> |
39 | * |
40 | * @author Michael Minella |
41 | * |
42 | */ |
43 | public class Neo4jItemWriter<T> implements ItemWriter<T>, InitializingBean { |
44 | |
45 | protected static final Log logger = LogFactory |
46 | .getLog(Neo4jItemWriter.class); |
47 | |
48 | private boolean delete = false; |
49 | |
50 | private Neo4jOperations template; |
51 | |
52 | public void setDelete(boolean delete) { |
53 | this.delete = delete; |
54 | } |
55 | |
56 | /** |
57 | * Set the {@link Neo4jOperations} to be used to save items |
58 | * |
59 | * @param template the template implementation to be used |
60 | */ |
61 | public void setTemplate(Neo4jOperations template) { |
62 | this.template = template; |
63 | } |
64 | |
65 | /** |
66 | * Checks mandatory properties |
67 | * |
68 | * @see InitializingBean#afterPropertiesSet() |
69 | */ |
70 | public void afterPropertiesSet() throws Exception { |
71 | Assert.state(template != null, "A Neo4JOperations implementation is required"); |
72 | } |
73 | |
74 | /** |
75 | * Write all items to the data store. |
76 | * |
77 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
78 | */ |
79 | public void write(List<? extends T> items) throws Exception { |
80 | if(!CollectionUtils.isEmpty(items)) { |
81 | doWrite(items); |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * Performs the actual write using the template. This can be overriden by |
87 | * a subclass if necessary. |
88 | * |
89 | * @param items the list of items to be persisted. |
90 | */ |
91 | protected void doWrite(List<? extends T> items) { |
92 | if(delete) { |
93 | for (T t : items) { |
94 | template.delete(t); |
95 | } |
96 | } |
97 | else { |
98 | for (T t : items) { |
99 | template.save(t); |
100 | } |
101 | } |
102 | } |
103 | } |