|
44 | 44 | * </p>
|
45 | 45 | *
|
46 | 46 | * @param <T> type of the entity to write
|
47 |
| - * |
48 | 47 | * @author Michael Minella
|
49 | 48 | * @author Glenn Renfro
|
50 | 49 | * @author Mahmoud Ben Hassine
|
51 | 50 | * @author Gerrit Meier
|
52 | 51 | */
|
53 | 52 | public class Neo4jItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
54 | 53 |
|
55 |
| - private boolean delete = false; |
56 |
| - |
57 |
| - private Neo4jTemplate neo4jTemplate; |
58 |
| - private Neo4jMappingContext neo4jMappingContext; |
59 |
| - private Driver neo4jDriver; |
60 |
| - |
61 |
| - /** |
62 |
| - * Boolean flag indicating whether the writer should save or delete the item at write |
63 |
| - * time. |
64 |
| - * @param delete true if write should delete item, false if item should be saved. |
65 |
| - * Default is false. |
66 |
| - */ |
67 |
| - public void setDelete(boolean delete) { |
68 |
| - this.delete = delete; |
69 |
| - } |
70 |
| - |
71 |
| - /** |
72 |
| - * Establish the neo4jTemplate for interacting with Neo4j. |
73 |
| - * @param neo4jTemplate neo4jTemplate to be used. |
74 |
| - */ |
75 |
| - public void setNeo4jTemplate(Neo4jTemplate neo4jTemplate) { |
76 |
| - this.neo4jTemplate = neo4jTemplate; |
77 |
| - } |
78 |
| - |
79 |
| - /** |
80 |
| - * Set the Neo4j driver to be used for the delete operation |
81 |
| - * @param neo4jDriver configured Neo4j driver instance |
82 |
| - */ |
83 |
| - public void setNeo4jDriver(Driver neo4jDriver) { |
84 |
| - this.neo4jDriver = neo4jDriver; |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * Neo4jMappingContext needed for determine the id type of the entity instances. |
89 |
| - * |
90 |
| - * @param neo4jMappingContext initialized mapping context |
91 |
| - */ |
92 |
| - public void setNeo4jMappingContext(Neo4jMappingContext neo4jMappingContext) { |
93 |
| - this.neo4jMappingContext = neo4jMappingContext; |
94 |
| - } |
95 |
| - |
96 |
| - /** |
97 |
| - * Checks mandatory properties |
98 |
| - * |
99 |
| - * @see InitializingBean#afterPropertiesSet() |
100 |
| - */ |
101 |
| - @Override |
102 |
| - public void afterPropertiesSet() { |
103 |
| - Assert.state(this.neo4jTemplate != null, "A Neo4jTemplate is required"); |
104 |
| - Assert.state(this.neo4jMappingContext != null, "A Neo4jMappingContext is required"); |
105 |
| - Assert.state(this.neo4jDriver != null, "A Neo4j driver is required"); |
106 |
| - } |
107 |
| - |
108 |
| - /** |
109 |
| - * Write all items to the data store. |
110 |
| - * |
111 |
| - * @see org.springframework.batch.item.ItemWriter#write(Chunk chunk) |
112 |
| - */ |
113 |
| - @Override |
114 |
| - public void write(@NonNull Chunk<? extends T> chunk) { |
115 |
| - if (!chunk.isEmpty()) { |
116 |
| - doWrite(chunk.getItems()); |
117 |
| - } |
118 |
| - } |
119 |
| - |
120 |
| - /** |
121 |
| - * Performs the actual write using the template. This can be overridden by |
122 |
| - * a subclass if necessary. |
123 |
| - * |
124 |
| - * @param items the list of items to be persisted. |
125 |
| - */ |
126 |
| - protected void doWrite(List<? extends T> items) { |
127 |
| - if(delete) { |
128 |
| - delete(items); |
129 |
| - } |
130 |
| - else { |
131 |
| - save(items); |
132 |
| - } |
133 |
| - } |
134 |
| - |
135 |
| - private void delete(List<? extends T> items) { |
136 |
| - for(T item : items) { |
137 |
| - // Figure out id field individually because different |
138 |
| - // id strategies could have been taken for classes within a |
139 |
| - // business model hierarchy. |
140 |
| - Neo4jPersistentEntity<?> nodeDescription = (Neo4jPersistentEntity<?>) this.neo4jMappingContext.getNodeDescription(item.getClass()); |
141 |
| - Object identifier = nodeDescription.getIdentifierAccessor(item).getRequiredIdentifier(); |
142 |
| - Node named = Cypher.anyNode().named(nodeDescription.getPrimaryLabel()); |
143 |
| - Statement statement = Cypher.match(named) |
144 |
| - .where(nodeDescription.getIdDescription().asIdExpression(nodeDescription.getPrimaryLabel()).eq(Cypher.parameter("id"))) |
145 |
| - .detachDelete(named).build(); |
146 |
| - |
147 |
| - String renderedStatement = Renderer.getDefaultRenderer().render(statement); |
148 |
| - this.neo4jDriver.executableQuery(renderedStatement).withParameters(Map.of("id", identifier)).execute(); |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - private void save(List<? extends T> items) { |
153 |
| - this.neo4jTemplate.saveAll(items); |
154 |
| - } |
| 54 | + private boolean delete = false; |
| 55 | + |
| 56 | + private Neo4jTemplate neo4jTemplate; |
| 57 | + private Neo4jMappingContext neo4jMappingContext; |
| 58 | + private Driver neo4jDriver; |
| 59 | + |
| 60 | + /** |
| 61 | + * Boolean flag indicating whether the writer should save or delete the item at write |
| 62 | + * time. |
| 63 | + * |
| 64 | + * @param delete true if write should delete item, false if item should be saved. |
| 65 | + * Default is false. |
| 66 | + */ |
| 67 | + public void setDelete(boolean delete) { |
| 68 | + this.delete = delete; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Establish the neo4jTemplate for interacting with Neo4j. |
| 73 | + * |
| 74 | + * @param neo4jTemplate neo4jTemplate to be used. |
| 75 | + */ |
| 76 | + public void setNeo4jTemplate(Neo4jTemplate neo4jTemplate) { |
| 77 | + this.neo4jTemplate = neo4jTemplate; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set the Neo4j driver to be used for the delete operation |
| 82 | + * |
| 83 | + * @param neo4jDriver configured Neo4j driver instance |
| 84 | + */ |
| 85 | + public void setNeo4jDriver(Driver neo4jDriver) { |
| 86 | + this.neo4jDriver = neo4jDriver; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Neo4jMappingContext needed for determine the id type of the entity instances. |
| 91 | + * |
| 92 | + * @param neo4jMappingContext initialized mapping context |
| 93 | + */ |
| 94 | + public void setNeo4jMappingContext(Neo4jMappingContext neo4jMappingContext) { |
| 95 | + this.neo4jMappingContext = neo4jMappingContext; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Checks mandatory properties |
| 100 | + * |
| 101 | + * @see InitializingBean#afterPropertiesSet() |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public void afterPropertiesSet() { |
| 105 | + Assert.state(this.neo4jTemplate != null, "A Neo4jTemplate is required"); |
| 106 | + Assert.state(this.neo4jMappingContext != null, "A Neo4jMappingContext is required"); |
| 107 | + Assert.state(this.neo4jDriver != null, "A Neo4j driver is required"); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Write all items to the data store. |
| 112 | + * |
| 113 | + * @see org.springframework.batch.item.ItemWriter#write(Chunk chunk) |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public void write(@NonNull Chunk<? extends T> chunk) { |
| 117 | + if (!chunk.isEmpty()) { |
| 118 | + doWrite(chunk.getItems()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Performs the actual write using the template. This can be overridden by |
| 124 | + * a subclass if necessary. |
| 125 | + * |
| 126 | + * @param items the list of items to be persisted. |
| 127 | + */ |
| 128 | + protected void doWrite(List<? extends T> items) { |
| 129 | + if (delete) { |
| 130 | + delete(items); |
| 131 | + } else { |
| 132 | + save(items); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private void delete(List<? extends T> items) { |
| 137 | + for (T item : items) { |
| 138 | + // Figure out id field individually because different |
| 139 | + // id strategies could have been taken for classes within a |
| 140 | + // business model hierarchy. |
| 141 | + Neo4jPersistentEntity<?> nodeDescription = (Neo4jPersistentEntity<?>) this.neo4jMappingContext.getNodeDescription(item.getClass()); |
| 142 | + Object identifier = nodeDescription.getIdentifierAccessor(item).getRequiredIdentifier(); |
| 143 | + Node named = Cypher.anyNode().named(nodeDescription.getPrimaryLabel()); |
| 144 | + Statement statement = Cypher.match(named) |
| 145 | + .where(nodeDescription.getIdDescription().asIdExpression(nodeDescription.getPrimaryLabel()).eq(Cypher.parameter("id"))) |
| 146 | + .detachDelete(named).build(); |
| 147 | + |
| 148 | + String renderedStatement = Renderer.getDefaultRenderer().render(statement); |
| 149 | + this.neo4jDriver.executableQuery(renderedStatement).withParameters(Map.of("id", identifier)).execute(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void save(List<? extends T> items) { |
| 154 | + this.neo4jTemplate.saveAll(items); |
| 155 | + } |
155 | 156 | }
|
0 commit comments