1 /*
2 * #%L
3 * ReplyResultCallback.java - mongodb-async-driver - Allanbank Consulting, Inc.
4 * %%
5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6 * %%
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
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 * Callback to extract the map/reduce and aggregation results from the reply.
37 *
38 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
39 * mutated in incompatible ways between any two releases of the driver.
40 * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
41 */
42 public class ReplyResultCallback extends
43 AbstractReplyCallback<MongoIterator<Document>> {
44
45 /** The field in the reply holding the results. */
46 private final String myReplyField;
47
48 /**
49 * Create a new ReplyResultCallback.
50 *
51 * @param forwardCallback
52 * The callback to forward the result documents to.
53 */
54 public ReplyResultCallback(
55 final Callback<MongoIterator<Document>> forwardCallback) {
56 this("results", forwardCallback);
57 }
58
59 /**
60 * Create a new ReplyResultCallback.
61 *
62 * @param field
63 * The field in the reply holding the results.
64 * @param forwardCallback
65 * The callback to forward the result documents to.
66 */
67 public ReplyResultCallback(final String field,
68 final Callback<MongoIterator<Document>> forwardCallback) {
69 super(forwardCallback);
70 myReplyField = field;
71 }
72
73 /**
74 * {@inheritDoc}
75 * <p>
76 * Overridden to extract the 'results' elements from the reply.
77 * </p>
78 *
79 * @see com.allanbank.mongodb.client.callback.AbstractReplyCallback#convert(com.allanbank.mongodb.client.message.Reply)
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 }