8000 convert ArangoOperations.update() · arangodb/spring-data@71625ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 71625ad

Browse files
committed
convert ArangoOperations.update()
1 parent c3fcf66 commit 71625ad

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

src/main/java/com/arangodb/springframework/core/ArangoOperations.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ <T> MultiDocumentEntity<DocumentDeleteEntity<T>> deleteAll(
188188
* @return information about the documents
189189
* @throws DataAccessException
190190
*/
191-
<T> MultiDocumentEntity<? extends DocumentEntity> update(
192-
Iterable<T> values,
193-
Class<T> entityClass,
194-
DocumentUpdateOptions options) throws DataAccessException;
191+
<T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
192+
Iterable<T> values,
193+
DocumentUpdateOptions options,
194+
Class<T> entityClass
195+
) throws DataAccessException;
195196

196197
/**
197198
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
@@ -208,8 +209,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> update(
208209
* @return information about the documents
209210
* @throws DataAccessException
210211
*/
211-
<T> MultiDocumentEntity<? extends DocumentEntity> update(Iterable<T> values, Class<T> entityClass)
212-
throws DataAccessException;
212+
<T> MultiDocumentEntity<DocumentUpdateEntity<?>> updateAll(Iterable<T> values, Class<T> entityClass) throws DataAccessException;
213213

214214
/**
215215
* Partially updates the document identified by document id or key. The value must contain a document with the
@@ -225,7 +225,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> update(Iterable<T> values, Cla
225225
* @return information about the document
226226
* @throws DataAccessException
227227
*/
228-
<T> DocumentEntity update(Object id, T value, DocumentUpdateOptions options) throws DataAccessException;
228+
<T> DocumentUpdateEntity<T> update(Object id, T value, DocumentUpdateOptions options) throws DataAccessException;
229229

230230
/**
231231
* Partially updates the document identified by document id or key. The value must contain a document with the
@@ -239,7 +239,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> update(Iterable<T> values, Cla
239239
* @return information about the document
240240
* @throws DataAccessException
241241
*/
242-
<T> DocumentEntity update(Object id, T value) throws DataAccessException;
242+
DocumentUpdateEntity<?> update(Object id, Object value) throws DataAccessException;
243243

