Skip to content

Commit a3e8f9f

Browse files
author
alex-semenyuk
committed
Create separate class for callback
1 parent 67d636a commit a3e8f9f

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.baeldung.event;
2+
3+
import java.lang.reflect.Field;
4+
5+
import org.baeldung.annotation.CascadeSave;
6+
import org.springframework.data.mongodb.core.MongoOperations;
7+
import org.springframework.data.mongodb.core.mapping.DBRef;
8+
import org.springframework.util.ReflectionUtils;
9+
10+
public class CascadeCallback implements ReflectionUtils.FieldCallback {
11+
12+
private Object source;
13+
private MongoOperations mongoOperations;
14+
15+
public CascadeCallback(final Object source, final MongoOperations mongoOperations) {
16+
this.source = source;
17+
this.setMongoOperations(mongoOperations);
18+
}
19+
20+
@Override
21+
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
22+
ReflectionUtils.makeAccessible(field);
23+
24+
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
25+
final Object fieldValue = field.get(getSource());
26+
27+
if (fieldValue != null) {
28+
FieldCallback callback = new FieldCallback();
29+
30+
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
31+
32+
getMongoOperations().save(fieldValue);
33+
}
34+
}
35+
36+
}
37+
38+
public Object getSource() {
39+
return source;
40+
}
41+
42+
public void setSource(Object source) {
43+
this.source = source;
44+
}
45+
46+
public MongoOperations getMongoOperations() {
47+
return mongoOperations;
48+
}
49+
50+
public void setMongoOperations(MongoOperations mongoOperations) {
51+
this.mongoOperations = mongoOperations;
52+
}
53+
}
Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package org.baeldung.event;
22

3-
import org.baeldung.annotation.CascadeSave;
43
import org.springframework.beans.factory.annotation.Autowired;
54
import org.springframework.data.mongodb.core.MongoOperations;
6-
import org.springframework.data.mongodb.core.mapping.DBRef;
75
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
86
import org.springframework.util.ReflectionUtils;
97

10-
import java.lang.reflect.Field;
11-
128
public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
9+
1310
@Autowired
1411
private MongoOperations mongoOperations;
15-
12+
1613
@Override
1714
public void onBeforeConvert(final Object source) {
18-
ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
19-
20-
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
21-
ReflectionUtils.makeAccessible(field);
22-
23-
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
24-
final Object fieldValue = field.get(source);
25-
26-
if (fieldValue != null) {
27-
FieldCallback callback = new FieldCallback();
28-
29-
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
30-
31-
mongoOperations.save(fieldValue);
32-
}
33-
}
34-
}
35-
});
15+
ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
3616
}
37-
}
17+
}

0 commit comments

Comments
 (0)