Create separate class for callback
This commit is contained in:
parent
67d636aaf9
commit
a3e8f9f60e
@ -0,0 +1,53 @@
|
|||||||
|
package org.baeldung.event;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import org.baeldung.annotation.CascadeSave;
|
||||||
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
ReflectionUtils.makeAccessible(field);
|
||||||
|
|
||||||
|
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
|
||||||
|
final Object fieldValue = field.get(getSource());
|
||||||
|
|
||||||
|
if (fieldValue != null) {
|
||||||
|
FieldCallback callback = new FieldCallback();
|
||||||
|
|
||||||
|
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
|
||||||
|
|
||||||
|
getMongoOperations().save(fieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getSource() {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSource(Object source) {
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MongoOperations getMongoOperations() {
|
||||||
|
return mongoOperations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMongoOperations(MongoOperations mongoOperations) {
|
||||||
|
this.mongoOperations = mongoOperations;
|
||||||
|
}
|
||||||
|
}
|
@ -1,37 +1,17 @@
|
|||||||
package org.baeldung.event;
|
package org.baeldung.event;
|
||||||
|
|
||||||
import org.baeldung.annotation.CascadeSave;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.mongodb.core.MongoOperations;
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
|
||||||
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
|
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
|
public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MongoOperations mongoOperations;
|
private MongoOperations mongoOperations;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBeforeConvert(final Object source) {
|
public void onBeforeConvert(final Object source) {
|
||||||
ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
|
ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
|
||||||
|
|
||||||
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
|
|
||||||
ReflectionUtils.makeAccessible(field);
|
|
||||||
|
|
||||||
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
|
|
||||||
final Object fieldValue = field.get(source);
|
|
||||||
|
|
||||||
if (fieldValue != null) {
|
|
||||||
FieldCallback callback = new FieldCallback();
|
|
||||||
|
|
||||||
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
|
|
||||||
|
|
||||||
mongoOperations.save(fieldValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user