54 lines
1.5 KiB
Java
54 lines
1.5 KiB
Java
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;
|
|
}
|
|
}
|