BAEL-790 Using JaVers for Data Model Auditing in Spring Data
This commit is contained in:
parent
39a2838ba0
commit
8f8679d846
|
@ -6,44 +6,42 @@
|
|||
<packaging>war</packaging>
|
||||
<name>spring-boot-data</name>
|
||||
<description>Spring Boot Data Module</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.javers</groupId>
|
||||
<artifactId>javers-spring-boot-starter-sql</artifactId>
|
||||
<version>${javers.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.197</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
|
@ -54,8 +52,8 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot</finalName>
|
||||
|
@ -98,6 +96,14 @@
|
|||
</generateGitPropertiesFilename>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -106,6 +112,22 @@
|
|||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.javers.SpringBootJaVersApplication</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
|
@ -133,14 +155,21 @@
|
|||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.SpringBootDataApplication</start-class>
|
||||
<javers.version>5.6.3</javers.version>
|
||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.javers;
|
||||
|
||||
import com.baeldung.javers.domain.Address;
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.repo.StoreRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootJaVersApplication {
|
||||
@Autowired
|
||||
StoreRepository storeRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootJaVersApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void appReady(ApplicationReadyEvent event) {
|
||||
Store store = new Store("Baeldung store", new Address("Some street", 22222));
|
||||
for (int i = 1; i < 3; i++) {
|
||||
Product product = new Product("Product #" + i, 100 * i);
|
||||
store.addProduct(product);
|
||||
}
|
||||
storeRepository.save(store);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.javers.config;
|
||||
|
||||
import org.javers.spring.auditable.AuthorProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JaversConfiguration {
|
||||
@Bean
|
||||
public AuthorProvider provideJaversAuthor() {
|
||||
return new SimpleAuthorProvider();
|
||||
}
|
||||
|
||||
private static class SimpleAuthorProvider implements AuthorProvider {
|
||||
@Override
|
||||
public String provide() {
|
||||
return "Baeldung Author";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
private String address;
|
||||
private Integer zipCode;
|
||||
|
||||
public Address(String address, Integer zipCode) {
|
||||
this.address = address;
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Integer getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(Integer zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "store_id")
|
||||
private Store store;
|
||||
|
||||
public Product(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Product() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Store getStore() {
|
||||
return store;
|
||||
}
|
||||
|
||||
public void setStore(Store store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public void setNamePrefix(String prefix) {
|
||||
this.name = prefix + this.name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Store {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
@Embedded
|
||||
private Address address;
|
||||
@OneToMany(
|
||||
mappedBy = "store",
|
||||
cascade = CascadeType.ALL,
|
||||
orphanRemoval = true
|
||||
)
|
||||
private List<Product> products = new ArrayList<>();
|
||||
|
||||
public Store(String name, Address address) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Store() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void addProduct(Product product) {
|
||||
product.setStore(this);
|
||||
this.products.add(product);
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return this.products;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.javers.repo;
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import org.javers.spring.annotation.JaversAuditable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ProductRepository extends CrudRepository<Product, Integer> {
|
||||
@Override
|
||||
@JaversAuditable
|
||||
<S extends Product> S save(S s);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.javers.repo;
|
||||
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import org.javers.spring.annotation.JaversSpringDataAuditable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
@JaversSpringDataAuditable
|
||||
public interface StoreRepository extends CrudRepository<Store, Integer> {
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.javers.service;
|
||||
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.repo.ProductRepository;
|
||||
import com.baeldung.javers.repo.StoreRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class StoreService {
|
||||
private final ProductRepository productRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
|
||||
public StoreService(ProductRepository productRepository, StoreRepository storeRepository) {
|
||||
this.productRepository = productRepository;
|
||||
this.storeRepository = storeRepository;
|
||||
}
|
||||
|
||||
public void updateProductPrice(Integer productId, Double price) {
|
||||
Optional<Product> productOpt = productRepository.findById(productId);
|
||||
productOpt.ifPresent(product -> {
|
||||
product.setPrice(price);
|
||||
productRepository.save(product);
|
||||
});
|
||||
}
|
||||
|
||||
public void rebrandStore(int storeId, String updatedName) {
|
||||
Optional<Store> storeOpt = storeRepository.findById(storeId);
|
||||
storeOpt.ifPresent(store -> {
|
||||
store.setName(updatedName);
|
||||
store.getProducts().forEach(product -> {
|
||||
product.setNamePrefix(updatedName);
|
||||
});
|
||||
storeRepository.save(store);
|
||||
});
|
||||
}
|
||||
|
||||
public void createRandomProduct(Integer storeId) {
|
||||
Optional<Store> storeOpt = this.storeRepository.findById(storeId);
|
||||
storeOpt.ifPresent(store -> {
|
||||
Random random = new Random();
|
||||
Product product = new Product("Product#" + random.nextInt(), random.nextDouble() * 100);
|
||||
store.addProduct(product);
|
||||
storeRepository.save(store);
|
||||
});
|
||||
}
|
||||
|
||||
public Store findStoreById(int storeId) {
|
||||
return storeRepository.findById(storeId).get();
|
||||
}
|
||||
|
||||
public Product findProductById(int id) {
|
||||
return this.productRepository.findById(id).get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.javers.web;
|
||||
|
||||
public class RebrandStoreDto {
|
||||
public String name;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.javers.web;
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.service.StoreService;
|
||||
import org.javers.core.Changes;
|
||||
import org.javers.core.Javers;
|
||||
import org.javers.core.metamodel.object.CdoSnapshot;
|
||||
import org.javers.repository.jql.JqlQuery;
|
||||
import org.javers.repository.jql.QueryBuilder;
|
||||
import org.javers.shadow.Shadow;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class StoreController {
|
||||
private final StoreService storeService;
|
||||
private final Javers javers;
|
||||
|
||||
public StoreController(StoreService customerService, Javers javers) {
|
||||
this.storeService = customerService;
|
||||
this.javers = javers;
|
||||
}
|
||||
|
||||
@PostMapping("/stores/{storeId}/products/random")
|
||||
public void createRandomProduct(@PathVariable final Integer storeId) {
|
||||
storeService.createRandomProduct(storeId);
|
||||
}
|
||||
|
||||
@PostMapping("/stores/{storeId}/rebrand")
|
||||
public void rebrandStore(@PathVariable final Integer storeId, @RequestBody RebrandStoreDto rebrandStoreDto) {
|
||||
storeService.rebrandStore(storeId, rebrandStoreDto.name);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/stores/{storeId}/products/{productId}/price", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
public void updateProductPrice(@PathVariable final Integer productId, @PathVariable String storeId, @RequestBody UpdatePriceDto priceDto) {
|
||||
storeService.updateProductPrice(productId, priceDto.price);
|
||||
}
|
||||
|
||||
@GetMapping("/products/{productId}/changes")
|
||||
public String getProductChanges(@PathVariable int productId) {
|
||||
Product product = storeService.findProductById(productId);
|
||||
QueryBuilder jqlQuery = QueryBuilder.byInstance(product);
|
||||
Changes changes = javers.findChanges(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(changes);
|
||||
}
|
||||
|
||||
@GetMapping("/products/snapshots")
|
||||
public String getProductSnapshots() {
|
||||
QueryBuilder jqlQuery = QueryBuilder.byClass(Product.class);
|
||||
List<CdoSnapshot> snapshots = javers.findSnapshots(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(snapshots);
|
||||
}
|
||||
|
||||
@GetMapping("/stores/{storeId}/shadows")
|
||||
public String getStoreShadows(@PathVariable int storeId) {
|
||||
Store store = storeService.findStoreById(storeId);
|
||||
JqlQuery jqlQuery = QueryBuilder.byInstance(store)
|
||||
.withChildValueObjects().build();
|
||||
List<Shadow<Store>> shadows = javers.findShadows(jqlQuery);
|
||||
return javers.getJsonConverter().toJson(shadows.get(0));
|
||||
}
|
||||
|
||||
@GetMapping("/stores/snapshots")
|
||||
public String getStoresSnapshots() {
|
||||
QueryBuilder jqlQuery = QueryBuilder.byClass(Store.class);
|
||||
List<CdoSnapshot> snapshots = javers.findSnapshots(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(snapshots);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.javers.web;
|
||||
|
||||
public class UpdatePriceDto {
|
||||
public double price;
|
||||
}
|
|
@ -1,2 +1,25 @@
|
|||
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
|
||||
spring.jackson.time-zone=Europe/Zagreb
|
||||
spring.h2.console.path=/h2
|
||||
spring.h2.console.enabled=true
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
javers.mappingStyle=FIELD
|
||||
javers.algorithm=SIMPLE
|
||||
javers.commitIdGenerator=synchronized_sequence
|
||||
javers.prettyPrint=true
|
||||
javers.typeSafeValues=false
|
||||
javers.newObjectSnapshot=true
|
||||
javers.packagesToScan=
|
||||
javers.auditableAspectEnabled=true
|
||||
javers.springDataAuditableRepositoryAspectEnabled=true
|
||||
javers.sqlSchema=
|
||||
javers.sqlSchemaManagementEnabled=true
|
||||
javers.prettyPrintDateFormats.localDateTime=dd MMM yyyy, HH:mm:ss
|
||||
javers.prettyPrintDateFormats.zonedDateTime=dd MMM yyyy, HH:mm:ssZ
|
||||
javers.prettyPrintDateFormats.localDate=dd MMM yyyy
|
||||
javers.prettyPrintDateFormats.localTime=HH:mm:ss
|
||||
|
||||
|
|
Loading…
Reference in New Issue