8000 [DE-539] Feature #291 CRUD save returning server result by aburmeis · Pull Request #295 · arangodb/spring-data · GitHub
[go: up one dir, main page]

Skip to content

[DE-539] Feature #291 CRUD save returning server result #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public interface ArangoConfiguration {

String database();

/**
* Configures the behaviour of {@link com.arangodb.springframework.repository.ArangoRepository#save(Object)} and
* {@link com.arangodb.springframework.repository.ArangoRepository#saveAll(Iterable)} to either return the original
* entities (updated where possible) or new ones.
* Set to {@code false} to use immutable entity classes or java records.
*/
default boolean returnOriginalEntities() {
return true;
}

/**
* Override to set the data format to use in {@link #serde()}. It must match the content-type required by the
* protocol used in the driver, e.g. set to {@link ContentType#VPACK} for protocols
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
return collection;
}

@SuppressWarnings("deprecation")
private static void ensureCollectionIndexes(final CollectionOperations collection,
final ArangoPersistentEntity<?> persistentEntity) {
persistentEntity.getPersistentIndexes().forEach(index -> ensurePersistentIndex(collection, index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.reflect.Method;
import java.util.Optional;

import com.arangodb.springframework.config.ArangoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
Expand Down Expand Up @@ -59,13 +60,17 @@ public class ArangoRepositoryFactory extends RepositoryFactorySupport {

private final ArangoOperations arangoOperations;
private final ApplicationContext applicationContext;
private final boolean returnOriginalEntities;
private final MappingContext<? extends ArangoPersistentEntity<?>, ArangoPersistentProperty> context;

public ArangoRepositoryFactory(final ArangoOperations arangoOperations, final ApplicationContext applicationContext) {
this.arangoOperations = arangoOperations;
this.applicationContext = applicationContext;
this.context = arangoOperations.getConverter().getMappingContext();
}
public ArangoRepositoryFactory(final ArangoOperations arangoOperations,
final ApplicationContext applicationContext,
final ArangoConfiguration arangoConfiguration) {
this.arangoOperations = arangoOperations;
this.applicationContext = applicationContext;
this.context = arangoOperations.getConverter().getMappingContext();
returnOriginalEntities = arangoConfiguration.returnOriginalEntities();
}

@SuppressWarnings("unchecked")
@Override
Expand All @@ -77,7 +82,7 @@ public <T, ID> ArangoEntityInformation<T, ID> getEntityInformation(final Class<T
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected Object getTargetRepository(final RepositoryInformation metadata) {
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType());
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType(), returnOriginalEntities);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.arangodb.springframework.repository;

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

private ArangoOperations arangoOperations;
private ApplicationContext applicationContext;
private ArangoConfiguration arangoConfiguration;

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

@Autowired
public void setArangoConfiguration(final ArangoConfiguration arangoConfiguration) {
this.arangoConfiguration = arangoConfiguration;
}

@Override
protected RepositoryFactorySupport createRepositoryFactory() {
Assert.notNull(arangoOperations, "arangoOperations not configured");
return new ArangoRepositoryFactory(arangoOperations, applicationContext);
return new ArangoRepositoryFactory(arangoOperations, applicationContext, arangoConfiguration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ public class SimpleArangoRepository<T, ID> implements ArangoRepository<T, ID> {
private final ArangoMappingContext mappingContext;
private final ArangoExampleConverter exampleConverter;
private final Class<T> domainClass;
private final boolean returnOriginalEntities;

/**
*
* @param arangoOperations The template used to execute much of the
* functionality of this class
* @param domainClass the class type of this repository
* @param arangoOperations The template used to execute much of the
* functionality of this class
* @param domainClass the class type of this repository
* @param returnOriginalEntities whether save and saveAll should return the
* original entities or new ones
*/
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass) {
public SimpleArangoRepository(final ArangoOperations arangoOperations, final Class<T> domainClass, boolean returnOriginalEntities) {
super();
this.arangoOperations = arangoOperations;
this.domainClass = domainClass;
this.returnOriginalEntities = returnOriginalEntities;
mappingContext = (ArangoMappingContext) arangoOperations.getConverter().getMappingContext();
exampleConverter = new ArangoExampleConverter(mappingContext, arangoOperations.getResolverFactory());
}
Expand All @@ -74,8 +77,8 @@ public SimpleArangoRepository(final ArangoOperations arangoOperations, final Cla
*/
@Override
public <S extends T> S save(final S entity) {
arangoOperations.repsert(entity);
return entity;
S saved = arangoOperations.repsert(entity);
return returnOriginalEntities ? entity : saved;
}

/**
Expand All @@ -87,8 +90,8 @@ public <S extends T> S save(final S entity) {
*/
@Override
public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
arangoOperations.repsertAll(entities, domainClass);
return entities;
Iterable<S> saved = arangoOperations.repsertAll(entities, domainClass);
return returnOriginalEntities ? entities : saved;
}

/**
Expand Down Expand Up @@ -168,13 +171,8 @@ public void deleteById(final ID id) {
*/
@Override
public void delete(final T entity) {
String id = null;
try {
id = (String) arangoOperations.getConverter().getMappingContext().getPersistentEntity(domainClass)
.getIdProperty().getField().get(entity);
} catch (final IllegalAccessException e) {
e.printStackTrace();
}
String id = (String) arangoOperations.getConverter().getMappingContext()
.getRequiredPersistentEntity(domainClass).getIdentifierAccessor(entity).getRequiredIdentifier();
arangoOperations.delete(id, domainClass);
}

Expand Down Expand Up @@ -270,8 +268,7 @@ public <S extends T> Optional<S> findOne(final Example<S> example) {
*/
@Override
public <S extends T> Iterable<S> findAll(final Example<S> example) {
final ArangoCursor cursor = findAllInternal((Pageable) null, example, new HashMap<>());
return cursor;
return (ArangoCursor) findAllInternal((Pageable) null, example, new HashMap<>());
}

/**
Expand All @@ -285,8 +282,7 @@ public <S extends T> Iterable<S> findAll(final Example<S> example) {
*/
@Override
public <S extends T> Iterable<S> findAll(final Example<S> example, final Sort sort) {
final ArangoCursor cursor = findAllInternal(sort, example, new HashMap());
return cursor;
return findAllInternal(sort, example, new HashMap());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.arangodb.springframework.core.ArangoOperations;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* @author Mark Vollmary
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { ArangoTestConfiguration.class })
@SpringJUnitConfig(ArangoTestConfiguration.class)
public abstract class AbstractArangoTest {

private static volatile ArangoOperations staticTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class ArangoTestConfiguration implements ArangoConfiguration {

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

@Value("${returnOriginalEntities:true}")
private boolean returnOriginalEntities;

@Value("${arangodb.protocol:HTTP2_JSON}")
private Protocol protocol;

Expand Down Expand Up @@ -86,6 +89,11 @@ public String database() {
return converters;
}

@Override
public boolean returnOriginalEntities() {
return returnOriginalEntities;
}

@Bean
public AuditorAware<Person> auditorProvider() {
return new AuditorProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Mark Vollmary
*
*/
@SuppressWarnings("deprecation")
public class ArangoIndexTest extends AbstractArangoTest {

private IndexType geo1() {
Expand Down
Loading
0