Skip to content

Instantly share code, notes, and snippets.

@jadhavj
Last active April 4, 2016 10:25
Show Gist options
  • Save jadhavj/1e314e35542dcc95f8515dc25cfb2ba2 to your computer and use it in GitHub Desktop.
Save jadhavj/1e314e35542dcc95f8515dc25cfb2ba2 to your computer and use it in GitHub Desktop.
public class Products extends Controller {
public WebSocket<String> products() {
return new WebSocket<String>() {
public void onReady(WebSocket.In<String> in,
WebSocket.Out<String> out) {
in.onMessage(new F.Callback<String>() {
public void invoke(String event) {
Object o = com.mongodb.util.JSON.parse(event);
BasicDBObject merchantInfo = (BasicDBObject) o;
String username = merchantInfo.getString("username");
ObjectMapper mapper = new ObjectMapper();
BasicDBList productJsons = new BasicDBList();
Block<Document> printDocumentBlock = new Block<Document>() {
@Override
public void apply(final Document product) {
BasicDBObject productDo = (BasicDBObject) com.mongodb.util.JSON.parse(product.toJson());
productDo.put("_id", product.getObjectId("_id").toString());
productJsons.add(productDo);
}
};
SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
out.write(productJsons.toString());
}
};
Document query = new Document();
query.put("username", username);
FindIterable<Document> products = Mongo.asyncDatabase().getCollection("products").find(query);
products.forEach(printDocumentBlock, callbackWhenFinished);
}
});
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment