|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2014 the original author or authors. |
| 2 | + * Copyright 2015 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.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.data.auditing;
|
17 | 17 |
|
18 |
| -import java.lang.reflect.Field; |
19 |
| -import java.util.Calendar; |
20 |
| - |
21 |
| -import org.joda.time.DateTime; |
22 |
| -import org.joda.time.LocalDateTime; |
23 |
| -import org.springframework.core.convert.ConversionService; |
24 |
| -import org.springframework.core.convert.converter.Converter; |
25 |
| -import org.springframework.data.domain.Auditable; |
26 |
| -import org.springframework.data.util.ReflectionUtils; |
27 |
| -import org.springframework.format.support.DefaultFormattingConversionService; |
28 |
| -import org.springframework.util.Assert; |
29 |
| -import org.springframework.util.ClassUtils; |
30 |
| - |
31 | 18 | /**
|
32 |
| - * A factory class to {@link AuditableBeanWrapper} instances. |
| 19 | + * A factory to lookup {@link AuditableBeanWrapper}s. |
33 | 20 | *
|
34 | 21 | * @author Oliver Gierke
|
35 |
| - * @since 1.5 |
| 22 | + * @since 1.10 |
36 | 23 | */
|
37 |
| -class AuditableBeanWrapperFactory { |
38 |
| - |
39 |
| - /** |
40 |
| - * Returns an {@link AuditableBeanWrapper} if the given object is capable of being equipped with auditing information. |
41 |
| - * |
42 |
| - * @param source the auditing candidate. |
43 |
| - * @return |
44 |
| - */ |
45 |
| - @SuppressWarnings("unchecked") |
46 |
| - public AuditableBeanWrapper getBeanWrapperFor(Object source) { |
47 |
| - |
48 |
| - if (source == null) { |
49 |
| - return null; |
50 |
| - } |
51 |
| - |
52 |
| - if (source instanceof Auditable) { |
53 |
| - return new AuditableInterfaceBeanWrapper((Auditable<Object, ?>) source); |
54 |
| - } |
55 |
| - |
56 |
| - AnnotationAuditingMetadata metadata = AnnotationAuditingMetadata.getMetadata(source.getClass()); |
57 |
| - |
58 |
| - if (metadata.isAuditable()) { |
59 |
| - return new ReflectionAuditingBeanWrapper(source); |
60 |
| - } |
61 |
| - |
62 |
| - return null; |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * An {@link AuditableBeanWrapper} that works with objects implementing |
67 |
| - * |
68 |
| - * @author Oliver Gierke |
69 |
| - */ |
70 |
| - static class AuditableInterfaceBeanWrapper implements AuditableBeanWrapper { |
71 |
| - |
72 |
| - private final Auditable<Object, ?> auditable; |
73 |
| - |
74 |
| - public AuditableInterfaceBeanWrapper(Auditable<Object, ?> auditable) { |
75 |
| - this.auditable = auditable; |
76 |
| - } |
77 |
| - |
78 |
| - /* |
79 |
| - * (non-Javadoc) |
80 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedBy(java.lang.Object) |
81 |
| - */ |
82 |
| - public void setCreatedBy(Object value) { |
83 |
| - auditable.setCreatedBy(value); |
84 |
| - } |
85 |
| - |
86 |
| - /* |
87 |
| - * (non-Javadoc) |
88 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(org.joda.time.DateTime) |
89 |
| - */ |
90 |
| - public void setCreatedDate(Calendar value) { |
91 |
| - auditable.setCreatedDate(new DateTime(value)); |
92 |
| - } |
93 |
| - |
94 |
| - /* |
95 |
| - * (non-Javadoc) |
96 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedBy(java.lang.Object) |
97 |
| - */ |
98 |
| - public void setLastModifiedBy(Object value) { |
99 |
| - auditable.setLastModifiedBy(value); |
100 |
| - } |
101 |
| - |
102 |
| - /* |
103 |
| - * (non-Javadoc) |
104 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(org.joda.time.DateTime) |
105 |
| - */ |
106 |
| - public void setLastModifiedDate(Calendar value) { |
107 |
| - auditable.setLastModifiedDate(new DateTime(value)); |
108 |
| - } |
109 |
| - } |
| 24 | +public interface AuditableBeanWrapperFactory { |
110 | 25 |
|
111 | 26 | /**
|
112 |
| - * Base class for {@link AuditableBeanWrapper} implementations that might need to convert {@link Calendar} values into |
113 |
| - * compatible types when setting date/time information. |
| 27 | + * Returns the {@link AuditableBeanWrapper} for the given source obejct if it's eligible for auditing. |
114 | 28 | *
|
115 |
| - * @author Oliver Gierke |
116 |
| - * @since 1.8 |
| 29 | + * @param source can be {@literal null}. |
| 30 | + * @return the {@link AuditableBeanWrapper} for the given source obejct if it's eligible for auditing or |
| 31 | + * {@literal null} otherwise. |
117 | 32 | */
|
118 |
| - abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper { |
119 |
| - |
120 |
| - private static final boolean IS_JODA_TIME_PRESENT = ClassUtils.isPresent("org.joda.time.DateTime", |
121 |
| - ReflectionAuditingBeanWrapper.class.getClassLoader()); |
122 |
| - |
123 |
| - private final ConversionService conversionService; |
124 |
| - |
125 |
| - /** |
126 |
| - * Creates a new {@link DateConvertingAuditableBeanWrapper}. |
127 |
| - */ |
128 |
| - public DateConvertingAuditableBeanWrapper() { |
129 |
| - |
130 |
| - DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); |
131 |
| - |
132 |
| - if (IS_JODA_TIME_PRESENT) { |
133 |
| - conversionService.addConverter(CalendarToDateTimeConverter.INSTANCE); |
134 |
| - conversionService.addConverter(CalendarToLocalDateTimeConverter.INSTANCE); |
135 |
| - } |
136 |
| - |
137 |
| - this.conversionService = conversionService; |
138 |
| - } |
139 |
| - |
140 |
| - /** |
141 |
| - * Returns the {@link Calendar} in a type, compatible to the given field. |
142 |
| - * |
143 |
| - * @param value can be {@literal null}. |
144 |
| - * @param targetType must not be {@literal null}. |
145 |
| - * @param source must not be {@literal null}. |
146 |
| - * @return |
147 |
| - */ |
148 |
| - protected Object getDateValueToSet(Calendar value, Class<?> targetType, Object source) { |
149 |
| - |
150 |
| - if (value == null) { |
151 |
| - return null; |
152 |
| - } |
153 |
| - |
154 |
| - if (Calendar.class.equals(targetType)) { |
155 |
| - return value; |
156 |
| - } |
157 |
| - |
158 |
| - if (conversionService.canConvert(Calendar.class, targetType)) { |
159 |
| - return conversionService.convert(value, targetType); |
160 |
| - } |
161 |
| - |
162 |
| - throw new IllegalArgumentException(String.format("Invalid date type for member %s! Supported types are %s.", |
163 |
| - source, AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES)); |
164 |
| - } |
165 |
| - } |
166 |
| - |
167 |
| - /** |
168 |
| - * An {@link AuditableBeanWrapper} implementation that sets values on the target object using refelction. |
169 |
| - * |
170 |
| - * @author Oliver Gierke |
171 |
| - */ |
172 |
| - static class ReflectionAuditingBeanWrapper extends DateConvertingAuditableBeanWrapper { |
173 |
| - |
174 |
| - private final AnnotationAuditingMetadata metadata; |
175 |
| - private final Object target; |
176 |
| - |
177 |
| - /** |
178 |
| - * Creates a new {@link ReflectionAuditingBeanWrapper} to set auditing data on the given target object. |
179 |
| - * |
180 |
| - * @param target must not be {@literal null}. |
181 |
| - */ |
182 |
| - public ReflectionAuditingBeanWrapper(Object target) { |
183 |
| - |
184 |
| - Assert.notNull(target, "Target object must not be null!"); |
185 |
| - |
186 |
| - this.metadata = AnnotationAuditingMetadata.getMetadata(target.getClass()); |
187 |
| - this.target = target; |
188 |
| - } |
189 |
| - |
190 |
| - /* |
191 |
| - * (non-Javadoc) |
192 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedBy(java.lang.Object) |
193 |
| - */ |
194 |
| - public void setCreatedBy(Object value) { |
195 |
| - setField(metadata.getCreatedByField(), value); |
196 |
| - } |
197 |
| - |
198 |
| - /* |
199 |
| - * (non-Javadoc) |
200 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setCreatedDate(java.util.Calendar) |
201 |
| - */ |
202 |
| - public void setCreatedDate(Calendar value) { |
203 |
| - setDateField(metadata.getCreatedDateField(), value); |
204 |
| - } |
205 |
| - |
206 |
| - /* |
207 |
| - * (non-Javadoc) |
208 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedBy(java.lang.Object) |
209 |
| - */ |
210 |
| - public void setLastModifiedBy(Object value) { |
211 |
| - setField(metadata.getLastModifiedByField(), value); |
212 |
| - } |
213 |
| - |
214 |
| - /* |
215 |
| - * (non-Javadoc) |
216 |
| - * @see org.springframework.data.auditing.AuditableBeanWrapper#setLastModifiedDate(java.util.Calendar) |
217 |
| - */ |
218 |
| - public void setLastModifiedDate(Calendar value) { |
219 |
| - setDateField(metadata.getLastModifiedDateField(), value); |
220 |
| - } |
221 |
| - |
222 |
| - /** |
223 |
| - * Sets the given field to the given value if the field is not {@literal null}. |
224 |
| - * |
225 |
| - * @param field |
226 |
| - * @param value |
227 |
| - */ |
228 |
| - private void setField(Field field, Object value) { |
229 |
| - |
230 |
| - if (field != null) { |
231 |
| - ReflectionUtils.setField(field, target, value); |
232 |
| - } |
233 |
| - } |
234 |
| - |
235 |
| - /** |
236 |
| - * Sets the given field to the given value if the field is not {@literal null}. |
237 |
| - * |
238 |
| - * @param field |
239 |
| - * @param value |
240 |
| - */ |
241 |
| - private void setDateField(Field field, Calendar value) { |
242 |
| - |
243 |
| - if (field == null) { |
244 |
| - return; |
245 |
| - } |
246 |
| - |
247 |
| - ReflectionUtils.setField(field, target, getDateValueToSet(value, field.getType(), field)); |
248 |
| - } |
249 |
| - } |
250 |
| - |
251 |
| - private static enum CalendarToDateTimeConverter implements Converter<Calendar, DateTime> { |
252 |
| - |
253 |
| - INSTANCE; |
254 |
| - |
255 |
| - @Override |
256 |
| - public DateTime convert(Calendar source) { |
257 |
| - return new DateTime(source); |
258 |
| - } |
259 |
| - } |
260 |
| - |
261 |
| - private static enum CalendarToLocalDateTimeConverter implements Converter<Calendar, LocalDateTime> { |
262 |
| - |
263 |
| - INSTANCE; |
264 |
| - |
265 |
| - @Override |
266 |
| - public LocalDateTime convert(Calendar source) { |
267 |
| - return new LocalDateTime(source); |
268 |
| - } |
269 |
| - } |
| 33 | + AuditableBeanWrapper getBeanWrapperFor(Object source); |
270 | 34 | }
|
0 commit comments