|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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 | + |
| 17 | +package org.springframework.data.couchbase.core.mapping.event; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.beans.factory.ObjectFactory; |
| 24 | +import org.springframework.context.ApplicationListener; |
| 25 | +import org.springframework.data.auditing.AuditingHandler; |
| 26 | +import org.springframework.data.auditing.IsNewAwareAuditingHandler; |
| 27 | +import org.springframework.data.auditing.ReactiveIsNewAwareAuditingHandler; |
| 28 | +import org.springframework.data.mapping.context.MappingContext; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * Event listener to populate auditing related fields on an entity about to be saved. |
| 33 | + * |
| 34 | + * @author Oliver Gierke |
| 35 | + * @author Simon Baslé |
| 36 | + * @author Mark Paluch |
| 37 | + * @author Michael Reiche |
| 38 | + */ |
| 39 | +public class ReactiveAuditingEventListener implements ApplicationListener<CouchbaseMappingEvent<?>> { |
| 40 | + |
| 41 | + private final ObjectFactory<Object> auditingHandlerFactory; |
| 42 | + |
| 43 | + public ReactiveAuditingEventListener() { |
| 44 | + this.auditingHandlerFactory = null; |
| 45 | + } |
| 46 | + |
| 47 | + private static final Logger LOG = LoggerFactory.getLogger(ReactiveAuditingEventListener.class); |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new {@link ReactiveAuditingEventListener} using the given {@link MappingContext} and {@link AuditingHandler} |
| 51 | + * provided by the given {@link ObjectFactory}. Registered in CouchbaseAuditingRegistrar |
| 52 | + * |
| 53 | + * @param auditingHandlerFactory must not be {@literal null}. |
| 54 | + */ |
| 55 | + public ReactiveAuditingEventListener(ObjectFactory<Object> auditingHandlerFactory) { |
| 56 | + Assert.notNull(auditingHandlerFactory, "auditingHandlerFactory must not be null!"); |
| 57 | + this.auditingHandlerFactory = auditingHandlerFactory; |
| 58 | + Object o = auditingHandlerFactory.getObject(); |
| 59 | + if(!(o instanceof ReactiveIsNewAwareAuditingHandler)){ |
| 60 | + LOG.warn("auditingHandler IS NOT a ReactiveIsNewAwareAuditingHandler: {}",o); |
| 61 | + } else { |
| 62 | + LOG.info("auditingHandler IS a ReactiveIsNewAwareAuditingHandler: {}",o); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * (non-Javadoc) |
| 68 | + * @see {@link ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)} |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void onApplicationEvent(CouchbaseMappingEvent<?> event) { |
| 72 | + if (event instanceof BeforeConvertEvent) { |
| 73 | + ReactiveIsNewAwareAuditingHandler h = auditingHandlerFactory != null |
| 74 | + && auditingHandlerFactory.getObject() instanceof ReactiveIsNewAwareAuditingHandler |
| 75 | + ? (ReactiveIsNewAwareAuditingHandler) (auditingHandlerFactory.getObject()) |
| 76 | + : null; |
| 77 | + if (auditingHandlerFactory != null && h == null) { |
| 78 | + if (LOG.isWarnEnabled()) { |
| 79 | + LOG.warn("event:{} source:{} auditingHandler is not a ReactiveIsNewAwareAuditingHandler: {}", |
| 80 | + event.getClass().getSimpleName(), event.getSource(), auditingHandlerFactory.getObject()); |
| 81 | + } |
| 82 | + } |
| 83 | + if (h != null) { |
| 84 | + Optional.ofNullable(event.getSource()).ifPresent(it -> h.markAudited(it)); |
| 85 | + } |
| 86 | + } |
| 87 | + if (event instanceof BeforeSaveEvent) {} |
| 88 | + if (event instanceof AfterSaveEvent) {} |
| 89 | + if (event instanceof BeforeDeleteEvent) {} |
| 90 | + if (event instanceof AfterDeleteEvent) {} |
| 91 | + |
| 92 | + if (LOG.isTraceEnabled()) { |
| 93 | + LOG.trace("{} {}", event.getClass().getSimpleName(), event.getSource()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments