Complete samples coming soon
For now you should download the source available in the "Release" section of this project. The unit tests include working examples of MongoDB reports and examples of how to compile and run them. Here's a code snippet to get you started:
private final static String REPORT_NAMES = { "MongoDbReport", "MongoDbConditionalOperators", "MongoDbDates", "MongoDbGeospatial", "MongoDbRegEx", "MongoDbReport_AdHoc" };
@Test public void testReport() {
String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
Map parameters = new HashMap();
try {
connection = new MongoDbConnection(mongoURI, null, null);
parameters.put(MongoDbDataSource.CONNECTION, connection);
File jasperFile;
for (String reportname : REPORT_NAMES) {
jasperFile = new File(PATH + reportname + ".jasper");
if (!jasperFile.exists()) {
JasperCompileManager.compileReportToFile(PATH + reportname + ".jrxml", PATH + reportname + ".jasper");
}
JasperFillManager.fillReportToFile(PATH + reportname + ".jasper", parameters);
JasperExportManager.exportReportToPdfFile(PATH + reportname + ".jrprint");
}
}
catch (Exception e) {
logger.error(e);
}
finally {
if (connection != null) { connection.close(); }
}
You can also leverage the connection pooling feature to handle your connections.
For instance:
@Test public void testManager() {
String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
MongoDbConnectionManager connectionManager = null;
Map parameters = new HashMap();
try {
connectionManager = new MongoDbConnectionManager();
connectionManager.setMongoURI(mongoURI);
connection = connectionManager.borrowConnection();
parameters.put(MongoDbDataSource.CONNECTION, connection);
File jasperFile;
for (String reportname : REPORT_NAMES) {
jasperFile = new File(PATH + reportname + ".jasper");
if (!jasperFile.exists()) {
JasperCompileManager.compileReportToFile(PATH + reportname + ".jrxml", PATH + reportname + ".jasper");
}
JasperFillManager.fillReportToFile(PATH + reportname + ".jasper", parameters);
JasperExportManager.exportReportToPdfFile(PATH + reportname + ".jrprint");
}
}
catch (Exception e) {
logger.error(e);
}
finally {
if (connection != null) {
connectionManager.returnConnection(connection);
}
connectionManager.shutdown();
}
}
The connection pool offers the option to set the Mongo URI, username and password (if applicable), also the connections will be validated to full fit these properties before they are borrowed, so it's safe to change them during the connection manager existence.
Thanks
LissaCoffey
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now