2
2
3
3
import com .antkorwin .commonutils .exceptions .InternalException ;
4
4
import com .antkorwin .commonutils .validation .Guard ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
6
import org .bson .Document ;
6
7
import org .springframework .data .mongodb .core .MongoTemplate ;
7
8
8
9
import java .util .Collections ;
9
10
import java .util .HashMap ;
10
11
import java .util .List ;
11
12
import java .util .Map ;
13
+ import java .util .stream .Collectors ;
12
14
13
15
import static com .antkorwin .springtestmongo .errorinfo .MongoDbErrorInfo .MONGO_TEMPLATE_IS_MANDATORY ;
14
16
20
22
class MongoDataExport implements DataSet {
21
23
22
24
private final MongoTemplate mongoTemplate ;
25
+ private final ObjectMapper objectMapper ;
23
26
24
27
MongoDataExport (MongoTemplate mongoTemplate ) {
25
28
Guard .check (mongoTemplate != null , InternalException .class , MONGO_TEMPLATE_IS_MANDATORY );
26
29
this .mongoTemplate = mongoTemplate ;
30
+ this .objectMapper = new ObjectMapper ();
27
31
}
28
32
29
33
@ Override
30
- public Map <String , List <? >> read () {
31
- Map <String , List <? >> map = new HashMap <>();
34
+ public Map <String , List <Map < String , Object > >> read () {
35
+ Map <String , List <Map < String , Object > >> map = new HashMap <>();
32
36
33
37
for (String name : mongoTemplate .getCollectionNames ()) {
34
38
map .put (getEntityClassName (name ), getDataSet (name ));
@@ -37,7 +41,7 @@ public Map<String, List<?>> read() {
37
41
return map ;
38
42
}
39
43
40
- private List <? > getDataSet (String collectionName ) {
44
+ private List <Map < String , Object > > getDataSet (String collectionName ) {
41
45
42
46
Document first = mongoTemplate .getCollection (collectionName )
43
47
.find (Document .class )
@@ -49,9 +53,14 @@ private List<?> getDataSet(String collectionName) {
49
53
try {
50
54
String className = (String ) first .get ("_class" );
51
55
Class <?> aClass = Class .forName (className );
52
- return mongoTemplate .findAll (aClass );
53
- }
54
- catch (ClassNotFoundException e ) {
56
+
57
+ return mongoTemplate .findAll (aClass )
58
+ .stream ()
59
+ .map (e -> objectMapper .convertValue (e , Map .class ))
60
+ .map (e -> (Map <String ,Object >)e )
61
+ .collect (Collectors .toList ());
62
+
63
+ } catch (ClassNotFoundException e ) {
55
64
e .printStackTrace ();
56
65
throw new InternalException (e );
57
66
}
0 commit comments