Senin, 14 Februari 2011

Kismiss Sub Title

At this section, we will be shown how to add sub title to enhance information of the report. We just need to add some rows of code. Let's see the code below :

KismissReport report = KismissReport.getInstance();
List<Object> subs = new ArrayList<Object>();
subs.add("Business Developement Indicator");

params.put(ReportFactory.SUB_TITLE, subs);
params.put(ReportFactory.TITLE, "TEST KISMISS REPORT");
params.put(ReportFactory.INFOTITLE, "Report");
params.put(ReportFactory.REPORT_NAME, "employeeTest.pdf");
report.setVisibleField(new String[]{"id","firstName","lastName","capacity"});
report.generateAnnotatedPdfFiles(Employee.class, employees, "D:/Personal/Test/", params, group);

When we run the above code, our report will be added “Business Developement Indicator” as our report's subtitle, see the result below :


KismissReport report = KismissReport.getInstance();
List<Object> subs = new ArrayList<Object>();
subs.add("Business Developement Indicator");
subs.add(new SubTitle("Print Date :"+new SimpleDateFormat("dd MMM yyyy hh:mm:ss").format(new Date()), "Print By : Hong Kisman"));

params.put(ReportFactory.SUB_TITLE, subs);
params.put(ReportFactory.TITLE, "TEST KISMISS REPORT");
params.put(ReportFactory.INFOTITLE, "Report");
params.put(ReportFactory.REPORT_NAME, "employeeTest.pdf");
report.setVisibleField(new String[]{"id","firstName","lastName","capacity"});
report.generateAnnotatedPdfFiles(Employee.class, employees, "D:/Personal/Test/", params, group);

At the code above, we add a SubTitle instance class to sub title collection, the report we print “Print Date” at the left side and “Print By” at the right side.
Let's see the report result as follow :


Coloring Header background

We can change the background color of header by defining the “columnHeaderColor” at “Header” tag.

Let us give a color to our employee report header, the “Employee.java” will be look like :

...
/**
    * @author Kisman Hong
    * test Kismiss Reports
*/
@Kismiss(name = "Employee", columnAutoSize=true)
@Header(columnHeaderHeight=45, isColumnHeaderBold=true, columnHeaderColor ="#BDBBBC")
@Detail(lineWidth=0.5)
public class Employee {
   …
}


The code above tell us to give the column header color, then the result will be looked like :



Kismiss Multi Row Header

At this section, we will learn how to draw multi rows at report header. For multi rows header we need to define the properties of getter method. The example code below show us how to multi header :

package com.softtech.kismiss.elevenExample;

import com.softtech.kismiss.enumer.CalculationType;
import com.softtech.kismiss.property.Header;
import com.softtech.kismiss.property.Calculation;
import com.softtech.kismiss.property.Kismiss;
import com.softtech.kismiss.property.Property;

/**
    * @author Kisman Hong
    * test Kismiss Reports
*/
@Kismiss(name = "Employee", columnAutoSize=true)
@Header(columnHeaderHeight=45, isColumnHeaderBold=true)
@Detail(lineWidth=0.5)
public class Employee {
    ...
    @Property(width = 100, position = 1, columnHierarchy={"NAME"}, heightPortion=     {15})
    public String getFirstName() {
      return firstName;
    }

    public void setFirstName(String firstName) {
      this.firstName = firstName;
    }

    @Property(width = 100, position = 2, columnHierarchy={"NAME"}, heightPortion={15})
    public String getLastName() {
      return lastName;
    }

    public void setLastName(String lastName) {
      this.lastName = lastName;
    }
    ...
}


By adding “columnHierarchy” at property, we tell kismiss to define parent field name, “heightPortion” indicate the height of the parent field.

Let us take a look to our report result :

Note :
Make sure the fields to be group in header have the position side by side.
We can add the header height by define “columnHeaderHeight” in Header tag.


Kismiss Field Group

