8000 [DE-539] Feature #291 CRUD save returning server result (#295) · arangodb/spring-data@dfeb275 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfeb275

Browse files
aburmeisrashtao
andauthored
[DE-539] Feature #291 CRUD save returning server result (#295)
* #291 return fetched instead of original entity * remove some warnings * #291 make the behaviour customizable, defaulting to the old one * fixed tests config * test with java records * test with immutable entities * added returnOriginalEntities configuration --------- Co-authored-by: Michele Rastelli <michele@arangodb.com>
1 parent 60903ae commit dfeb275

13 files changed

+821
-548
lines changed

src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public interface ArangoConfiguration {
6060

6161
String database();
6262

63+
/**
64+
* Configures the behaviour of {@link com.arangodb.springframework.repository.ArangoRepository#save(Object)} and
65+
* {@link com.arangodb.springframework.repository.ArangoRepository#saveAll(Iterable)} to either return the original
66+
* entities (updated where possible) or new ones.
67+
* Set to {@code false} to use immutable entity classes or java records.
68+
*/
69+
default boolean returnOriginalEntities() {
70+
return true;
71+
}
72+
6373
/**
6474
* Override to set the data format to use in {@link #serde()}. It must match the content-type required by the
6575
* protocol used in the driver, e.g. set to {@link ContentType#VPACK} for protocols

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
161161
return collection;
162162
}
163163

164+
@SuppressWarnings("deprecation")
164165
private static void ensureCollectionIndexes(final CollectionOperations collection,
165166
final ArangoPersistentEntity<?> persistentEntity) {
166167
persistentEntity.getPersistentIndexes().forEach(index -> ensurePersistentIndex(collection, index));

src/main/java/com/arangodb/springframework/repository/ArangoRepositoryFactory.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Method;
2424
import java.util.Optional;
2525

26+
import com.arangodb.springframework.config.ArangoConfiguration;
2627
import org.springframework.context.ApplicationContext;
2728
import org.springframework.data.mapping.context.MappingContext;
2829
import org.springframework.data.projection.ProjectionFactory;
@@ -59,13 +60,17 @@ public class ArangoRepositoryFactory extends RepositoryFactorySupport {
5960

6061
private final ArangoOperations arangoOperations;
6162
private final ApplicationContext applicationContext;
63+
private final boolean returnOriginalEntities;
6264
private final MappingContext<? extends ArangoPersistentEntity<?>, ArangoPersistentProperty> context;
6365

64-
public ArangoRepositoryFactory(final ArangoOperations arangoOperations, final ApplicationContext applicationContext) {
65-
this.arangoOperations = arangoOperations;
66-
this.applicationContext = applicationContext;
67-
this.context = arangoOperations.getConverter().getMappingContext();
68-
}
66+
public ArangoRepositoryFactory(final ArangoOperations arangoOperations,
67+
final ApplicationContext applicationContext,
68+
final ArangoConfiguration arangoConfiguration) {
69+
this.arangoOperations = arangoOperations;
70+
this.applicationContext = applicationContext;
71+
this.context = arangoOperations.getConverter().getMappingContext();
72+
returnOriginalEntities = arangoConfiguration.returnOriginalEntities();
73+
}
6974

7075
@SuppressWarnings("unchecked")
7176
@Override
@@ -77,7 +82,7 @@ public <T, ID> ArangoEntityInformation<T, ID> getEntityInformation(final Class<T
7782
@SuppressWarnings({ "rawtypes", "unchecked" })
7883
@Override
7984
protected Object getTargetRepository(final RepositoryInformation metadata) {
80-
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType());
85+
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType(), returnOriginalEntities);
8186
}
8287

8388
@Override

src/main/java/com/arangodb/springframework/repository/ArangoRepositoryFactoryBean.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.arangodb.springframework.repository;
2222

23+
import com.arangodb.springframework.config.ArangoConfiguration;
2324
import org.springframework.beans.BeansException;
2425
import org.springframework.beans.factory.annotation.Autowired;
2526
import org.springframework.context.ApplicationContext;
@@ -39,6 +40,7 @@ public class ArangoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID>
3940

4041
private ArangoOperations arangoOperations;
4142
private ApplicationContext applicationContext;
43+
private ArangoConfiguration arangoConfiguration;
4244

4345
@Autowired
4446
public ArangoRepositoryFactoryBean(final Class<? extends T> repositoryInterface) {
@@ -50,10 +52,15 @@ public void setArangoOperations(final ArangoOperations arangoOperations) {
5052
this.arangoOperations = arangoOperations;
5153
}
5254

55+
@Autowired
56+
public void setArangoConfiguration(final ArangoConfiguration arangoConfiguration) {
57+
this.arangoConfiguration = arangoConfiguration;
58+
}
59+
5360
@Override
5461
protected RepositoryFactorySupport createRepositoryFactory() {
5562
Assert.notNull(arangoOperations, "arangoOperations not configured");
56-
return new ArangoRepositoryFactory(arangoOperations, applicationContext);
63+
return new ArangoRepositoryFactory(arangoOperations, applicationContext, arangoConfiguration);
5764
}
5865

5966
@Override

src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,20 @@ public class SimpleArangoRepository<T, ID> implements ArangoRepository<T, ID> {
5151
private final ArangoMappingContext mappingContext;
5252
private final ArangoExampleConverter exampleConverter;
5353
private final Class<T> domainClass;
54+
private final boolean returnOriginalEntities;
5455

5556
/**
56-
*
57-
* @param arangoOperations The template used to execute much of the
58-
* functionality of this class
59-
* @param domainClass the class type of this repository
57+
* @param arangoOperations The template used to execute much of the
58+
* functionality of this class
59+
* @param domainClass the class type of this repository
60+
* @param returnOriginalEntities whether save and saveAll should return the
61+
* original entities or new ones
6062
*/
61-
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass) {
63+
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass, boolean returnOriginalEntities) {
6264
super();
6365
this.arangoOperations = arangoOperations;
6466
this.domainClass = domainClass;
67+
this.returnOriginalEntities = returnOriginalEntities;
6568
mappingContext = (ArangoMappingContext) arangoOperations.getConverter().getMappingContext();
6669
exampleConverter = new ArangoExampleConverter(mappingContext, arangoOperations.getResolverFactory());
6770
}
@@ -74,8 +77,8 @@ public SimpleArangoRepository(final ArangoOperations arangoOperations, final Cla
7477
*/
7578
@Override
7679
public <S extends T> S save(final S entity) {
77-
arangoOperations.repsert(entity);
78-
return entity;
80+
S saved = arangoOperations.repsert(entity);
81+
return returnOriginalEntities ? entity : saved;
7982
}
8083

8184
/**
@@ -87,8 +90,8 @@ public <S extends T> S save(final S entity) {
8790
*/
8891
@Override
8992
public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
90-
arangoOperations.repsertAll(entities, domainClass);
91-
return entities;
93+
Iterable<S> saved = arangoOperations.repsertAll(entities, domainClass);
94+
return returnOriginalEntities ? entities : saved;
9295
}
9396

