Thursday, March 10, 2005
JasperReports Installation and Setup in WebSphere Studio 5.1.1
Wow!
I love JasperReports!
Alright, I had to download JasperAssistant and JasperReports version 0.6.4 and iText-1.2.jar to get everything running.
Since I am using WSAD 5.1.1 I needed to install JasperAssistant like so:
1. I downloaded JasperAssistant_1.5.1_Eclipse2.1.3.zip
2. I unzipped the .zip file and found a features folder and a plugins folder.
3. I placed plugins/com.jasperassistant.designer_1.5.1 into the c:/program files/ibm/websphere studio/eclipse/plugins folder.
4. I placed the features/com.jasperassistant.designer_1.5.1 into the c:/program files/ibm/websphere studio/eclipse/features folder.
5. I started up Websphere Studio 5.1.1 and whaddayaknow JasperAssistant's Welcome screen appeared.
Then I simply created a DataSource that implements JRDataSource and added a constructor that recieves a List of objects. (in this case County objects)
The class that I needed to create looks like the following:
public class WorkCountyDataSource implements JRDataSource{My WorkCountyReportAction class looks like the following:
public WorkCountyDataSource(List list){
this.data = list;
}
private List data;
private int index;
/* (non-Javadoc)
* @see net.sf.jasperreports.engine.JRDataSource#next()
*/
public boolean next() throws JRException {
index ++;
return (index < data.size());
}
/* (non-Javadoc)
* @see net.sf.jasperreports.engine.JRDataSource#getFieldValue(net.sf.jasperreports.engine.JRField)
*/
public Object getFieldValue(JRField field) throws JRException {
County county = (County) data.get(index);
Object value = null;
try{
Object [] args = { };
Class [] paramTypes = { };
Class countyClass = county.getClass();
Method getMethod = countyClass.getMethod(field.getName(), paramTypes);
value = getMethod.invoke(county, args);
}
catch(Exception e){
throw new JRException(e.getMessage());
}
return value;
}
}
public ActionForward unspecified(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception{
// Get the userid out of the session
String userId = getUser(request.getSession());
if(StringUtils.isEmpty(userId)){
return mapping.findForward("logoff");
}
BudgetDevelopmentManager mgr = (BudgetDevelopmentManager) getBean("budgetDevelopmentManager");
List workCounties = mgr.getWorkCounties();
Map parameterMap = new HashMap();
parameterMap.put("countyId", "Something Crazy");
WorkCountyDataSource workCountyDataSource = new WorkCountyDataSource(workCounties);
response.setContentType("application/pdf");
try {
JasperRunManager.runReportToPdfStream(
getClass().getClassLoader().getResourceAsStream(
"/WEB-INF/reports/workCounty.jasper"),
response.getOutputStream(),
parameterMap,
workCountyDataSource
);
} catch (Exception e) {
e.printStackTrace(System.out);
}
// return to the page that invoked this action
return null;
}
JasperAssistant:
http://www.jasperassistant.com/installation.html
http://www.jasperassistant.com/download.html
iText:
http://www.lowagie.com/iText/download.html
JasperReports:
http://jasperreports.sourceforge.net/
http://sourceforge.net/projects/jasperreports