At this section, kismiss give a bonus feature to prepare fields name for grouping. By putting “FieldGroup” tag on top of field name in JavaBean class, we tell kismiss that the fields are used for dynamic group.

This feature is quite simple, only by adding FieldGroup tag like shown below :

    @FieldGroup(label={"firstName:Employee First Name","lastName"})
    private Employee employee;

    @FieldGroup
    private String lastName;

    @FieldGroup
    private String firstName;

    @FieldGroup(label="Alamak")
    private String address;

    @FieldGroup
    private double phoneNumber;

    @FieldGroup
    private String postCode;

After defining the tag, we must add some code when generating report, like :

Map<String,String> fieldsGroup = GroupUtils.getInstance(). groupPropertyForClass(Employee.class);



Then we must passing the fieldsGroup to jsp file in dropdown box or others.

Kismiss Dynamic Field

At this section, kismiss introduce a feature to define field to be shown dynamically. We choose the field(s) to generated to report. This feature is easy to use and simple to implement.
This feature can be used by calling the “setVisibleField” method in the report instance. Code will look like :

   KismissReport report = KismissReport.getInstance();
     params.put(ReportFactory.TITLE, "TEST KISMISS REPORT");
     params.put(ReportFactory.INFOTITLE, "Report");
     params.put(ReportFactory.REPORT_NAME, "employeeTest.pdf");
     report.setVisibleField(new String[]{"id","firstName","lastName","capacity"});
     report.generateAnnotatedPdfFiles(Employee.class, employees, "D:/Personal/Test/", params, group);

This example tell us that the fields to be shown in report are “id”, “firstName”, “lastName”, and “capacity” only. Kismiss will generate the report based on field name of the report declared in JavaBean. See the following report :




Kismiss Dynamic Grouping

Kismiss provide one powerful feature, dynamic grouping. We can group field dynamically as the user want. We just passing the field to be grouped, kismiss will generate the report based on the field group chosen.

When using dynamic grouping, we need not to declare grouping in JavaBean class, but it will be defined outside the class.

Then our “Employee.java” will be :


@Kismiss(name = "Employee")
@Header
@Detail
public class Employee {
...
}



We don't need to define “Group” tag anymore.

Before we call the report generate method, we need to define our field group. Our code will look like :

KismissReport report = KismissReport.getInstance();

HashMap<String, Object> params = new HashMap<String, Object>();
params.put(ReportFactory.TITLE, "TEST KISMISS REPORT");
params.put(ReportFactory.INFOTITLE, "Report");
params.put(ReportFactory.REPORT_NAME, "employeeTest.pdf");
List<Calculation> calculations = new ArrayList<Calculation>();
Calculation calculation = new Calculation("capacity", CalculationType.Sum, "#",  "Sum Capacity", VerticalAlignment.Middle, HorizontalAlignment.Right);
calculations.add(calculation);
com.softtech.kismiss.access.Group group = new com.softtech.kismiss.access.Group("firstName", calculations, 20, 20);
group.setCalculationPrintType(CalculationPrintType.Complete);

report.generateAnnotatedPdfFiles(Employee.class, employees, "D:/Personal/Test/", params, group);

By declaring field in code, we have flexibility to define field group dynamically, only by passing the name of field to be grouped and calculation that included to that group.

In the example above, we can see that the report group by “firstName” and calculate the “capacity” with “Sum” CalculationType.


Kismiss Group and Calculation

At this section, we will learn how to define group and calculation. It's not difficult for grouping and calculating field.

@Kismiss(name = "Employee", columnAutoSize=true)
@Header
@Group(calculation = {"capacity : Sum"}, groupBy="firstName", calculationPrintType=CalculationPrintType.Complete)
public class Employee {
...
}

calculation defining in a group, this example tell us that the employee report group by “firstName” and calculate the “capacity” with “Sum” caculation type.

If the group want to omitted we can set isShowInDetail in Property Tag to “false”. This tell kismiss not to display and operate the field.
See the example report that using group and calculation :


