1
1
/*
2
- * Copyright 2012 the original author or authors.
2
+ * Copyright 2012-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
package org .springframework .data .auditing ;
17
17
18
18
import java .lang .reflect .Field ;
19
- import java .util .Date ;
19
+ import java .util .Calendar ;
20
20
21
21
import org .joda .time .DateTime ;
22
+ import org .joda .time .LocalDateTime ;
22
23
import org .springframework .core .convert .ConversionService ;
23
24
import org .springframework .core .convert .converter .Converter ;
24
- import org .springframework .core .convert .support .DefaultConversionService ;
25
+ import org .springframework .core .convert .support .ConfigurableConversionService ;
25
26
import org .springframework .data .domain .Auditable ;
26
27
import org .springframework .data .util .ReflectionUtils ;
28
+ import org .springframework .format .support .DefaultFormattingConversionService ;
27
29
import org .springframework .util .Assert ;
30
+ import org .springframework .util .ClassUtils ;
28
31
29
32
/**
30
33
* A factory class to {@link AuditableBeanWrapper} instances.
34
37
*/
35
38
class AuditableBeanWrapperFactory {
36
39
40
+ private static boolean IS_JODA_TIME_PRESENT = ClassUtils .isPresent ("org.joda.time.DateTime" ,
41
+ ReflectionAuditingBeanWrapper .class .getClassLoader ());
42
+
37
43
/**
38
44
* Returns an {@link AuditableBeanWrapper} if the given object is capable of being equipped with auditing information.
39
45
*
@@ -85,8 +91,8 @@ public void setCreatedBy(Object value) {
85
91
* (non-Javadoc)
86
92
* @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(org.joda.time.DateTime)
87
93
*/
88
- public void setCreatedDate (DateTime value ) {
89
- auditable .setCreatedDate (value );
94
+ public void setCreatedDate (Calendar value ) {
95
+ auditable .setCreatedDate (new DateTime ( value ) );
90
96
}
91
97
92
98
/*
@@ -101,8 +107,8 @@ public void setLastModifiedBy(Object value) {
101
107
* (non-Javadoc)
102
108
* @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(org.joda.time.DateTime)
103
109
*/
104
- public void setLastModifiedDate (DateTime value ) {
105
- auditable .setLastModifiedDate (value );
110
+ public void setLastModifiedDate (Calendar value ) {
111
+ auditable .setLastModifiedDate (new DateTime ( value ) );
106
112
}
107
113
}
108
114
@@ -129,9 +135,12 @@ public ReflectionAuditingBeanWrapper(Object target) {
129
135
this .metadata = AnnotationAuditingMetadata .getMetadata (target .getClass ());
130
136
this .target = target ;
131
137
132
- DefaultConversionService conversionService = new DefaultConversionService ();
133
- conversionService .addConverter (DateTimeToLongConverter .INSTANCE );
134
- conversionService .addConverter (DateTimeToDateConverter .INSTANCE );
138
+ ConfigurableConversionService conversionService = new DefaultFormattingConversionService ();
139
+
140
+ if (IS_JODA_TIME_PRESENT ) {
141
+ conversionService .addConverter (CalendarToDateTimeConverter .INSTANCE );
142
+ conversionService .addConverter (CalendarToLocalDateTimeConverter .INSTANCE );
143
+ }
135
144
136
145
this .conversionService = conversionService ;
137
146
}
@@ -146,9 +155,9 @@ public void setCreatedBy(Object value) {
146
155
147
156
/*
148
157
* (non-Javadoc)
149
- * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(org.joda.time.DateTime )
158
+ * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(java.util.Calendar )
150
159
*/
151
- public void setCreatedDate (DateTime value ) {
160
+ public void setCreatedDate (Calendar value ) {
152
161
setDateField (metadata .getCreatedDateField (), value );
153
162
}
154
163
@@ -162,9 +171,9 @@ public void setLastModifiedBy(Object value) {
162
171
163
172
/*
164
173
* (non-Javadoc)
165
- * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(org.joda.time.DateTime )
174
+ * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(java.util.Calendar )
166
175
*/
167
- public void setLastModifiedDate (DateTime value ) {
176
+ public void setLastModifiedDate (Calendar value ) {
168
177
setDateField (metadata .getLastModifiedDateField (), value );
169
178
}
170
179
@@ -187,7 +196,7 @@ private void setField(Field field, Object value) {
187
196
* @param field
188
197
* @param value
189
198
*/
190
- private void setDateField (Field field , DateTime value ) {
199
+ private void setDateField (Field field , Calendar value ) {
191
200
192
201
if (field == null ) {
193
202
return ;
@@ -203,19 +212,19 @@ private void setDateField(Field field, DateTime value) {
203
212
* @param field must not be {@literal null}.
204
213
* @return
205
214
*/
206
- private Object getDateValueToSet (DateTime value , Field field ) {
215
+ private Object getDateValueToSet (Calendar value , Field field ) {
207
216
208
217
if (value == null ) {
209
218
return null ;
210
219
}
211
220
212
221
Class <?> targetType = field .getType ();
213
222
214
- if (DateTime .class .equals (targetType )) {
223
+ if (Calendar .class .equals (targetType )) {
215
224
return value ;
216
225
}
217
226
218
- if (conversionService .canConvert (DateTime .class , targetType )) {
227
+ if (conversionService .canConvert (Calendar .class , targetType )) {
219
228
return conversionService .convert (value , targetType );
220
229
}
221
230
@@ -224,23 +233,23 @@ private Object getDateValueToSet(DateTime value, Field field) {
224
233
}
225
234
}
226
235
227
- static enum DateTimeToLongConverter implements Converter <DateTime , Long > {
236
+ static enum CalendarToDateTimeConverter implements Converter <Calendar , DateTime > {
228
237
229
238
INSTANCE ;
230
239
231
240
@ Override
232
- public Long convert (DateTime source ) {
233
- return source . getMillis ( );
241
+ public DateTime convert (Calendar source ) {
242
+ return new DateTime ( source );
234
243
}
235
244
}
236
245
237
- static enum DateTimeToDateConverter implements Converter <DateTime , Date > {
246
+ static enum CalendarToLocalDateTimeConverter implements Converter <Calendar , LocalDateTime > {
238
247
239
248
INSTANCE ;
240
249
241
250
@ Override
242
- public Date convert (DateTime source ) {
243
- return source . toDate ( );
251
+ public LocalDateTime convert (Calendar source ) {
252
+ return new LocalDateTime ( source );
244
253
}
245
254
}
246
255
}
0 commit comments