1

Here is my GeneratePdf.java Import ...

  public class GeneratePdf {
        public static void main(String[] args) {
        try {
            JRDataSource ds = getDatasource();
            // - Chargement et compilation du rapport
line32      JasperDesign jasperDesign = JRXmlLoader.load("/home/gocoffee.jrxml");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

            // - Paramètres à envoyer au rapport
            Map parameters = new HashMap();
            parameters.put("Titre", "Titre");

            // - Execution du rapport
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, ds);

            // - Création du rapport au format PDF
            JasperExportManager.exportReportToPdfFile(jasperPrint, "home/test2.pdf");
        } catch (JRException e) {
        }
    }

Mongo connection and get data ():

private static JRDataSource getDatasource() {
        // Retrieve session
        try{
            Mongo m = new Mongo("localhost", 27017);
            DB db = m.getDB("test");
            DBCollection t = db.getCollection("test");
            List<DBObject> list = t.getIndexInfo();
            JRDataSource ds = new JRBeanCollectionDataSource(list);
            return ds;
        } catch (UnknownHostException e) {
            System.out.println("Error mongo connection");
        } catch (Exception e) {
            System.out.println("Other Exception");
        }
        return null;
    }
}

I get this error but I dont understand it:

run:
17-Feb-2012 17:07:26 org.apache.commons.digester.Digester endElement
SEVERE: End event threw exception
java.lang.reflect.InvocationTargetException
....
....
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156)
    at GeneratePdf.main(GeneratePdf.java:32)

Caused by: net.sf.jasperreports.engine.JRRuntimeException: No query executer factory registered for the 'MongoDbQuery' language.
    at net.sf.jasperreports.engine.util.JRQueryExecuterUtils.getQueryExecuterFactory(JRQueryExecuterUtils.java:64)
    at net.sf.jasperreports.engine.design.JRDesignDataset.queryLanguageChanged(JRDesignDataset.java:1122)
    at net.sf.jasperreports.engine.design.JRDesignDataset.setQuery(JRDesignDataset.java:600)
    at net.sf.jasperreports.engine.design.JasperDesign.setQuery(JasperDesign.java:789)
    ... 28 more
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
Louis
  • 622
  • 2
  • 10
  • 26

2 Answers2

3

Using the JRBeanCollectionDataSource is not the right way to go about using the MongoDB connector. Take a look at this test that comes with the Jaspersoft MongoDB Connector source:

MongoDbDatasource/src/test/java/com/jaspersoft/mongodb/ReportTest.java

Both the binary connector and the source are on the project page.

To keep this answer self-contained, here's a code snippet showing how to fill a MongoDB report. It's a modified extract from the file I mention above.

String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
  connection = new MongoDbConnection(mongoURI, null, null);
  parameters.put(MongoDbDataSource.CONNECTION, connection);
  File jasperFile;
  jasperFile = new File("MongoDbReport.jasper");
  JasperCompileManager.compileReportToFile("MongoDbReport.jrxml", "MongoDbReport.jasper");
  JasperFillManager.fillReportToFile("MongoDbReport.jasper", parameters);
  JasperExportManager.exportReportToPdfFile("MongoDbReport.jrprint");
}
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (connection != null) {
    connection.close();
  }
}
mdahlman
  • 9,204
  • 4
  • 44
  • 72
  • I read your blog and it is very interesting .I have a new question [here](http://stackoverflow.com/questions/9340767/generatepdf-with-jasperreport-library-and-mongodb) if you could help me. Thanks – Louis Feb 18 '12 at 13:17
  • new mistake ! I added log4j.xml (log4j.properties) and i have again the same error : `24 Feb 2012 14:20:53 ERROR [org.apache.commons.digester.Digester] End event threw exception java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156) .... at Generate.main(Generate.java:29) Caused by: net.sf.jasperreports.engine.JRRuntimeException: No query executer factory registered for the 'MongoDbQuery' language. ...` – Louis Feb 24 '12 at 14:24
  • 1
    Hi mdahlman, It looks your above mentioned site is not working. Could you please look for this? Please try to provide sample code/Project – PAA Oct 09 '15 at 20:26
  • @mdahlman - Could you please guide on http://stackoverflow.com/questions/41723713/how-to-provide-connection-object-of-mongodb-to-jasperfillmanager-fillreport ? –  Jan 18 '17 at 16:09
0

You need to add following line:

JRProperties.setProperty("net.sf.jasperreports.query.executer.factory.MongoDbQuery", "com.jaspersoft.mongodb.query.MongoDbQueryExecuterFactory");

Verify path in datasource jar file, e.g. js-mongodb-datasource-0.5.0

Alex K
  • 22,315
  • 19
  • 108
  • 236
Amit S
  • 151
  • 9