Skip to content

Commit

Permalink
Added simple CascadeSave email for User
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-semenyuk committed Aug 23, 2015
1 parent a3e8f9f commit c9d5aff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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;
Expand Down Expand Up @@ -36,6 +37,11 @@ public String getMappingBasePackage() {
return "org.baeldung";
}

@Bean
public UserCascadeSaveMongoEventListener userCascadingMongoEventListener() {
return new UserCascadeSaveMongoEventListener();
}

@Bean
public CascadeSaveMongoEventListener cascadingMongoEventListener() {
return new CascadeSaveMongoEventListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.springframework.util.ReflectionUtils;

public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {

@Autowired
private MongoOperations mongoOperations;

@Override
public void onBeforeConvert(final Object source) {
ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> {
@Autowired
private MongoOperations mongoOperations;

@Override
public void onBeforeConvert(final Object source) {
if (source instanceof User && ((User) source).getEmailAddress() != null) {
mongoOperations.save(((User) source).getEmailAddress());
}
}
}

0 comments on commit c9d5aff

Please sign in to comment.