Friday, 22 June 2012

Deploy the Java Web Application to Jelastic Paas Cloud

Jelastic is Java Paas (Platform as a Service) cloud for Java Server hosting. we can easily create the server instances(Tomcat, Glassfish ) and SQL Instances (MySQL, PostgreSQL)  and deploy the Java Web applications.


Jelastic website make its easy to use. when we logged in  one of the server then one window popped up, to create the environment



here we select servers (Tomcat, GlassFish), Database Servers (MySQL,PostgreSQL) and give the unique name of ur environment. then click create button. Jelastic create the tomcat and MySQL instances. these instance are not shared to others. so we can start/stop these instance at any time. we can change the config files like Tomcat (server.xml, catalina.properties), MySQL(mysql.ini) files.

When we create the instances they send mail contain the user name and password for accessing the Database server.

In this screen-cast (below) i deploy the  previous post Jasper Report +JSF+JPA Web Application into Jelastic paas cloud.



Screen Cast

Thursday, 21 June 2012

JSF + JPA + JasperReports (iReport) Part 2

In this post  is  a continuation of Jasper Report Part 1. here we will discuss about some advanced jasper report concepts like passing complex object like List, Date object and how to create and use local variable in jasper report, Add the sub-report and background image to report. Check my latest post about Integrate Charts into Jasper Reports.

here i am using JPA (Java Persistence API) for accessing the Database. so i create the two entity ShoppingCart and Item. here ShoppingCart entity contain list of Item entities. then i am going to pass these objects to Jasper Report. (Check the video in below)

ShoppingCart.java
@Entity
public class ShoppingCart {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;
    @Temporal(TemporalType.DATE)
    private Date dop;
    @OneToMany(cascade= CascadeType.ALL)
    private List listOfItems;

    // getters and setters
}

Item.java
@Entity
public class Item {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;
    private String itemName;
    private float price;
    private int noOfUnits;

    //getters and setters

}

Create Report
          now i am going create report. we can use iReport standalone or iReport Netbeans Plugin to create the report. first we need to create some fields in report. these fields are matched with ShoppingCart Bean property names and Data types also.


and set the data types of these fields correctly.

firstName  ==> java.lang.String
lastName  ==> java.lang.String
dop  ==> java.util.Date
listOfItems ==> java.util.List

then drag these fields to detail pane of the report as per needs. then right click the Report and compile the report



then its creates the jasper file. and its compiled format of jasper report. check part 1 for more details of overview of jasper report.

now create one JSF page with single Command Button (Submit Button)
    
        Facelet Title
    
    
        
            
        
    

and we create one managed bean for above JSF page
DemoBean.java
@ManagedBean
@SessionScoped

public class DemoBean {
    
     public void pdf() throws JRException, IOException {
     
         EntityManager em=PerisitenceManager.getEntityManager();
        Query query= em.createQuery("select s from ShoppingCart s");
         List listOfShoppingCart=(List)query.getResultList();
        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(listOfShoppingCart);
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/report.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap(), beanCollectionDataSource);
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        FacesContext.getCurrentInstance().responseComplete();
    }
}

Now when u run the project its generate the report in PDF file format.

now we integrate the sub report. so drag the sub-report component from palettes. then add the Item beans property as a fields names and correct data types.

and data types
itemName ==> java.lang.String
price ==> java.lang.Float
noOfUnits ==>  java.lang.Integer

once sub report is ready then compile the subreports.

while create the subreport we passed the Empty Data Source. so now we need to change to listOfItems as a data source, we can't pass directly because its not accept List. so we need to wrap into the JRBeanCollectionDataSource Object.

so in main report select the sub report and change the properties

Connection Type ==> Use a datasource Expression
DataSource Expression ==> new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfItems})




You Can Download this complete example from GitHub (or) google code .

How to use

  1. Change the properties in persistence.xml file, like db name, db hostname ,username and password.
  2. Add necessary lib to class path like Hibernate libs, MySQL lib, Jasper Report lib. for simplicity in this zip contain war file under dist/ folder. so just extract   and see all lib are located in WEB-INF/lib section. copy all files and add to to ur class path (Libraries )
  3. Create the jasperdb database in ur mysql server
  4. First run the NewServlet , this servlet create necessary tables in ur database. and insert some sample rows to the table.
  5. Finally run the index.xhtml file and generate the report.


I deployed same example in Jelastic Paas cloud, you can access through http://ramkicse.jelastic.servint.net/jasper/ URL
click the button to generate the report. (Its expired)

check How to deploy the Web application to Jelastic cloud


Check my latest post about Integrate Charts into Jasper Reports

Screen Cast (Watch the video in HD)

Comments are Welcomed
Related Posts Plugin for WordPress, Blogger...