Wednesday, January 12, 2011

Jasper Report with Liferay integration

JASPER REPORT
===============
1) step-1
~~~~~~~~~~~

package com.ext.portlet.<some>.service.impl;

public class <some>LocalServiceImpl extends <some>LocalServiceBaseImpl {

public void downloadReport() throws SystemException, PortalException{


UserLocalService service = UserLocalServiceUtil.getService();

DetachedCriteria dCriteria = DetachedCriteria.forClass(User.class);

dCriteria.add(Restrictions.eq("active", true));

DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

List<user> userList = (List)service.dynamicQuery(dynamicQuery);
List<userandcontactinfo> userandContactInfoList = new ArrayList<userandcontactinfo>();
for(int i=0; i<userlist.size(); classloader="" contact);="" contact="" i++){="" user="" userandcontactinfo(user,="" userandcontactinfo="" userandcontactinfolist.add(userandcontactinfo);="" }="">LocalServiceImpl.class.getClassLoader();
JasperDesign jasperDesign;
JasperReport jasperReport;
JasperPrint jasperPrint;
try {

InputStream inputStream = classloader.getResourceAsStream("profiles.jrxml");

jasperDesign = JRXmlLoader.load(inputStream);


jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
userandContactInfoList);
jasperPrint = JasperFillManager.fillReport(
jasperReport, null, ds);

String dest ;
File file = new File("");
dest = file.getAbsolutePath();

JasperExportManager.exportReportToPdfFile(jasperPrint, dest+"/profiles.pdf");

} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

------------------------------------------------------------------------------------

step-2
~~~~~~~~~~

package com.ext.portlet.<some>.action;

import java.io.Serializable;
import java.util.Date;

import com.ext.portlet.<some>.model.UserInfo;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.User;


public class UserandContactInfo implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public UserandContactInfo(User user, Contact contact){

this.screenName = user.getScreenName();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.dob = user.getBirthday();
this.gender = (contact.getMale()?"Male":"Female");
this.emailAddress = user.getEmailAddress();
}


public String getEmailAddress() {
return emailAddress;
}
public String getFullName() {
return fullName;
}
public String getScreenName() {
return screenName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}

private String emailAddress;
private String fullName;
private String screenName;
private String firstName;
private String lastName;


}

---------------------------------------------------------------------------------------------------------------------

step-3
~~~~~~~~~~~~


package com.ext.portlet.<some>.action;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ext.portlet.<some>.service.<some>LocalServiceUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.servlet.ServletResponseUtil;

public class DownloadPdf extends PortletAction {

public ActionForward strutsExecute(
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception {

try {
<some>ServiceUtil.downloadReport();
InputStream is = new BufferedInputStream(
new FileInputStream("profiles.pdf"));

ServletResponseUtil.sendFile(response, "Profiles.pdf", is, "application/pdf");

return null;
}
catch (Exception e) {
PortalUtil.sendError(e, request, response);
return null;
}
}
}

--------------------------------------------------------------------------------------------------------------------

step-4
~~~~~~~~~~
jrxml file should be >>>>>>>>> ext-impl/src/profiles.jrxml
==============

<jasperreport bottommargin="20" columncount="1" columnspacing="0" columnwidth="535" issummarynewpage="false" istitlenewpage="false" leftmargin="30" name="profiles" orientation="Portrait" pageheight="842" pagewidth="595" printorder="Vertical" rightmargin="30" topmargin="20" whennodatatype="NoPages">
<property name="ireport.scriptlethandling" value="0">
<property name="ireport.encoding" value="UTF-8">
<import value="java.util.*">
<import value="net.sf.jasperreports.engine.*">
<import value="net.sf.jasperreports.engine.data.*">


<field class="java.lang.String" name="emailAddress">
<field class="java.lang.String" name="fullName">
<field class="java.lang.String" name="screenName">
<field class="java.lang.String" name="firstName">
<field class="java.lang.String" name="lastName">


<background>
<band height="0" issplitallowed="true">
</band>
</background>
< title > <br /> <band height="12" isSplitAllowed="true" ><br /> </band><br />
<pageheader>
<band height="19" issplitallowed="true">
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement forecolor="red" height="18" key="textField" width="100" x="404" y="1">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.Integer"></textfieldexpression>
</reportelement>

</textfield>
</band>
<columnheader>
<band height="0" issplitallowed="true">
</band>
</columnheader>
<detail>
<band height="783" issplitallowed="true">
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement height="18" key="textField" width="205" x="133" y="136">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.String"></textfieldexpression>
</reportelement>
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement height="18" key="textField" width="271" x="133" y="37">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.String"></textfieldexpression></reportelement>
-------------
-----------
------------
---------
</textfield>
</textfield>
<columnfooter>
<band height="0" issplitallowed="true">
</band>
</columnfooter>
<pagefooter>
<band height="0" issplitallowed="true">
</band>
</pagefooter>
<lastpagefooter>
<band height="16" issplitallowed="true">
</band>
</lastpagefooter>
<summary>
<band height="15" issplitallowed="true">
</band>
</summary>
</band>

--------------------------------------------------------------------------------------------------------------------


step-5
~~~~~~~~~

Required jars


a) jasperreports-3.0.0.jar >>>>>>>>tomcat/webapps/ROOT/WEB-INF/lib
b) itext-1.3.1.jar >>>>>>> tomcat/webapps/ROOT/WEB-INF/lib

JasperReports 3.5 for Java Developers

1 comment :

Anonymous said...

Hey can you please give the proper step by step example of jasper report integration to liferay ....

Actually I have created the iReport i.e .jrxml file . now please tell me the furthur steps.