-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAJPA-9 - Added DateTimeProvider SPI for auditing.
Extracted the DateTime instance calculation to be used for auditing into a DateTimeProvider callback interface to open it up for customization. AuditingEntityListener can get an instance of it configured. Exposed the property via the auditing namespace. By default a CurrentDateTimeProvider its used that contains the behavior we had so far.
- Loading branch information
Showing
9 changed files
with
232 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/springframework/data/jpa/domain/support/CurrentDateTimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2012 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 | ||
* | ||
* http://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 org.springframework.data.jpa.domain.support; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
/** | ||
* Default {@link DateTimeProvider} simply creating new {@link DateTime} instances for each method call. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public enum CurrentDateTimeProvider implements DateTimeProvider { | ||
|
||
INSTANCE; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.jpa.domain.support.DateTimeProvider#getDateTime() | ||
*/ | ||
public DateTime getDateTime() { | ||
return new DateTime(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/org/springframework/data/jpa/domain/support/DateTimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2012 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 | ||
* | ||
* http://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 org.springframework.data.jpa.domain.support; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
/** | ||
* SPI to calculate the {@link DateTime} instance to be used when auditing. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public interface DateTimeProvider { | ||
|
||
/** | ||
* Returns the {@link DateTime} to be used as modification date. | ||
* | ||
* @return | ||
*/ | ||
DateTime getDateTime(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.0.xsd | ||
http\://www.springframework.org/schema/data/jpa/spring-jpa.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.0.xsd | ||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.1.xsd | ||
http\://www.springframework.org/schema/data/jpa/spring-jpa.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.1.xsd |
75 changes: 75 additions & 0 deletions
75
src/main/resources/org/springframework/data/jpa/repository/config/spring-jpa-1.1.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xsd:schema xmlns="http://www.springframework.org/schema/data/jpa" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
xmlns:tool="http://www.springframework.org/schema/tool" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xmlns:repository="http://www.springframework.org/schema/data/repository" | ||
targetNamespace="http://www.springframework.org/schema/data/jpa" | ||
elementFormDefault="qualified" attributeFormDefault="unqualified"> | ||
|
||
<xsd:import namespace="http://www.springframework.org/schema/tool" /> | ||
<xsd:import namespace="http://www.springframework.org/schema/context" | ||
schemaLocation="http://www.springframework.org/schema/context/spring-context.xsd" /> | ||
<xsd:import namespace="http://www.springframework.org/schema/data/repository" | ||
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd" /> | ||
|
||
<xsd:complexType name="jpa-repository"> | ||
<xsd:complexContent> | ||
<xsd:extension base="repository:repository"> | ||
<xsd:attributeGroup ref="repository:transactional-repository-attributes" /> | ||
<xsd:attribute name="entity-manager-factory-ref" type="entityManagerFactoryRef" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
|
||
<xsd:element name="repositories"> | ||
<xsd:complexType> | ||
<xsd:complexContent> | ||
<xsd:extension base="repository:repositories"> | ||
<xsd:sequence> | ||
<xsd:element name="repository" minOccurs="0" maxOccurs="unbounded" type="jpa-repository" /> | ||
</xsd:sequence> | ||
<xsd:attributeGroup ref="repository:transactional-repository-attributes" /> | ||
<xsd:attribute name="entity-manager-factory-ref" type="entityManagerFactoryRef" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
</xsd:element> | ||
|
||
<xsd:element name="auditing"> | ||
<xsd:complexType> | ||
<xsd:attribute name="auditor-aware-ref"> | ||
<xsd:annotation> | ||
<xsd:appinfo> | ||
<tool:annotation kind="ref"> | ||
<tool:assignable-to type="org.springframework.data.domain.AuditorAware" /> | ||
</tool:annotation> | ||
</xsd:appinfo> | ||
</xsd:annotation> | ||
</xsd:attribute> | ||
<xsd:attribute name="set-dates" default="true" type="xsd:boolean" /> | ||
<xsd:attribute name="date-time-provider-ref"> | ||
<xsd:annotation> | ||
<xsd:appinfo> | ||
<tool:annotation kind="ref"> | ||
<tool:assignable-to type="org.springframework.data.jpa.domain.support.DateTimeProvider" /> | ||
</tool:annotation> | ||
</xsd:appinfo> | ||
</xsd:annotation> | ||
</xsd:attribute> | ||
</xsd:complexType> | ||
</xsd:element> | ||
|
||
<xsd:simpleType name="entityManagerFactoryRef"> | ||
<xsd:annotation> | ||
<xsd:appinfo> | ||
<tool:annotation kind="ref"> | ||
<tool:assignable-to type="org.springframework.orm.jpa.AbstractEntityManagerFactoryBean" /> | ||
</tool:annotation> | ||
</xsd:appinfo> | ||
</xsd:annotation> | ||
<xsd:union memberTypes="xsd:string" /> | ||
</xsd:simpleType> | ||
|
||
|
||
</xsd:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/resources/auditing/auditing-namespace-context3.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:jpa="http://www.springframework.org/schema/data/jpa" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> | ||
|
||
<jpa:auditing set-dates="false" date-time-provider-ref="dateTimeProvider" /> | ||
|
||
<bean id="dateTimeProvider" class="org.mockito.Mockito" factory-method="mock"> | ||
<constructor-arg value="org.springframework.data.jpa.domain.support.DateTimeProvider" /> | ||
</bean> | ||
|
||
</beans> |