10000 #291 make the bahaviour customizable, defaulting to the old one · aburmeis/spring-data@0b57201 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b57201

Browse files
committed
arangodb#291 make the bahaviour customizable, defaulting to the old one
1 parent 58c2ce8 commit 0b57201

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class ArangoRepositoryFactory extends RepositoryFactorySupport {
5959

6060
private final ArangoOperations arangoOperations;
6161
private final ApplicationContext applicationContext;
62+
private boolean returnOriginalEntities = true;
6263
private final MappingContext<? extends ArangoPersistentEntity<?>, ArangoPersistentProperty> context;
6364

6465
public ArangoRepositoryFactory(final ArangoOperations arangoOperations, final ApplicationContext applicationContext) {
@@ -74,10 +75,21 @@ public <T, ID> ArangoEntityInformation<T, ID> getEntityInformation(final Class<T
7475
(ArangoPersistentEntity<T>) context.getRequiredPersistentEntity(domainClass));
7576
}
7677

78+
/**
79+
* Change the behaviour of {@link org.springframework.data.repository.CrudRepository#save(Object)} and
80+
* {@link org.springframework.data.repository.CrudRepository#saveAll(Iterable)} to either return the original
81+
* entities passed or those returned from the server using proxies for lazy evaluation.
82+
*
83+
* @see org.springframework.data.repository.core.support.RepositoryFactoryCustomizer
84+
*/
85+
public void setReturnOriginalEntities(boolean returnOriginalEntities) {
86+
this.returnOriginalEntities = returnOriginalEntities;
87+
}
88+
7789
@SuppressWarnings({ "rawtypes", "unchecked" })
7890
@Override
7991
protected Object getTargetRepository(final RepositoryInformation metadata) {
80-
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType());
92+
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType(), returnOriginalEntities);
8193
}
8294

8395
@Override

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

Lines changed: 12 additions & 7 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 returnOriginalEnties;
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 returnOriginalEnties whether save and saveAll should return the
61+
* original entities passed or the server ones
6062
*/
61-
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass) {
63+
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass, boolean returnOriginalEnties) {
6264
super();
6365
this.arangoOperations = arangoOperations;
6466
this.domainClass = domainClass;
67+
this.returnOriginalEnties = returnOriginalEnties;
6568
mappingContext = (ArangoMappingContext) arangoOperations.getConverter().getMappingContext();
6669
exampleConverter = new ArangoExampleConverter(mappingContext, arangoOperations.getResolverFactory());
6770
}
@@ -74,7 +77,8 @@ public SimpleArangoRepository(final ArangoOperations arangoOperations, final Cla
7477
*/
7578
@Override
7679
public <S extends T> S save(final S entity) {
77-
return arangoOperations.repsert(entity);
80+
S saved = arangoOperations.repsert(entity);
81+
return returnOriginalEnties ? entity : saved;
7882
}
7983

8084
/**
@@ -86,7 +90,8 @@ public <S extends T> S save(final S entity) {
8690
*/
8791
@Override
8892
public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
89-
return arangoOperations.repsertAll(entities, domainClass);
93+
Iterable<S> saved = arangoOperations.repsertAll(entities, domainClass);
94+
return returnOriginalEnties ? entities : saved;
9095
}
9196

9297
/**

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@
2626
import com.arangodb.springframework.annotation.EnableArangoRepositories;
2727
import com.arangodb.springframework.config.ArangoConfiguration;
2828
import com.arangodb.springframework.core.mapping.CustomMappingTest;
29+
import com.arangodb.springframework.repository.ArangoRepositoryFactory;
2930
import com.arangodb.springframework.testdata.Person;
31+
import org.springframework.beans.BeansException;
32+
import org.springframework.beans.factory.config.BeanPostProcessor;
3033
import org.springframework.context.annotation.Bean;
3134
import org.springframework.context.annotation.ComponentScan;
3235
import org.springframework.context.annotation.Configuration;
3336
import org.springframework.core.convert.converter.Converter;
37+
import org.springframework.core.env.Environment;
3438
import org.springframework.data.domain.AuditorAware;
39+
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
40+
import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer;
3541

3642
import java.util.ArrayList;
3743
import java.util.Collection;
@@ -77,4 +83,21 @@ public String database() {
7783
public AuditorAware<Person> auditorProvider() {
7884
return new AuditorProvider();
7985
}
86+
87+
@Bean
88+
public BeanPostProcessor repositoryFactoryCustomizerRegistrar(Environment env) {
89+
return new BeanPostProcessor() {
90+
@Override
91+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
92+
if (bean instanceof RepositoryFactoryBeanSupport<?, ?, ?> repoFactoryBean) {
93+
repoFactoryBean.addRepositoryFactoryCustomizer(repositoryFactoryCustomizer(env));
94+
}
95+
return bean;
96+
}
97+
};
98+
}
99+
100+
private RepositoryFactoryCustomizer repositoryFactoryCustomizer(Environment env) {
101+
return factory -> ((ArangoRepositoryFactory) factory).setReturnOriginalEntities(env.getProperty("returnOriginalEntities", Boolean.class, true));
102+
}
80103
}

src/test/java/com/arangodb/springframework/repository/ArangoRepositoryTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import com.arangodb.springframework.testdata.Customer;
1515
import com.arangodb.springframework.testdata.ShoppingCart;
1616
import org.springframework.data.util.Streamable;
17+
import org.springframework.test.context.TestPropertySource;
1718

1819
/**
1920
* Created by F625633 on 06/07/2017.
2021
*/
22+
@TestPropertySource(properties = "returnOriginalEntities=false")
2123
public class ArangoRepositoryTest extends AbstractArangoRepositoryTest {
2224

2325
@Test

0 commit comments

Comments
 (0)
0