Crystal and Java

If you are, suddenly involved in a project with plain java and Crytal reports 9 or 10, knowing nothing about it, here are some hints and code that could help you. In my case, i had to get a report from crystal report server and export it to pdf format with plain java, no web, nor swing. Well the first thing you need to get is the crystal SDK for java. Once that got it, the fisrt thing to care about is the connection. If the crystal report is an enterpise server you have to use an interface to get a session. That interface is IEnterpriseSession. Here is an example code of what you can use to get an enterprise session:

IEnterpriseSessiones  es = CrystalEnterprise.getSessionMgr().logon(“Administrator”,
“”, “server”, “secEnterprise”);

Then you need to get a service called InfoStore, which i understood is like the service of crystal reports medadata. so you can do query’s to get some data related with the reports already published.

IInfoStore iStore = (IInfoStore) es.getService(“”, “InfoStore”);

after that you can submit query’s to the crystal repository. Obviously you need to know the objects who you want to request the data. But for general purposes i got a report properties with the following one:

IInfoObjects infoObjects = iStore.query(“Select * From CI_INFOOBJECTS Where SI_PROGID = ‘CrystalEnterprise.Report’ and SI_INSTANCE=0 and SI_NAME=’Report name'”);

the object infoObjects is a collection with the resultset of the query you just submited. To get my report, knowing that it is the only result, i call the method get and store the result in a single IInfoObject object.

IInfoObject infoObject = (IInfoObject) infoObjects.get(0);

Well, you already have the report and all the properties. Now you need an interface to get a handler for the report with an Object from the class ReportClientDocument. You can get it, firts calling a service named “RASReportFactory”

IReportAppFactory reportAppFactory = (IReportAppFactory) es.getService(“RASReportFactory”);

Then get the Reportclientdocument Object which through some methods lets you modify the report

clientDoc = reportAppFactory.openDocument(infoObject.getID(),0, Locale.ENGLISH);

then you can connect to the database. It is suppoused  that the report already have configured the database name that uses, so i just specify the user name and password in the logon method from the DatabaseController gotten from the ReportClientDocument.getDatabaseController() method.

clientDoc.getDatabaseController().logon(“user”, “pwd”);

After that tyou have to set the parameters, if any, in the report. To do that you have to cast the InfoObject instance to an IReport instance and get the properties of the report. Here the code:

IReport reportObject = (IReport)infoObject;

ParameterField newParameter1 = new ParameterField();
ParameterField pField =null;
pField =(ParameterField) clientDoc.getDataDefinition().getParameterFields().getField(0);
pField.copyTo( newParameter1, true );
String valContent = “1”;
ParameterFieldDiscreteValue newValue = new ParameterFieldDiscreteValue();
newValue.setValue( valContent );
newParameter1.getCurrentValues().add( newValue );
clientDoc.getDataDefController().getParameterFieldController().modify(pField, newParameter1);

Once that setted it is almost ready. You only have to execute the report and give it the export option to PDF. This function will return an InputStream, the we create a new FileOutputStream with the path and filename, an output stream linked to the new file, read the InputStream and write to the output.

this.byteArrayInputStream = (ByteArrayInputStream) clientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
this.byteArray = new byte[1024];
final FileOutputStream pdfStream =    new FileOutputStream(“c:\\CrystalConn”+ clientDoc.displayName() + “.pdf”);
final BufferedOutputStream ostream = new BufferedOutputStream(pdfStream);
while ((this.bytesRead = this.byteArrayInputStream.read(this.byteArray)) != -1) {
ostream.write(this.byteArray, 0, this.bytesRead);
}

ostream.flush();
ostream.close();
clientDoc.close();

Well, that’s all for the moment. I will add the way to zip any file in the next entry. If this is useful to you please respond to this blog entry.

Posted in Uncategorized | Leave a comment