|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.mapping.event; |
| 17 | + |
| 18 | +import jakarta.validation.ConstraintViolation; |
| 19 | +import jakarta.validation.ConstraintViolationException; |
| 20 | +import jakarta.validation.Validator; |
| 21 | +import java.util.Set; |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.bson.Document; |
| 25 | +import org.springframework.core.Ordered; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * JSR-303 dependant entities validator. |
| 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. |
| 33 | + * |
| 34 | + * @author original authors of {@link ValidatingMongoEventListener} |
| 35 | + * @author Rene Felgenträger |
| 36 | + * @see {@link ValidatingMongoEventListener} |
| 37 | + */ |
| 38 | +public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered { |
| 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; |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a new {@link ValidatingEntityCallback} using the given {@link Validator}. |
| 47 | + * |
| 48 | + * @param validator must not be {@literal null}. |
| 49 | + */ |
| 50 | + public ValidatingEntityCallback(Validator validator) { |
| 51 | + Assert.notNull(validator, "Validator must not be null"); |
| 52 | + this.validator = validator; |
| 53 | + } |
| 54 | + |
| 55 | + // TODO: alternatively implement the "BeforeConvertCallback" interface and set the order to highest value ? |
| 56 | + @Override |
| 57 | + public Object onBeforeSave(Object entity, Document document, String collection) { |
| 58 | + |
| 59 | + if (LOG.isDebugEnabled()) { |
| 60 | + LOG.debug(String.format("Validating object: %s", entity)); |
| 61 | + } |
| 62 | + Set<ConstraintViolation<Object>> violations = validator.validate(entity); |
| 63 | + |
| 64 | + if (!violations.isEmpty()) { |
| 65 | + if (LOG.isDebugEnabled()) { |
| 66 | + LOG.info(String.format("During object: %s validation violations found: %s", entity, violations)); |
| 67 | + } |
| 68 | + throw new ConstraintViolationException(violations); |
| 69 | + } |
| 70 | + return entity; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int getOrder() { |
| 75 | + return 100; |
| 76 | + } |
| 77 | +} |
0 commit comments