Skip to content

Commit 58b2bde

Browse files
committed
DATACMNS-867 - Removed Optionals from Auditing methods signatures.
1 parent ba87518 commit 58b2bde

File tree

9 files changed

+103
-108
lines changed

9 files changed

+103
-108
lines changed

src/main/java/org/springframework/data/auditing/AuditableBeanWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ public interface AuditableBeanWrapper {
3131
*
3232
* @param value
3333
*/
34-
Optional<? extends Object> setCreatedBy(Optional<? extends Object> value);
34+
Object setCreatedBy(Object value);
3535

3636
/**
3737
* Set the date the object was created.
3838
*
3939
* @param value
4040
*/
41-
Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> value);
41+
TemporalAccessor setCreatedDate(TemporalAccessor value);
4242

4343
/**
4444
* Set the last modifier of the object.
4545
*
4646
* @param value
4747
*/
48-
Optional<? extends Object> setLastModifiedBy(Optional<? extends Object> value);
48+
Object setLastModifiedBy(Object value);
4949

5050
/**
5151
* Returns the date of the last modification date of the backing bean.
@@ -60,5 +60,5 @@ public interface AuditableBeanWrapper {
6060
*
6161
* @param value
6262
*/
63-
Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor> value);
63+
TemporalAccessor setLastModifiedDate(TemporalAccessor value);
6464
}

src/main/java/org/springframework/data/auditing/AuditingHandler.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ private void touch(Object target, boolean isNew) {
157157
Optional<Object> auditor = touchAuditor(it, isNew);
158158
Optional<TemporalAccessor> now = dateTimeForNow ? touchDate(it, isNew) : Optional.empty();
159159

160-
Object defaultedNow = now.map(Object::toString).orElse("not set");
161-
Object defaultedAuditor = auditor.map(Object::toString).orElse("unknown");
160+
if (LOGGER.isDebugEnabled()) {
162161

163-
LOGGER.debug("Touched {} - Last modification at {} by {}",
164-
new Object[] { target, defaultedNow, defaultedAuditor });
162+
Object defaultedNow = now.map(Object::toString).orElse("not set");
163+
Object defaultedAuditor = auditor.map(Object::toString).orElse("unknown");
164+
165+
LOGGER.debug("Touched {} - Last modification at {} by {}",
166+
new Object[] { target, defaultedNow, defaultedAuditor });
167+
}
165168
});
166169
}
167170

@@ -177,14 +180,10 @@ private Optional<Object> touchAuditor(AuditableBeanWrapper wrapper, boolean isNe
177180

178181
Optional<?> auditor = it.getCurrentAuditor();
179182

180-
if (isNew) {
181-
wrapper.setCreatedBy(auditor);
182-
if (!modifyOnCreation) {
183-
return auditor;
184-
}
185-
}
183+
auditor.filter(__ -> isNew).ifPresent(foo -> wrapper.setCreatedBy(foo));
184+
auditor.filter(__ -> !isNew || modifyOnCreation).ifPresent(foo -> wrapper.setLastModifiedBy(foo));
186185

187-
return wrapper.setLastModifiedBy(auditor);
186+
return auditor;
188187
});
189188

190189
}
@@ -199,14 +198,10 @@ private Optional<TemporalAccessor> touchDate(AuditableBeanWrapper wrapper, boole
199198

200199
Optional<TemporalAccessor> now = dateTimeProvider.getNow();
201200

202-
if (isNew) {
203-
wrapper.setCreatedDate(now);
204-
if (!modifyOnCreation) {
205-
return now;
206-
}
207-
}
201+
now.filter(__ -> isNew).ifPresent(it -> wrapper.setCreatedDate(it));
202+
now.filter(__ -> !isNew || modifyOnCreation).ifPresent(it -> wrapper.setLastModifiedDate(it));
208203

209-
return wrapper.setLastModifiedDate(now);
204+
return now;
210205
}
211206

