From c9d5aff1ff9eff3c2d5e3bd03fa8ce38ded8b73b Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sun, 23 Aug 2015 20:46:34 +0200 Subject: [PATCH] Added simple CascadeSave email for User --- .../java/org/baeldung/config/MongoConfig.java | 6 ++++++ .../org/baeldung/event/CascadeCallback.java | 4 ++-- .../event/CascadeSaveMongoEventListener.java | 4 ++-- .../UserCascadeSaveMongoEventListener.java | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/event/UserCascadeSaveMongoEventListener.java diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index 4b776af5b6..f0f6105dfa 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -5,6 +5,7 @@ import java.util.List; import org.baeldung.converter.UserWriterConverter; import org.baeldung.event.CascadeSaveMongoEventListener; +import org.baeldung.event.UserCascadeSaveMongoEventListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; @@ -36,6 +37,11 @@ public class MongoConfig extends AbstractMongoConfiguration { return "org.baeldung"; } + @Bean + public UserCascadeSaveMongoEventListener userCascadingMongoEventListener() { + return new UserCascadeSaveMongoEventListener(); + } + @Bean public CascadeSaveMongoEventListener cascadingMongoEventListener() { return new CascadeSaveMongoEventListener(); diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeCallback.java b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeCallback.java index 2f433b6b44..e56caad4cb 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeCallback.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeCallback.java @@ -8,10 +8,10 @@ import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.util.ReflectionUtils; public class CascadeCallback implements ReflectionUtils.FieldCallback { - + private Object source; private MongoOperations mongoOperations; - + public CascadeCallback(final Object source, final MongoOperations mongoOperations) { this.source = source; this.setMongoOperations(mongoOperations); diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java index ae79c1d92e..79840fb570 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java @@ -6,10 +6,10 @@ import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventLis import org.springframework.util.ReflectionUtils; public class CascadeSaveMongoEventListener extends AbstractMongoEventListener { - + @Autowired private MongoOperations mongoOperations; - + @Override public void onBeforeConvert(final Object source) { ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations)); diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/UserCascadeSaveMongoEventListener.java b/spring-data-mongodb/src/main/java/org/baeldung/event/UserCascadeSaveMongoEventListener.java new file mode 100644 index 0000000000..423df59b95 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/UserCascadeSaveMongoEventListener.java @@ -0,0 +1,18 @@ +package org.baeldung.event; + +import org.baeldung.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; + +public class UserCascadeSaveMongoEventListener extends AbstractMongoEventListener { + @Autowired + private MongoOperations mongoOperations; + + @Override + public void onBeforeConvert(final Object source) { + if (source instanceof User && ((User) source).getEmailAddress() != null) { + mongoOperations.save(((User) source).getEmailAddress()); + } + } +}