Wednesday, April 20, 2011

MVC, duet: Ubuntu, MySQL, Java, Netbeans, Tomcat, ZK, iReport (part 6)

Laporan Rekap Transaksi - iReport
1. Open explorer, locate project "pfm", open folder "web", then create a folder named "laporan".
2. Open folder "iReport-3.0.0"
3. Right click "open" or just double click on file "iReport.sh", if question appear, choose "execute".
4. After iReport appear, press "ctrl+n". Appear window "Report properties", set option as you wish, don't forget to give a name with "rekap transaksi".


5. Press "ctrl+s" to save the file. File saved in the newest folder that you make (folder "laporan").
6. By default, there is some section in report, there is "title" (only in the first page), "pageHeader" (always appear in every page), "columnHeader", "detail", "columnFooter" (
always appear in every page), "lastPageFooter" (only appear in the last page), dan "summary" (only appear in the last page). There is one special section named "background".
7. In tab "document structure" few category exist: "parameters" (send from outside), "fields", "variables" (formula) and section-section in iReport. All data type handled must be equal in report (example: formula) or from outside report (example: from "*.java"), else error.
8. To add "parameters" or "fields" or "variables", just right click on "document structure", then choose "add..." and choose one.
9. This report is based on query "v_rekap"
in which there is the  "idtransaksi", "idpengguna", "nominal", "bulan", "tahun", "namaitem", "alurkas". Not all columns are displayed. Make a "field" on iReport to "nominal" (integer), "namaitem" (string) and alurkas (boolean). Make a "parameter" with the name "namapengguna" (string) and "periode" (string). Make a "variable" with the name "vnominal" (integer) and fill in the "variable expression" formula
( $F{alurkas}.booleanValue() == true ? $F{nominal} : new java.lang.Integer (-1 * $F{nominal}.intValue()) )
which means if alurkas is true, then the nominal value is positive, other than its nominal value was negative.
10. Eliminate the "title", "columnFooter", "lastPageFooter", and "summary" by making the height to 0 or sealed using a mouse.
11. Enter the "field" "namaitem" and "variable" "vnominal" into the details. Adjust the position according to the will. If an item is selected, then the line will change to orange and there are 8 points that are purple when the item is not colliding with other items. If the items collide, then its point is green. Make 2 pieces of "static text" into "columnHeader" and change the label to "Nama Item" and "Nominal". Right click on "text field" "$V{vnominal}", then select "properties". Appears window "rekap transaksi", select the tab "Font". Change "horizontally align" to "right". Select the tab "text field", then the button "create ...". Appears window "pattern editor", select "number". Change the "decimal places" to 0 and check the "use 1000 separator". Click the "Apply", then close the window "rekap transaksi".
12. Make 2 pieces of "static text" into "pageHeader" and change the label to "Nama Pengguna" and "Periode" in a vertical position. Enter the "parameter" "namapengguna" and "periode" into "pageHeader", positioned according to the label.
13. For best results: do not let the items crash, high item higher than the high point and the common types of letters in the "font" and "pdf font".
14. Press the menu "Build"> "Compile". Open the folder "iReport-3.0.0", open the folder "bin " and copy the "rekap transaksi.jasper" into folder "laporan".



Rekap Transaksi page
View: [ Web Pages/laporan/rekap.zul ]
<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="win" ?>

<zk xmlns="http://www.zkoss.org/2005/zul">
<window  width="800px" id="win" apply="control.rekap">
    <panel zindex="2">
        <panelchildren>
            <grid>
                <columns>
                    <column width="150px"/>
                    <column width="600px"/>
                </columns>
                <rows>
                    <row spans="2" align="center">
                        <label value="Rekap Transaksi"/>
                    </row>
                    <row>
                        <label value="Tahun Transaksi mulai" style="font-weight:bold;color:red"/>
                        <hbox>
                            <intbox id="intMulai" />
                            <label value=" s.d. " />
                            <intbox id="intAkhir" />
                            <button  id="btnTampil1" label="Tampilkan" image="../image/presentasi.png" />
                        </hbox>
                    </row>
                    <row>
                        <label value="Tahun Transaksi dalam" style="font-weight:bold;color:red"/>
                        <hbox>
                            <textbox id="txtTahun" />
                            <label value=" pisah dengan tanda koma, misal: 2010, 2011" />
                            <button  id="btnTampil2" label="Tampilkan" image="../image/presentasi.png" />
                        </hbox>
                    </row>
                </rows>
            </grid>
            <jasperreport id="laporan" type="pdf" height="400px" />
        </panelchildren>
    </panel>
