Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Auditing Support for Spring Data #892

Merged
merged 2 commits into from
Jul 19, 2021
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 @@ -53,7 +53,8 @@ public void run(String... args) throws Exception {
{
System.out.println("refresh index");
repository.deleteAll();
operations.indexOps(Conference.class).refresh();
operations.indexOps(Conference.class).delete();
operations.indexOps(Conference.class).create();
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,45 @@
*/
package com.example.data.elasticsearch;

import static org.springframework.data.elasticsearch.annotations.FieldType.*;

import java.lang.Object;
import java.util.List;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

import java.lang.Object;
import java.time.Instant;
import java.util.List;

import static org.springframework.data.elasticsearch.annotations.FieldType.*;

@Document(indexName = "conference-index")
public class Conference {

private @Id String id;
private @Id
String id;
private String name;
private @Field(type = Date) String date;
private @Field(type = Date)
String date;
private GeoPoint location;
private List<String> keywords;

@CreatedDate
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
@Field(type = FieldType.Date, format= DateFormat.basic_date_time)
Instant modifiedAt;
@LastModifiedBy
String modifiedBy;

// do not remove it
public Conference() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +18,12 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.elasticsearch.config.EnableElasticsearchAuditing;

@SpringBootApplication
@EnableElasticsearchAuditing(auditorAwareRef = "fixedAuditor")
public class ElasticsearchApplication {

public static void main(String[] args) throws Exception {
Expand All @@ -29,4 +33,9 @@ public static void main(String[] args) throws Exception {
app.run();
Thread.currentThread().join(); // To be able to measure memory consumption
}

@Bean
AuditorAware<String> fixedAuditor() {
return () -> java.util.Optional.of("Douglas Adams");
}
}
23 changes: 23 additions & 0 deletions samples/data-jpa/src/main/java/app/main/SampleApplication.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.main;

import app.main.model.Flurb;
Expand All @@ -7,6 +22,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.servlet.function.RouterFunction;

Expand All @@ -17,6 +34,7 @@
import static org.springframework.web.servlet.function.ServerResponse.*;

@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "fixedAuditor")
public class SampleApplication {

private final FooRepository entities;
Expand All @@ -41,6 +59,11 @@ public CommandLineRunner runner() {
};
}

@Bean
AuditorAware<String> fixedAuditor() {
return () -> Optional.of("Douglas Adams");
}

@Bean
public RouterFunction<?> userEndpoints() {
return route(GET("/"), request -> ok().body(findOne()));
Expand Down
32 changes: 29 additions & 3 deletions samples/data-jpa/src/main/java/app/main/model/Foo.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.main.model;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import java.time.Instant;

@Entity
public class Foo {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String value;

@Enumerated
private SomeEnum enumerated;

@OneToOne(cascade = CascadeType.ALL)
private Flurb flurb;
@CreatedDate
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
Instant modifiedAt;
@LastModifiedBy
String modifiedBy;

public Foo() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.domain.ReactiveAuditorAware;
import org.springframework.data.mongodb.config.EnableReactiveMongoAuditing;
import reactor.core.publisher.Mono;

import java.util.Optional;

@SpringBootApplication
@EnableReactiveMongoAuditing(auditorAwareRef = "fixedAuditor")
public class MongoApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(MongoApplication.class);
Thread.currentThread().join(); // To be able to measure memory consumption
}

@Bean
ReactiveAuditorAware<String> fixedAuditor(){
return () -> Mono.just("Douglas Adams");}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package com.example.data.mongo;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
Expand All @@ -37,6 +42,15 @@ public class Order {

private List<LineItem> items;

@CreatedDate
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
Instant modifiedAt;
@LastModifiedBy
String modifiedBy;

protected Order() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,23 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mongodb.config.EnableMongoAuditing;

import java.util.Optional;

@SpringBootApplication
@EnableMongoAuditing(auditorAwareRef = "fixedAuditor")
public class MongoApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(MongoApplication.class);
Thread.currentThread().join(); // To be able to measure memory consumption
}

@Bean
AuditorAware<String> fixedAuditor() {
return () -> Optional.of("Douglas Adams");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,15 @@
*/
package com.example.data.mongo;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
Expand Down Expand Up @@ -47,6 +52,15 @@ public class Order {
@DBRef(lazy = true) // JdkProxy
private PriceReduction reduction;

@CreatedDate
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
Instant modifiedAt;
@LastModifiedBy
String modifiedBy;

protected Order() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,23 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.neo4j.config.EnableNeo4jAuditing;

import java.util.Optional;

@SpringBootApplication
@EnableNeo4jAuditing(auditorAwareRef = "fixedAuditor")
public class Neo4jApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(Neo4jApplication.class, args);
Thread.currentThread().join(); // To be able to measure memory consumption
}

@Bean
AuditorAware<String> fixedAuditor() {
return () -> Optional.of("Douglas Adams");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,15 @@
*/
package com.example.data.neo4j;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
Expand All @@ -41,6 +46,14 @@ public class Order {
@Relationship(value = "HAS")
private List<LineItem> items;

@CreatedDate
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
Instant modifiedAt;
@LastModifiedBy
String modifiedBy;
protected Order() {
}

Expand Down
Loading