Look at the report result, we can see that the report group by “firstName” and Sum the “capacity” based on “firstName”. This is not a difficult way to do.

We can also calculate the capacity field by adding calculation tag to “getCapacity” method, this calculation will calculate all the data. Modify the code as below :

package com.softtech.kismiss.seventhExample;
import com.softtech.kismiss.enumer.CalculationType;
import com.softtech.kismiss.property.Header;
import com.softtech.kismiss.property.Calculation;
import com.softtech.kismiss.property.Kismiss;
import com.softtech.kismiss.property.Property;

@Kismiss(name = "Employee", columnAutoSize=true)
@Header
@Detail(lineWidth=0.5)
public class Employee { …
    @Property(width = 35, position = 8)
    @Calculation(attribute = "capacity", calculationType = CalculationType.Sum)
    public int getCapacity() {
      return capacity;
    } …
}

The report will be shown as :






Kismiss Column Auto Size

We will try to generate employee report by using columnAutoSize.
The first we do is set the columnAutoSize to true :

     @Kismiss(name = "Employee", columnAutoSize=true)

See the following report result below :


The width of the property is detected as portion of the report width. This report consume maximum width.

Simple Kismiss Reports

First Kismiss Report


package com.softtech.kismiss.model;
import com.softtech.kismiss.property.Header;
import com.softtech.kismiss.property.Kismiss;
import com.softtech.kismiss.property.Property;

/**
    * @author Kisman Hong
    * test Kismiss Reports
*/
@Kismiss(name = "Employee")
@Header
@Detail(lineWidth=0.5)
public class Employee {

   private Integer id;

   private String lastName;

   private String firstName;

   private String address;

   private String phoneNumber;

   private String postCode;

   private String division;

   private double salary;

   private int capacity;

   @Property(width = 45, position = 4)
   public String getDivision() {
     return division;
   }

   public void setDivision(String division) {
     this.division = division;
   }

   @Property(width = 45, position = 5)

   public String getPhoneNumber() {
     return phoneNumber;
   }

   public void setPhoneNumber(String phoneNumber) {
     this.phoneNumber = phoneNumber;
   }

   @Property(width = 50, position = 6)
   public String getPostCode() {
     return postCode;
   }

   public void setPostCode(String postCode) {
     this.postCode = postCode;
   }

   @Property(width = 100, position = 2)
   public String getAddress() {
     return address;
   }

   public void setAddress(String address) {
     this.address = address;
   }

   @Property(width = 100, position = 0)
   public String getFirstName() {
     return firstName;
   }

   public void setFirstName(String firstName) {
     this.firstName = firstName;
   }

   @Property(width = 100, position = 1)
   public String getLastName() {
     return lastName;
   }

   public void setLastName(String lastName) {
     this.lastName = lastName;
   }

   @Property(width = 45, position = 3)

   public double getSalary() {
     return salary;
   }

   public void setSalary(double salary) {
     this.salary = salary;
   }

   @Property(width = 35, position = 7)
   public int getCapacity() {
     return capacity;
   }

   public void setCapacity(int capacity) {
     this.capacity = capacity;
   }


   public Integer getId() {
     return id;
   }

   public void setId(Integer id) {
     this.id = id;
   }
}

You can see that this class uses standard JavaBean naming conventions for property getter and setter methods, as well as private visibility for the fields. This is a recommended design.


Kismiss-reports properties are defined at JavaBean class. Let's see “Employee.java” for more detail :

  • The characteristic of kismiss is we must define “@Kismiss” and “@Header” and the “@Property” for the first at the class.

  • Kismiss and Header are defined at the top of the class. Attribute name of Kismiss is required.
  • Property is defined at the getter method. (Kismiss choose the getter method for defining property to prevent too many annotations at the fields). Property is used for defining field, width and position are required.

  • Width in property indicate the width of the field and position indicate the sequence of the field from left, it begin from zero.

Now, we will write some java code to fill the report data and generate Employee report :

