|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2025 the original author or authors. |
| 2 | + * Copyright 2025 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.
|
|
18 | 18 | import jakarta.validation.ConstraintViolation;
|
19 | 19 | import jakarta.validation.ConstraintViolationException;
|
20 | 20 | import jakarta.validation.Validator;
|
| 21 | + |
21 | 22 | import java.util.Set;
|
22 |
| -import org.apache.commons.logging.Log; |
23 |
| -import org.apache.commons.logging.LogFactory; |
| 23 | + |
24 | 24 | import org.bson.Document;
|
| 25 | + |
25 | 26 | import org.springframework.core.Ordered;
|
26 |
| -import org.springframework.util.Assert; |
27 | 27 |
|
28 | 28 | /**
|
29 | 29 | * JSR-303 dependant entities validator.
|
30 | 30 | * <p>
|
31 |
| - * When it is registered as Spring component its automatically invoked after any {@link AbstractMongoEventListener} and |
32 |
| - * before entities are saved in database. |
| 31 | + * When it is registered as Spring component its automatically invoked after object to {@link Document} conversion and |
| 32 | + * before entities are saved to the database. |
33 | 33 | *
|
34 |
| - * @author original authors of {@link ValidatingMongoEventListener} |
35 | 34 | * @author Rene Felgenträger
|
36 |
| - * @see {@link ValidatingMongoEventListener} |
| 35 | + * @author Mark Paluch |
| 36 | + * @since 4.5 |
37 | 37 | */
|
38 | 38 | public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered {
|
39 | 39 |
|
40 |
| - private static final Log LOG = LogFactory.getLog(ValidatingEntityCallback.class); |
41 |
| - |
42 |
| - // TODO: create a validation handler (similar to "AuditingHandler") an reference it from "ValidatingMongoEventListener" and "ValidatingMongoEventListener" |
43 |
| - private final Validator validator; |
| 40 | + private final BeanValidationDelegate delegate; |
44 | 41 |
|
45 | 42 | /**
|
46 | 43 | * Creates a new {@link ValidatingEntityCallback} using the given {@link Validator}.
|
47 | 44 | *
|
48 | 45 | * @param validator must not be {@literal null}.
|
49 | 46 | */
|
50 | 47 | public ValidatingEntityCallback(Validator validator) {
|
51 |
| - Assert.notNull(validator, "Validator must not be null"); |
52 |
| - this.validator = validator; |
| 48 | + this.delegate = new BeanValidationDelegate(validator); |
53 | 49 | }
|
54 | 50 |
|
55 |
| - // TODO: alternatively implement the "BeforeConvertCallback" interface and set the order to highest value ? |
56 | 51 | @Override
|
57 | 52 | public Object onBeforeSave(Object entity, Document document, String collection) {
|
58 | 53 |
|
59 |
| - if (LOG.isDebugEnabled()) { |
60 |
| - LOG.debug(String.format("Validating object: %s", entity)); |
61 |
| - } |
62 |
| - Set<ConstraintViolation<Object>> violations = validator.validate(entity); |
| 54 | + Set<ConstraintViolation<Object>> violations = delegate.validate(entity); |
63 | 55 |
|
64 | 56 | if (!violations.isEmpty()) {
|
65 |
| - if (LOG.isDebugEnabled()) { |
66 |
| - LOG.info(String.format("During object: %s validation violations found: %s", entity, violations)); |
67 |
| - } |
68 | 57 | throw new ConstraintViolationException(violations);
|
69 | 58 | }
|
| 59 | + |
70 | 60 | return entity;
|
71 | 61 | }
|
72 | 62 |
|
73 | 63 | @Override
|
74 | 64 | public int getOrder() {
|
75 | 65 | return 100;
|
76 | 66 | }
|
| 67 | + |
77 | 68 | }
|
0 commit comments