21
21
import org .apache .commons .logging .Log ;
22
22
import org .apache .commons .logging .LogFactory ;
23
23
24
- import org .springframework .beans .factory .InitializingBean ;
25
24
import org .springframework .core .log .LogMessage ;
26
25
import org .springframework .data .domain .Auditable ;
27
26
import org .springframework .data .mapping .context .PersistentEntities ;
36
35
* @author Mark Paluch
37
36
* @since 2.4
38
37
*/
39
- public abstract class AuditingHandlerSupport implements InitializingBean {
38
+ public abstract class AuditingHandlerSupport {
40
39
41
40
private static final Log logger = LogFactory .getLog (AuditingHandlerSupport .class );
42
41
43
- private final DefaultAuditableBeanWrapperFactory factory ;
42
+ private final AuditableBeanWrapperFactory factory ;
44
43
45
44
private DateTimeProvider dateTimeProvider = CurrentDateTimeProvider .INSTANCE ;
46
45
private boolean dateTimeForNow = true ;
@@ -83,40 +82,34 @@ public void setModifyOnCreation(boolean modifyOnCreation) {
83
82
/**
84
83
* Sets the {@link DateTimeProvider} to be used to determine the dates to be set.
85
84
*
86
- * @param dateTimeProvider
85
+ * @param dateTimeProvider can be {@literal null}, defaults to {@link CurrentDateTimeProvider} in that case.
87
86
*/
88
87
public void setDateTimeProvider (@ Nullable DateTimeProvider dateTimeProvider ) {
89
88
this .dateTimeProvider = dateTimeProvider == null ? CurrentDateTimeProvider .INSTANCE : dateTimeProvider ;
90
89
}
91
90
92
- /*
93
- * (non-Javadoc)
94
- * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
95
- */
96
- public void afterPropertiesSet () {}
97
-
98
91
/**
99
- * Returns whether the given source is considered to be auditable in the first place
92
+ * Returns whether the given source is considered to be auditable in the first place.
100
93
*
101
94
* @param source must not be {@literal null}.
102
- * @return
95
+ * @return {@literal true} if the given {@literal source} considered to be auditable.
103
96
*/
104
97
protected final boolean isAuditable (Object source ) {
105
98
106
- Assert .notNull (source , "Source must not be null!" );
99
+ Assert .notNull (source , "Source entity must not be null!" );
107
100
108
101
return factory .getBeanWrapperFor (source ).isPresent ();
109
102
}
110
103
111
104
/**
112
105
* Marks the given object as created.
113
106
*
114
- * @param auditor
115
- * @param source
107
+ * @param auditor can be {@literal null}.
108
+ * @param source must not be {@literal null}.
116
109
*/
117
- <T > T markCreated (@ Nullable Object auditor , T source ) {
110
+ <T > T markCreated (Auditor auditor , T source ) {
118
111
119
- Assert .notNull (source , "Entity must not be null!" );
112
+ Assert .notNull (source , "Source entity must not be null!" );
120
113
121
114
return touch (auditor , source , true );
122
115
}
@@ -127,28 +120,26 @@ <T> T markCreated(@Nullable Object auditor, T source) {
127
120
* @param auditor
128
121
* @param source
129
122
*/
130
- <T > T markModified (@ Nullable Object auditor , T source ) {
123
+ <T > T markModified (Auditor auditor , T source ) {
131
124
132
- Assert .notNull (source , "Entity must not be null!" );
125
+ Assert .notNull (source , "Source entity must not be null!" );
133
126
134
127
return touch (auditor , source , false );
135
128
}
136
129
137
- private <T > T touch (@ Nullable Object auditor , T target , boolean isNew ) {
130
+ private <T > T touch (Auditor auditor , T target , boolean isNew ) {
138
131
139
132
Optional <AuditableBeanWrapper <T >> wrapper = factory .getBeanWrapperFor (target );
140
133
141
134
return wrapper .map (it -> {
142
135
143
- if (auditor != null ) {
144
- touchAuditor (auditor , it , isNew );
145
- }
136
+ touchAuditor (auditor , it , isNew );
146
137
Optional <TemporalAccessor > now = dateTimeForNow ? touchDate (it , isNew ) : Optional .empty ();
147
138
148
139
if (logger .isDebugEnabled ()) {
149
140
150
141
Object defaultedNow = now .map (Object ::toString ).orElse ("not set" );
151
- Object defaultedAuditor = auditor != null ? auditor .toString () : "unknown" ;
142
+ Object defaultedAuditor = auditor . isPresent () ? auditor .toString () : "unknown" ;
152
143
153
144
logger .debug (
154
145
LogMessage .format ("Touched %s - Last modification at %s by %s" , target , defaultedNow , defaultedAuditor ));
@@ -166,17 +157,20 @@ private <T> T touch(@Nullable Object auditor, T target, boolean isNew) {
166
157
* @param isNew
167
158
* @return
168
159
*/
169
- private void touchAuditor (Object auditor , AuditableBeanWrapper <?> wrapper , boolean isNew ) {
160
+ private void touchAuditor (Auditor auditor , AuditableBeanWrapper <?> wrapper , boolean isNew ) {
161
+
162
+ if (!auditor .isPresent ()) {
163
+ return ;
164
+ }
170
165
171
166
Assert .notNull (wrapper , "AuditableBeanWrapper must not be null!" );
172
- Assert .notNull (auditor , "Auditor must not be null!" );
173
167
174
168
if (isNew ) {
175
- wrapper .setCreatedBy (auditor );
169
+ wrapper .setCreatedBy (auditor . getValue () );
176
170
}
177
171
178
172
if (!isNew || modifyOnCreation ) {
179
- wrapper .setLastModifiedBy (auditor );
173
+ wrapper .setLastModifiedBy (auditor . getValue () );
180
174
}
181
175
}
182
176
0 commit comments