244244
/**
245245
* Replaces multiple documents in the specified collection with the ones in the values, the replaced documents are

src/main/java/com/arangodb/springframework/core/template/ArangoTemplate.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,17 @@ public <T> DocumentDeleteEntity<T> delete(final Object id, final Class<T> entity
345345
}
346346

347347
@Override
348-
public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T> values,
349-
final Class<T> entityClass, final DocumentUpdateOptions options) throws DataAccessException {
348+
public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
349+
final Iterable<T> values,
350+
final DocumentUpdateOptions options,
351+
final Class<T> entityClass
352+
) throws DataAccessException {
350353

351354
potentiallyEmitBeforeSaveEvent(values);
352355

353-
final MultiDocumentEntity<? extends DocumentEntity> result;
356+
MultiDocumentEntity<DocumentUpdateEntity<T>> result;
354357
try {
355-
result = _collection(entityClass).updateDocuments(values, options);
358+
result = _collection(entityClass).updateDocuments(values, options, entityClass);
356359
} catch (final ArangoDBException e) {
357360
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
358361
}
@@ -363,18 +366,21 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T
363366
}
364367

365368
@Override
366-
public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T> values,
367-
final Class<T> entityClass) throws DataAccessException {
368-
return update(values, entityClass, new DocumentUpdateOptions());
369+
@SuppressWarnings({"rawtypes", "unchecked"})
370+
public <T> MultiDocumentEntity<DocumentUpdateEntity<?>> updateAll(
371+
final Iterable<T> values,
372+
final Class<T> entityClass
373+
) throws DataAccessException {
374+
return updateAll(values, new DocumentUpdateOptions(), (Class) entityClass);
369375
}
370376

371377
@Override
372-
public DocumentEntity update(final Object id, final Object value, final DocumentUpdateOptions options)
378+
public <T> DocumentUpdateEntity<T> update(final Object id, final T value, final DocumentUpdateOptions options)
373379
throws DataAccessException {
374380

375381
potentiallyEmitEvent(new BeforeSaveEvent<>(value));
376382

377-
final DocumentEntity result;
383+
DocumentUpdateEntity<T> result;
378384
try {
379385
result = _collection(value.getClass(), id).updateDocument(determineDocumentKeyFromId(id), value, options);
380386
} catch (final ArangoDBException e) {
@@ -387,7 +393,7 @@ public DocumentEntity update(final Object id, final Object value, final Document
387393
}
388394

389395
@Override
390-
public DocumentEntity update(final Object id, final Object value) throws DataAccessException {
396+
public DocumentUpdateEntity<?> update(final Object id, final Object value) throws DataAccessException {
391397
return update(id, value, new DocumentUpdateOptions());
392398
}
393399

src/test/java/com/arangodb/springframework/core/mapping/event/SaveEventTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void updateMultiSaveEvent() {
113113
listener.afterSaveEvents.clear();
114114
john.setId("non-existing-id");
115115
bob.setAge(30);
116-
template.update(customers, Customer.class);
116+
template.updateAll(customers, Customer.class);
117117

118118
assertThat(listener.beforeSaveEvents.size(), is(2));
119119
for (final BeforeSaveEvent<Customer> event : listener.beforeSaveEvents) {

src/test/java/com/arangodb/springframework/core/template/ArangoTemplateTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,19 @@ public void updateDocument() {
242242
assertThat(customer.getAge(), is(26));
243243
}
244244

245-
@Test
245+
@Test
246+
public void updateDocumentReturnNew() {
247+
Customer doc = new Customer("John", "Doe", 30);
248+
Customer doc2 = new Customer("Jane", "Doe", 26);
249+
final DocumentEntity res = template.insert(doc);
250+
Customer customer = template.update(res.getId(), doc2, new DocumentUpdateOptions().returnNew(true)).getNew();
251+
assertThat(customer, is(notNullValue()));
252+
assertThat(customer.getName(), is("Jane"));
253+
assertThat(customer.getSurname(), is("Doe"));
254+
assertThat(customer.getAge(), is(26));
255+
}
256+
257+
@Test
246258
public void updateDocumentRevConflict() {
247259
final DocumentEntity res = template.insert(new Customer("John", "Doe", 30));
248260
Customer doc = new Customer("Jane", "Doe", 26);
@@ -261,8 +273,8 @@ public void updateDocuments() {
261273
final Product documentB = template.find(b.getId(), Product.class).get();
262274
documentB.setName("bb");
263275

264-
final MultiDocumentEntity<? extends DocumentEntity> res = template.update(Arrays.asList(documentA, documentB),
265-
Product.class);
276+
final MultiDocumentEntity<? extends DocumentEntity> res = template.updateAll(Arrays.asList(documentA, documentB),
277+
Product.class);
266278
assertThat(res, is(notNullValue()));
267279
assertThat(res.getDocuments().size(), is(2));
268280

@@ -272,6 +284,29 @@ public void updateDocuments() {
272284
assertThat(newB.getName(), is("bb"));
273285
}
274286

287+
@Test
288+
public void updateDocumentsReturnNew() {
289+
final DocumentEntity a = template.insert(new Product("a"));
290+
final DocumentEntity b = template.insert(new Product("b"));
291+
292+
final Product documentA = template.find(a.getId(), Product.class).get();
293+
documentA.setName("aa");
294+
final Product documentB = template.find(b.getId(), Product.class).get();
295+
documentB.setName("bb");
296+
297+
final MultiDocumentEntity<DocumentUpdateEntity<Product>> res = template.updateAll(Arrays.asList(documentA, documentB),
298+
new DocumentUpdateOptions().returnNew(true), Product.class);
299+
assertThat(res, is(notNullValue()));
300+
assertThat(res.getDocuments().size(), is(2));
301+
302+
Iterator<DocumentUpdateEntity<Product>> resIt = res.getDocuments().iterator();
303+
304+
final Product newA = resIt.next().getNew();
305+
assertThat(newA.getName(), is("aa"));
306+
final Product newB = resIt.next().getNew();
307+
assertThat(newB.getName(), is("bb"));
308+
}
309+
275310
@Test
276311
public void deleteDocument() {
277312
final DocumentEntity res = template.insert(new Customer("John", "Doe", 30));

0 commit comments

Comments
 (0)
0