212207
/*

src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ public AuditableInterfaceBeanWrapper(Auditable<Object, ?, TemporalAccessor> audi
9494
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedBy(java.util.Optional)
9595
*/
9696
@Override
97-
public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value) {
97+
public Object setCreatedBy(Object value) {
9898

9999
auditable.setCreatedBy(value);
100-
101100
return value;
102101
}
103102

@@ -106,9 +105,10 @@ public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value)
106105
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(java.util.Optional)
107106
*/
108107
@Override
109-
public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> value) {
108+
public TemporalAccessor setCreatedDate(TemporalAccessor value) {
110109

111-
auditable.setCreatedDate(getAsTemporalAccessor(value, type));
110+
auditable.setCreatedDate(
111+
getAsTemporalAccessor(Optional.of(value), type).orElseThrow(() -> new IllegalStateException()));
112112

113113
return value;
114114
}
@@ -118,7 +118,8 @@ public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> valu
118118
* @see org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory.AuditableInterfaceBeanWrapper#setLastModifiedBy(java.util.Optional)
119119
*/
120120
@Override
121-
public Optional<? extends Object> setLastModifiedBy(Optional<? extends Object> value) {
121+
public Object setLastModifiedBy(Object value) {
122+
122123
auditable.setLastModifiedBy(value);
123124

124125
return value;
@@ -138,9 +139,10 @@ public Optional<TemporalAccessor> getLastModifiedDate() {
138139
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(java.util.Optional)
139140
*/
140141
@Override
141-
public Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor> value) {
142+
public TemporalAccessor setLastModifiedDate(TemporalAccessor value) {
142143

143-
auditable.setLastModifiedDate(getAsTemporalAccessor(value, type));
144+
auditable.setLastModifiedDate(
145+
getAsTemporalAccessor(Optional.of(value), type).orElseThrow(() -> new IllegalStateException()));
144146

145147
return value;
146148
}
@@ -179,33 +181,30 @@ public DateConvertingAuditableBeanWrapper() {
179181
* @param source must not be {@literal null}.
180182
* @return
181183
*/
182-
protected Optional<Object> getDateValueToSet(Optional<TemporalAccessor> value, Class<?> targetType, Object source) {
183-
184-
return value.map(it -> {
184+
protected Object getDateValueToSet(TemporalAccessor value, Class<?> targetType, Object source) {
185185

186-
if (TemporalAccessor.class.equals(targetType)) {
187-
return it;
188-
}
189-
190-
if (conversionService.canConvert(it.getClass(), targetType)) {
191-
return conversionService.convert(it, targetType);
192-
}
186+
if (TemporalAccessor.class.equals(targetType)) {
187+
return value;
188+
}
193189

194-
if (conversionService.canConvert(Date.class, targetType)) {
190+
if (conversionService.canConvert(value.getClass(), targetType)) {
191+
return conversionService.convert(value, targetType);
192+
}
195193

196-
if (!conversionService.canConvert(it.getClass(), Date.class)) {
197-
throw new IllegalArgumentException(
198-
String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.", source,
199-
it.getClass(), targetType));
200-
}
194+
if (conversionService.canConvert(Date.class, targetType)) {
201195

202-
Date date = conversionService.convert(it, Date.class);
203-
return conversionService.convert(date, targetType);
196+
if (!conversionService.canConvert(value.getClass(), Date.class)) {
197+
throw new IllegalArgumentException(
198+
String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.", source,
199+
value.getClass(), targetType));
204200
}
205201

206-
throw new IllegalArgumentException(String.format("Invalid date type for member %s! Supported types are %s.",
207-
source, AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES));
208-
});
202+
Date date = conversionService.convert(value, Date.class);
203+
return conversionService.convert(date, targetType);
204+
}
205+
206+
throw new IllegalArgumentException(String.format("Invalid date type for member %s! Supported types are %s.",
207+
source, AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES));
209208
}
210209

211210
/**
@@ -215,8 +214,8 @@ protected Optional<Object> getDateValueToSet(Optional<TemporalAccessor> value, C
215214
* @return
216215
*/
217216
@SuppressWarnings("unchecked")
218-
protected <T> Optional<T> getAsTemporalAccessor(Optional<?> source, Class<T> target) {
219-
217+
protected <T extends TemporalAccessor> Optional<T> getAsTemporalAccessor(Optional<? extends Object> source,
218+
Class<? extends T> target) {
220219
return source.map(it -> target.isInstance(it) ? (T) it : conversionService.convert(it, target));
221220
}
222221
}
@@ -249,7 +248,7 @@ public ReflectionAuditingBeanWrapper(Object target) {
249248
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedBy(java.util.Optional)
250249
*/
251250
@Override
252-
public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value) {
251+
public Object setCreatedBy(Object value) {
253252
return setField(metadata.getCreatedByField(), value);
254253
}
255254

@@ -258,7 +257,8 @@ public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value)
258257
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(java.util.Optional)
259258
*/
260259
@Override
261-
public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> value) {
260+
public TemporalAccessor setCreatedDate(TemporalAccessor value) {
261+
262262
return setDateField(metadata.getCreatedDateField(), value);
263263
}
264264

@@ -267,7 +267,7 @@ public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> valu
267267
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedBy(java.util.Optional)
268268
*/
269269
@Override
270-
public Optional<? extends Object> setLastModifiedBy(Optional<? extends Object> value) {
270+
public Object setLastModifiedBy(Object value) {
271271
return setField(metadata.getLastModifiedByField(), value);
272272
}
273273

@@ -291,7 +291,7 @@ public Optional<TemporalAccessor> getLastModifiedDate() {
291291
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(java.util.Optional)
292292
*/
293293
@Override
294-
public Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor> value) {
294+
public TemporalAccessor setLastModifiedDate(TemporalAccessor value) {
295295
return setDateField(metadata.getLastModifiedDateField(), value);
296296
}
297297

@@ -301,10 +301,9 @@ public Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor>
301301
* @param field
302302
* @param value
303303
*/
304-
private Optional<? extends Object> setField(Optional<Field> field, Optional<? extends Object> value) {
304+
private <T> T setField(Optional<Field> field, T value) {
305305

306-
field.ifPresent(it -> ReflectionUtils.setField(it, target,
307-
Optional.class.isAssignableFrom(it.getType()) ? value : value.orElse(null)));
306+
field.ifPresent(it -> ReflectionUtils.setField(it, target, value));
308307

309308
return value;
310309
}
@@ -315,13 +314,9 @@ private Optional<? extends Object> setField(Optional<Field> field, Optional<? ex
315314
* @param field
316315
* @param value
317316
*/
318-
private Optional<TemporalAccessor> setDateField(Optional<Field> field, Optional<TemporalAccessor> value) {
317+
private TemporalAccessor setDateField(Optional<Field> field, TemporalAccessor value) {
319318

320-
field.ifPresent(it -> {
321-
Optional<Object> toSet = getDateValueToSet(value, it.getType(), it);
322-
ReflectionUtils.setField(it, target,
323-
Optional.class.isAssignableFrom(it.getType()) ? toSet : toSet.orElse(null));
324-
});
319+
field.ifPresent(it -> ReflectionUtils.setField(it, target, getDateValueToSet(value, it.getType(), it)));
325320

326321
return value;
327322
}

src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.auditing;
1717

18+
import java.time.LocalDateTime;
1819
import java.time.temporal.TemporalAccessor;
1920
import java.util.HashMap;
2021
import java.util.Map;
@@ -158,9 +159,9 @@ public MappingMetadataAuditableBeanWrapper(PersistentPropertyAccessor accessor,
158159
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedBy(java.util.Optional)
159160
*/
160161
@Override
161-
public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value) {
162+
public Object setCreatedBy(Object value) {
162163

163-
metadata.createdByProperty.ifPresent(it -> this.accessor.setProperty(it, value));
164+
metadata.createdByProperty.ifPresent(it -> this.accessor.setProperty(it, Optional.of(value)));
164165

165166
return value;
166167
}
@@ -170,24 +171,17 @@ public Optional<? extends Object> setCreatedBy(Optional<? extends Object> value)
170171
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(java.util.Optional)
171172
*/
172173
@Override
173-
public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> value) {
174-
175-
metadata.createdDateProperty
176-
.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
177-
178-
return value;
174+
public TemporalAccessor setCreatedDate(TemporalAccessor value) {
175+
return setDateProperty(metadata.createdDateProperty, value);
179176
}
180177

181178
/*
182179
* (non-Javadoc)
183180
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedBy(java.util.Optional)
184181
*/
185182
@Override
186-
public Optional<? extends Object> setLastModifiedBy(Optional<? extends Object> value) {
187-
188-
metadata.lastModifiedByProperty.ifPresent(it -> this.accessor.setProperty(it, value));
189-
190-
return value;
183+
public Object setLastModifiedBy(Object value) {
184+
return setProperty(metadata.lastModifiedByProperty, value);
191185
}
192186

193187
/*
@@ -196,19 +190,31 @@ public Optional<? extends Object> setLastModifiedBy(Optional<? extends Object> v
196190
*/
197191
@Override
198192
public Optional<TemporalAccessor> getLastModifiedDate() {
199-
return getAsTemporalAccessor(metadata.lastModifiedDateProperty.map(accessor::getProperty),
200-
TemporalAccessor.class);
193+
return getAsTemporalAccessor(metadata.lastModifiedDateProperty.flatMap(accessor::getProperty),
194+
LocalDateTime.class);
201195
}
202196

203197
/*
204198
* (non-Javadoc)
205199
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(java.util.Optional)
206200
*/
207201
@Override
208-
public Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor> value) {
202+
public TemporalAccessor setLastModifiedDate(TemporalAccessor value) {
203+
return setDateProperty(metadata.lastModifiedDateProperty, value);
204+
}
205+
206+
private <T, P extends PersistentProperty<?>> T setProperty(Optional<P> property, T value) {
207+
208+
property.ifPresent(it -> this.accessor.setProperty(it, Optional.of(value)));
209+
210+
return value;
211+
}
212+
213+
private <P extends PersistentProperty<?>> TemporalAccessor setDateProperty(Optional<P> property,
214+
TemporalAccessor value) {
209215

210-
metadata.lastModifiedDateProperty
211-
.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
216+
property.ifPresent(
217+
it -> this.accessor.setProperty(it, Optional.of(getDateValueToSet(value, it.getType(), accessor.getBean()))));
212218

213219
return value;
214220
}

src/main/java/org/springframework/data/domain/Auditable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface Auditable<U, ID extends Serializable, T extends TemporalAccesso
4141
*
4242
* @param createdBy the creating entity to set
4343
*/
44-
void setCreatedBy(Optional<? extends U> createdBy);
44+
void setCreatedBy(U createdBy);
4545

4646
/**
4747
* Returns the creation date of the entity.
@@ -55,7 +55,7 @@ public interface Auditable<U, ID extends Serializable, T extends TemporalAccesso
5555
*
5656
* @param creationDate the creation date to set
5757
*/
58-
void setCreatedDate(Optional<? extends T> creationDate);
58+
void setCreatedDate(T creationDate);
5959

6060
/**
6161
* Returns the user who modified the entity lastly.
@@ -69,7 +69,7 @@ public interface Auditable<U, ID extends Serializable, T extends TemporalAccesso
6969
*
7070
* @param lastModifiedBy the last modifying entity to set
7171
*/
72-
void setLastModifiedBy(Optional<? extends U> lastModifiedBy);
72+
void setLastModifiedBy(U lastModifiedBy);
7373

7474
/**
7575
* Returns the date of the last modification.
@@ -83,5 +83,5 @@ public interface Auditable<U, ID extends Serializable, T extends TemporalAccesso
8383
*
8484
* @param lastModifiedDate the date of the last modification to set
8585
*/
86-
void setLastModifiedDate(Optional<? extends T> lastModifiedDate);
86+
void setLastModifiedDate(T lastModifiedDate);
8787
}

0 commit comments

Comments
 (0)