google.com, pub-5747754088801811, DIRECT, f08c47fec0942fa0 Skip to main content

Liferay crud porrtlet with Jasper Reports.

I have used Meera Prince Crud operation demo for the Jasper reports which allow to export report in PDF , word, XLS format. It will look lie below image.



Find Source code for the complete example on below location.
LIFERAY JASPER REPORT INTEGRATION


1 I have available PhoneManger Crud Portlet which have pagination and crud functionality.

2 I create report-listing-mobile.jrxml  file with help of Ireport tool.

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report-listing" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
<![CDATA[SELECT * FROM ELECTION_DATA]]>
</queryString>
<field name="mobileId" class="java.lang.String"/>
<field name="mobileName" class="java.lang.String"/>
<field name="mobileDescrition" class="java.lang.String"/>
<field name="mobileBrand" class="java.lang.String"/>
<field name="releaseDate" class="java.util.Date"/>
<group name="mobileName">
<groupExpression><![CDATA[$F{mobileName}]]></groupExpression>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="70" splitType="Stretch">
<staticText>
<reportElement x="132" y="15" width="263" height="25" forecolor="#3399FF"/>
<textElement>
<font fontName="Arial Black" size="15"/>
</textElement>
<text><![CDATA[               MOBILE LIST]]></text>
</staticText>
<line>
<reportElement x="132" y="39" width="263" height="1"/>
</line>
<image>
<reportElement x="26" y="13" width="75" height="57"/>
<imageExpression class="java.lang.String"><![CDATA["../img/logo.jpg"]]></imageExpression>
</image>
<textField pattern="dd/MM/yyyy">
<reportElement x="432" y="19" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="29" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="44" splitType="Stretch">
<staticText>
<reportElement x="10" y="17" width="100" height="20" forecolor="#666600"/>
<textElement/>
<text><![CDATA[MOBILE ID]]></text>
</staticText>
<staticText>
<reportElement x="110" y="17" width="100" height="20" forecolor="#666600"/>
<textElement/>
<text><![CDATA[MOBILE NAME]]></text>
</staticText>
<staticText>
<reportElement x="210" y="17" width="100" height="20" forecolor="#666600"/>
<textElement/>
<text><![CDATA[DESCRIPTION]]></text>
</staticText>
<staticText>
<reportElement x="310" y="17" width="100" height="20" forecolor="#666600"/>
<textElement/>
<text><![CDATA[MOBILE BRAND]]></text>
</staticText>
<staticText>
<reportElement x="410" y="17" width="100" height="20" forecolor="#666600"/>
<textElement/>
<text><![CDATA[MOBILE RELEASE DATE]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="44" splitType="Stretch">
<textField>
<reportElement x="10" y="18" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{mobileId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="18" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{mobileName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="210" y="18" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{mobileDescrition}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="310" y="18" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{mobileBrand}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="410" y="18" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.util.Date"><![CDATA[$F{releaseDate}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>




3 I created one bean name with MobileBean

 package com.bean;  
import java.util.Date;
public class MobileBean {
private String mobileId;
private String mobileName;
private String mobileDescrition;
private String mobileBrand;
private Date releaseDate;
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public String getMobileBrand() {
return mobileBrand;
}
public void setMobileBrand(String mobileBrand) {
this.mobileBrand = mobileBrand;
}
public String getMobileId() {
return mobileId;
}
public void setMobileId(String mobileId) {
this.mobileId = mobileId;
}
public String getMobileName() {
return mobileName;
}
public void setMobileName(String mobileName) {
this.mobileName = mobileName;
}
public String getMobileDescrition() {
return mobileDescrition;
}
public void setMobileDescrition(String mobileDescrition) {
this.mobileDescrition = mobileDescrition;
}
}


4 I have created DAO to get the Mobile data with help of MobilePhoneLocalServiceUtil

package com.dao;

import java.util.ArrayList;
import java.util.List;

import com.bean.MobileBean;
import com.bean.PartyBean;
import com.bean.PartyDataBean;
import com.liferay.portal.kernel.exception.SystemException;
import com.meera.dbservice.model.MobilePhone;
import com.meera.dbservice.service.MobilePhoneLocalServiceUtil;

public class PartyDAO {

public static List<MobileBean> allMobileData() {
List<MobilePhone> allDatas = new ArrayList<MobilePhone>();
List<MobileBean> allData = new ArrayList<MobileBean>();
try {
allDatas = MobilePhoneLocalServiceUtil.getMobilePhones(0,
MobilePhoneLocalServiceUtil.getMobilePhonesCount());

for (int i = 0; i < allDatas.size(); i++) {

MobilePhone mobilePhone = allDatas.get(i);

MobileBean mobileBean = new MobileBean();

mobileBean.setMobileId(String.valueOf(mobilePhone
.getMobilePhoneId()));

mobileBean.setMobileName(mobilePhone.getName());

mobileBean.setMobileDescrition(mobilePhone.getDescription());

mobileBean.setMobileBrand(mobilePhone.getBrand());

mobileBean.setReleaseDate(mobilePhone.getReleaseDate());

allData.add(mobileBean);

System.out.println(mobilePhone.getName());

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

return allData;

}
}

5 I have created serveResource method inside the PhoneManagerMVCPortletAction cotroller to response different serve report request.

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws PortletException, IOException {

resourceResponse.setContentType("application/pdf");

OutputStream sos = null;

String reportType = resourceRequest.getResourceID();


try {
System.out.println("Genarate PDF");

PortletContext application = getPortletContext();
String prefix = application.getRealPath("\\");
// String file = "WEB-INF/report-listing.jrxml";
String file = "WEB-INF/report-listing-mobile.jrxml";
String path = prefix + file;

System.out.println("hi "+path);

//ServletOutputStream outputStream = actionResponse.g




 sos = resourceResponse.getPortletOutputStream();


// Load that .jrxml file in Jasper Design
JasperDesign jasperDesign = JRXmlLoader.load(path);

// Compile that .jrxml file into .jasper file
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

/* Populate data from your database It must contain all the fields used in designing a .jrxml file
  Here we have used Java Bean Data Source in generating report.
  So populate ArrayList which contains your collection of your beans.
*/
//JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PartyDAO.allPartyData() ,false);

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PartyDAO.allMobileData() ,false);

// Filling the reports using your .jasper file and generated Java Bean Datasource
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, ds);


if(reportType!=null && reportType.equals("PDF")){
resourceResponse.setContentType("application/pdf");
JRPdfExporter expoterPDF = new JRPdfExporter();
expoterPDF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
expoterPDF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos);
expoterPDF.exportReport();
}

  // Response for generating EXCEL report
if(reportType!=null && reportType.equals("XLS")){
resourceResponse.setContentType("application/vnd.ms-excel");
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, sos);
exporterXLS.exportReport();
}

// Response for generating WORD report
if(reportType!=null && reportType.equals("WORD")){
resourceResponse.setContentType("application/msword");
JRRtfExporter exporterRTF = new JRRtfExporter();
exporterRTF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporterRTF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos);
exporterRTF.exportReport();
}

}

catch (Exception e) {
System.out.println(e);
}

finally{
sos.flush();
sos.close();
}
System.out.println("Report genarated succesfully");



}
}