</window>
</zk>
Controller: [ Sources Package/control/index.java ]
package control;
import dao.VRekap;
import formater.tipedata;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class rekap extends GenericForwardComposer
{
    static EntityManagerFactory emf     = Persistence.createEntityManagerFactory("dao-persistence-pfmPU");
    static EntityManager        emTabel = emf.createEntityManager();
    private List                dataList;
    private Jasperreport        laporan;
    private Map                 reportParam;
    protected   Window          win;
    protected   Intbox          intMulai, intAkhir;
    protected   Textbox         txtTahun;
    private     String          mode = "";
    public rekap()
    {
    }
    public void onCreate$win()
    {
        if (Sessions.getCurrent().getAttribute("userid") == null)
        {
            win.detach();
            win = null;
            execution.sendRedirect("../index.zul", "_top");
            return;
        }
        intMulai.setFocus(true);
    }
    public void onClick$btnTampil1() throws Exception
    {
        mode = "diantara";
        tampilkanlaporan();
    }
    public void onClick$btnTampil2() throws Exception
    {
        mode = "dalam";
        tampilkanlaporan();
    }
   
    //Laporan
    public class SumberDataLaporan implements JRDataSource
    {
        private Object[][] data;
        private int index = -1;
        public SumberDataLaporan() throws Exception
        {
            data = setDataToReport();
        }
        @Override
        public boolean next() throws JRException
        {
            index++;
            return (index < data.length);
        }
        @Override
        public Object getFieldValue(JRField field) throws JRException
        {
            Object value = null;
            LinkedList llBuff   = getField();
            for (int i=0; i<llBuff.size(); i++)
                if (llBuff.get(i).equals(field.getName()))
                {
                    value = data[index][i];
                    i = llBuff.size();
                }
            return value;
        }
    }
    public Object[][] setDataToReport() throws Exception
    {
        VRekap  buffer_objek;
        if (mode.equals("diantara"))
            dataList = emTabel.createNamedQuery("VRekap.findByTahunBetween")
                                .setHint("eclipselink.refresh", "true")
                                .setParameter("tahun1", intMulai.getValue())
                                .setParameter("tahun2", intAkhir.getValue())
                                .setParameter("idpengguna", tipedata.keInt(Sessions.getCurrent().getAttribute("id")))
                                .getResultList();
        else
        {
            LinkedList<Integer> periode = new LinkedList<Integer>();
            StringTokenizer st = new StringTokenizer(txtTahun.getValue(),",");
            while(st.hasMoreTokens())
                periode.add(tipedata.keInt(st.nextElement().toString().trim()));
            dataList = emTabel.createNamedQuery("VRekap.findByTahunIn")
                                .setHint("eclipselink.refresh", "true")
                                .setParameter("tahun", periode)
                                .setParameter("idpengguna", tipedata.keInt(Sessions.getCurrent().getAttribute("id")))
                                .getResultList();
        }
        Object[][] xBuffer = new Object[dataList.size()][3];
        int x = 0;
        for (Iterator it = dataList.iterator(); it.hasNext();)
        {
            buffer_objek = (VRekap) it.next();
            xBuffer[x][0]   = buffer_objek.getNamaitem();
            xBuffer[x][1]   = buffer_objek.getNominal();
            xBuffer[x][2]   = tipedata.keBoolean(buffer_objek.getAlurkas());
            x++;
        }
        dataList    = null;
        return xBuffer;
    }
    public LinkedList getField()
    {
        LinkedList ll = new LinkedList();
        ll.add("namaitem");
        ll.add("nominal");
        ll.add("alurkas");
        return ll;
    }
    public void tampilkanlaporan() throws Exception
    {
        laporan.setSrc("/laporan/rekap transaksi.jasper");
        reportParam = new HashMap();
        reportParam.put("namapengguna", Sessions.getCurrent().getAttribute("userid"));
        if (mode.equals("diantara"))
            reportParam.put("periode", intMulai.getValue() + " s.d. " + intAkhir.getValue());
        else
            reportParam.put("periode", txtTahun.getValue());
        laporan.setParameters(reportParam);
        laporan.setDatasource(new SumberDataLaporan());
    }
}
Addition:1. Add the script at:
@NamedQuery(name = "VRekap.findByTahunBetween", query = "SELECT v FROM VRekap v WHERE v.tahun BETWEEN :tahun1 AND :tahun2 AND v.idpengguna = :idpengguna")
and
@NamedQuery(name = "VRekap.findByTahunIn", query = "SELECT v FROM VRekap v WHERE v.tahun IN :tahun AND v.idpengguna = :idpengguna")
on "dao-persistence-pfm/dao/VRekap.java", then save. Penjelasan:
1. On "
Web Pages/laporan/rekap.zul" there is a method "SumberDataLaporan" to sending data from java to jasper.
2. Method "SumberDataLaporan" need the data returned from method "
setDataToReport". On method "setDataToReport" use namedquery  "VRekap.findByTahunBetween" to implement clause "between" and "VRekap.findByTahunIn" to implement clause "in" that need List data type (list/arraylist/linkedlist). Remember! Data type in java and iReport must be equal.
3. Field mapping occur in method "getField". The variable name in java and iReport must be equal.
4. The configuration exist on method "tampilkanlaporan".

No comments:

Post a Comment