9497
/**
@@ -168,13 +171,8 @@ public void deleteById(final ID id) {
168171
*/
169172
@Override
170173
public void delete(final T entity) {
171-
String id = null;
172-
try {
173-
id = (String) arangoOperations.getConverter().getMappingContext().getPersistentEntity(domainClass)
174-
.getIdProperty().getField().get(entity);
175-
} catch (final IllegalAccessException e) {
176-
e.printStackTrace();
177-
}
174+
String id = (String) arangoOperations.getConverter().getMappingContext()
175+
.getRequiredPersistentEntity(domainClass).getIdentifierAccessor(entity).getRequiredIdentifier();
178176
arangoOperations.delete(id, domainClass);
179177
}
180178

@@ -270,8 +268,7 @@ public <S extends T> Optional<S> findOne(final Example<S> example) {
270268
*/
271269
@Override
272270
public <S extends T> Iterable<S> findAll(final Example<S> example) {
273-
final ArangoCursor cursor = findAllInternal((Pageable) null, example, new HashMap<>());
274-
return cursor;
271+
return (ArangoCursor) findAllInternal((Pageable) null, example, new HashMap<>());
275272
}
276273

277274
/**
@@ -285,8 +282,7 @@ public <S extends T> Iterable<S> findAll(final Example<S> example) {
285282
*/
286283
@Override
287284
public <S extends T> Iterable<S> findAll(final Example<S> example, final Sort sort) {
288-
final ArangoCursor cursor = findAllInternal(sort, example, new HashMap());
289-
return cursor;
285+
return findAllInternal(sort, example, new HashMap());
290286
}
291287

292288
/**

src/test/java/com/arangodb/springframework/AbstractArangoTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222

2323
import org.junit.jupiter.api.AfterAll;
2424
import org.junit.jupiter.api.BeforeEach;
25-
import org.junit.jupiter.api.extension.ExtendWith;
2625
import org.springframework.beans.factory.annotation.Autowired;
27-
import org.springframework.test.context.ContextConfiguration;
28-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2926

3027
import com.arangodb.springframework.core.ArangoOperations;
28+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3129

3230
/**
3331
* @author Mark Vollmary
3432
*/
35-
@ExtendWith(SpringExtension.class)
36-
@ContextConfiguration(classes = { ArangoTestConfiguration.class })
33+
@SpringJUnitConfig(ArangoTestConfiguration.class)
3734
public abstract class AbstractArangoTest {
3835

3936
private static volatile ArangoOperations staticTemplate;

src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class ArangoTestConfiguration implements ArangoConfiguration {
5656

5757
public static final String DB = "spring-test-db";
5858

59+
@Value("${returnOriginalEntities:true}")
60+
private boolean returnOriginalEntities;
61+
5962
@Value("${arangodb.protocol:HTTP2_JSON}")
6063
private Protocol protocol;
6164

@@ -86,6 +89,11 @@ public String database() {
8689
return converters;
8790
}
8891

92+
@Override
93+
public boolean returnOriginalEntities() {
94+
return returnOriginalEntities;
95+
}
96+
8997
@Bean
9098
public AuditorAware<Person> auditorProvider() {
9199
return new AuditorProvider();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Mark Vollmary
5151
*
5252
*/
53+
@SuppressWarnings("deprecation")
5354
public class ArangoIndexTest extends AbstractArangoTest {
5455

5556
private IndexType geo1() {

0 commit comments

Comments
 (0)
0