public class ReportTestEmployee {
   private static List<Name> names = new ArrayList<Name>();
   public static void main(String args[]) throws Exception
   {
      Random random = new Random();
      List<Employee> employees = new ArrayList<Employee>();
      for(int i=0; i < 1000; i++) {
         Employee employee = new Employee();
         String[] name = produceName();
         employee.setAddress("Sudirman Rd., Jakarta Pusat");
         employee.setDivision("Marketing");
         employee.setFirstName(name[0]);
         employee.setLastName(name[1]);
         employee.setPhoneNumber(producePhone());
         employee.setPostCode(producePostCode());
         employee.setSalary(produceSalary());
         employee.setCapacity(random.nextInt(1000));
         employees.add(employee);
      }

      for(int i=0; i < 1000; i++) {
         Employee employee = new Employee();
         String[] name = produceName();
         employee.setAddress("Gajah Mada Rd.");
         employee.setDivision("HRD");
         employee.setFirstName(name[0]);
         employee.setLastName(name[1]);
         employee.setPhoneNumber(producePhone());
         employee.setPostCode(producePostCode());
         employee.setSalary(produceSalary());
         employee.setCapacity(random.nextInt(1000));
         employees.add(employee);
      }

      KismissReport report = KismissReport.getInstance(); //(1)
      HashMap<String, Object> params = new HashMap<String, Object>(); //(2)
      params.put(ReportFactory.TITLE, "TEST KISMISS REPORT"); //(3)
      params.put(ReportFactory.REPORT_NAME, "employeeTest.pdf"); //(4)

      report.generateAnnotatedPdfFiles(Employee.class, employees,     "D:/Personal/Test/", params); //(5)
   }
}









Let us see step by step :

  1. initialize kismiss report instance
  2. declare the report params to be included
  3. defining title of report
  4. give report file name
  5. generate report


The code above is quite simple. We do not need a long code to generate the report. Prepare the data then call the generate method function.

The generateAnnotatedPdfFiles method has four parameters :
  1. the JavaBean class that will be generated, in this example : “Employee.class”.
  2. collection data of class, this is the content of report.
  3. path where the pdf file will be generated.
  4. the params of report, such as title, report file name, etc.



After finishing the following steps above, let us see the report result :





We can prevent overflow by setting the attribute isStretchWithOverflow of “Property”. By default, isStretchWithOverflow is set to true, we can modify to false or change the field to smaller font. When isStretchWithOverflow is false, data of the Address, Salary or Phone Number field will not be shown completely. Let us set isStretchWithOverflow of Address to false, Our “getAddress” method will look like :

     @Property(width = 100, position = 2, isStretchWithOverflow=false)

    public String getAddress() {
       return address;
    }


   The data of Address field do not show completely because of isStretchWithOverflow of “getAddress” method is set to false. We give a fix width for this field. To show the data completely we can set font to be smaller by using “fontSize”. We will set fontSize of the Address, Salary , and Phone Number field. Our “getAddress”, “getSalary”, and “getPhoneNumber” methods will look like :


       @Property(width = 100, position = 2, isStretchWithOverflow=false, fontSize=7)
     public String getAddress() {
       return address;
     }

    @Property(width = 45, position = 3, isStretchWithOverflow=false, fontSize=7)
     public double getSalary() {
       return salary;
     }

    @Property(width = 45, position = 5, isStretchWithOverflow=false, fontSize=6)
     public String getPhoneNumber() {
       return phoneNumber;
     }

Once again, look at report result below :




The data of Address and Phone Number fields are shown completely, the Salary field is not. Data of salary is too long, so we need to increase the width of salary field.
Another thing that can be added to this report is “row number”. We can use “getId” method as row number. By adding row number field, we must increase the position of all the field :

    @Property(name = "No", width = 15, position = 0)
    @RecordNumber
    public Integer getId() {
      return id;
    }

The attribute “@RecordNumber” tell us that the field is used to be row number. Then the result shown like :