1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.allanbank.mongodb.client.callback;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 import com.allanbank.mongodb.Callback;
28 import com.allanbank.mongodb.MongoDbException;
29 import com.allanbank.mongodb.MongoIterator;
30 import com.allanbank.mongodb.bson.Document;
31 import com.allanbank.mongodb.bson.element.DocumentElement;
32 import com.allanbank.mongodb.client.SimpleMongoIteratorImpl;
33 import com.allanbank.mongodb.client.message.Reply;
34
35
36
37
38
39
40
41
42 public class ReplyResultCallback extends
43 AbstractReplyCallback<MongoIterator<Document>> {
44
45
46 private final String myReplyField;
47
48
49
50
51
52
53
54 public ReplyResultCallback(
55 final Callback<MongoIterator<Document>> forwardCallback) {
56 this("results", forwardCallback);
57 }
58
59
60
61
62
63
64
65
66
67 public ReplyResultCallback(final String field,
68 final Callback<MongoIterator<Document>> forwardCallback) {
69 super(forwardCallback);
70 myReplyField = field;
71 }
72
73
74
75
76
77
78
79
80
81 @Override
82 protected MongoIterator<Document> convert(final Reply reply)
83 throws MongoDbException {
84 List<Document> results = Collections.emptyList();
85
86 final List<Document> replyDocs = reply.getResults();
87 if (replyDocs.size() == 1) {
88 final Document doc = replyDocs.get(0);
89
90 final List<DocumentElement> resultsElems = doc.find(
91 DocumentElement.class, myReplyField, ".*");
92 if (!resultsElems.isEmpty()) {
93 results = new ArrayList<Document>();
94 for (final DocumentElement resultElem : resultsElems) {
95 results.add(resultElem.getDocument());
96 }
97 }
98 }
99
100 return new SimpleMongoIteratorImpl<Document>(results);
101 }
102
103 }