6 I create Link to call ServeResource of Controller file in view.jsp. i have created img folder. I added pdf.jpg, xls.jpg , word.png in img folder.

 <div>
 <a href="<portlet:resourceURL id="PDF"  />"><img src="<%=request.getContextPath()%>/img/pdf.jpg" alt="PDF" style="width:40px;height:40px"></a>
<a href="<portlet:resourceURL id="XLS"  />"><img src="<%=request.getContextPath()%>/img/xls.jpg" alt="XLS" style="width:40px;height:40px"></a>
<a href="<portlet:resourceURL id="WORD"  />"><img src="<%=request.getContextPath()%>/img/word.png" alt="WORD" style="width:40px;height:40px"></a>
  </div>

7 Deploy the portlet.

8 Click on PDF, WORD or XLS  image to download report.





Comments

Popular posts from this blog

Liferay portlet with alloy Ui form validator example.

 Alloy Ui  is built on YUI and we can use it in our liferay portlets.  Yui provides many component which are easy to use in our portlets  Here we see basic form validator.example with liferay portlet. Create portlet AlloyUi-portlet in eclipse. Add below content in view.jsp file Here we create two link for two JSP. <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <script src="http://cdn.alloyui.com/2.5.0/aui/aui-min.js"></script> <link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet"></link> <portlet:defineObjects /> <portlet:renderURL var="basicform"> <portlet:param name="mvcPath" value="/BasicFormValidatorExample.jsp"/> </portlet:renderURL> <portlet:renderURL var="fullform"> <portlet:param name="mvcPath" value="/FullFormValidatorExample.jsp"/> </portlet:re...

Add custom field in user expando api liferay 6.2

Custom Field Liferay 1) The easiest way to add customfield is  add customfield with control panel. Admin -> Control panel -> Configuraton -> custom fields -> Click user -> Add custom field -> Add key and Type (e.g key - nickname type - Text field index) Click on save Now you done. Create One Hook Liferay ->  https://www.liferay.com/documentation/liferay-portal/6.2/development/-/ai/customize-and-extend-functionality-hooks-liferay-portal-6-2-dev-guide-en Override html\portlet\users_admin\user\details.jsp Add below code in jsp for the custom field nickname.     <div class="exp-ctrl-holder">            <liferay-ui:custom-attribute              className="<%= User.class.getName() %>"              classPK="<%= (selUser != null) ? selUser.getUserId() : 0 %>"               editable="<%= true %...