Wednesday, April 20, 2011

MVC, duet: Ubuntu, MySQL, Java, Netbeans, Tomcat, ZK, iReport (Part 5)




Transaction page
View: [ Web Pages/transaksi.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 id="win" border="normal" width="100%" height="100%" title="Transaksi" apply="control.transaksi">
<panel>
<panelchildren>
<grid>
<columns>
<column width="170px"/>
<column width="580px"/>
</columns>
<rows>
<row>
<label value="Cari Transaksi By Keterangan" style="font-weight:bold;color:red"/>
<hbox>
<textbox id="txtCariTransaksi" width="250px" />
<button  id="btnTampilTransaksi" label="Cari" image="../image/cari.png" />
</hbox>
</row>
<row>
<label value="ID"/>
<textbox value="@{win$composer.objekBarisTransaksi.idtransaksi, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" width="50px" readonly="true"/>
</row>
<row>
<label value="Item"/>
<combobox id="cmbItem" model="@{win$composer.listItem, load-after='win.onCreate'}" value="@{win$composer.objekBarisTransaksi.iditem, converter='converter.combobox_value', load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" autocomplete="true">
<comboitem forEach="@{win$composer.listItem}" self="@{each='objekBarisItem'}" label="@{objekBarisItem.namaitem}" value="@{objekBarisItem.iditem}" />
</combobox>
</row>
<row>
<label value="Tanggal"/>
<datebox id="dateTanggal" value="@{win$composer.objekBarisTransaksi.tgltransaksi, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick', converter='converter.tanggal_date'}"  format="dd-MM-yyyy" width="500px"/>
</row>
<row>
<label value="Nominal"/>
<intbox id="intNominal" value="@{win$composer.objekBarisTransaksi.nominal, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" width="500px" />
</row>
<row>
<label value="Keterangan"/>
<textbox id="txtKeterangan" value="@{win$composer.objekBarisTransaksi.keterangan, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" width="500px" maxlength="100"/>
</row>
<row spans="2">
<hbox>
<button id="btnTambah" label="Tambah" image="../image/tambah.png" />
<button id="btnSimpan" label="Simpan" image="../image/simpan.png" />
<button id="btnBatal"  label="Batal" image="../image/batal.png" />
<button id="btnHapus"  label="Hapus" image="../image/hapus.png" />
</hbox>
</row>
</rows>
</grid>
<listbox id="lstTransaksi" rows="10" mold="paging" pageSize="10" pagingPosition="top" style="margin-top:5px" model="@{win$composer.listTransaksi, load-after='win.onCreate, btnTampilTransaksi.onClick, btnSimpan.onClick, btnTambah.onClick, btnBatal.onClick, btnHapus.onClick'}" selectedItem="@{win$composer.objekBarisTransaksi}">
<listhead>
<listheader label="I D" width="100px"/>
<listheader label="Tgl" width="100px"/>
<listheader label="Keterangan" width="400px"/>
</listhead>
<listitem forEach="@{win$composer.listTransaksi}" self="@{each='objekBarisTransaksi'}" value="@{objekBarisTransaksi}" >
<listcell label="@{objekBarisTransaksi.idtransaksi}"/>
<listcell label="@{objekBarisTransaksi.tgltransaksi, converter='converter.tanggal_string'}"/>
<listcell label="@{objekBarisTransaksi.keterangan}"/>
</listitem>
</listbox>
</panelchildren>
</panel>
</window>
</zk>
Controller: [ Sources Package/control/index.java ]
package control;
import dao.Item;
import dao.Transaksi;
import formater.tipedata;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import objek.tombolcrud;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class transaksi extends GenericForwardComposer
{
    private     tombolcrud perilakuTombolCrud = new tombolcrud();
    protected   Button btnBatal;
    protected   Button btnHapus;
    protected   Button btnSimpan;
    protected   Button btnTambah;

    public transaksi()
    {
    }

    static EntityManagerFactory emf = Persistence.createEntityManagerFactory("dao-persistence-pfmPU");
    static EntityManager emTabel    = emf.createEntityManager();
    private     objek.transaksi trans;
    protected   Window  win;

    //Zul Action
    private void bindcrud()
    {
        listTransaksi       = emTabel.createNamedQuery("Transaksi.findAll")
                                .setHint("eclipselink.refresh", "true")
                                .setFirstResult(0)
                                .setMaxResults(20)
                                .getResultList();
        trans           = new objek.transaksi();
        trans.setEm(emTabel);
    }

    private void reset()
    {
        perilakuTombolCrud.setTombolBatal(btnBatal);
        perilakuTombolCrud.setTombolHapus(btnHapus);
        perilakuTombolCrud.setTombolSimpan(btnSimpan);
        perilakuTombolCrud.setTombolTambah(btnTambah);
        perilakuTombolCrud.keadaanAwal();
    }

    public void onCreate$win()
    {

        if (Sessions.getCurrent().getAttribute("userid") == null)
        {
            win.detach();
            win = null;
            execution.sendRedirect("../index.zul", "_top");
            return;
        }
        listItem    = emTabel.createNamedQuery("Item.findAll")
                                .setHint("eclipselink.refresh", "true")
                                .getResultList();
        bindcrud();
        reset();
        perilakuTombolCrud.setObjekFokus(cmbItem);
        perilakuTombolCrud.tambahObjekZul(cmbItem, "Item", true);
        perilakuTombolCrud.tambahObjekZul(dateTanggal, "Tanggal Transaksi", true);
        perilakuTombolCrud.tambahObjekZul(intNominal, "Nominal Transaksi", true);
        perilakuTombolCrud.tambahObjekZul(txtKeterangan, "Keterangan", true);
        perilakuTombolCrud.siapIsi(false);
    }

    public void onClick$btnTampilTransaksi()
    {
        listTransaksi          = emTabel.createNamedQuery("Transaksi.findByKeteranganLike")
                                    .setHint("eclipselink.refresh", "true")
                                    .setParameter("keterangan", "%" + txtCariTransaksi.getValue() + "%")
                                    .getResultList();
        objekBarisTransaksi    = new Transaksi();
    }

    public void onSelect$lstTransaksi()
    {
        perilakuTombolCrud.dataTerpilih();
        perilakuTombolCrud.siapIsi(true);
    }

    //Object Baris
    protected Transaksi objekBarisTransaksi;
    protected Item      objekBarisItem;

    //Zul Object
    protected List      listTransaksi;
    protected List      listItem;
    protected Textbox   txtCariTransaksi;
    protected Textbox   txtKeterangan;
    protected Intbox    intNominal;
    protected Datebox   dateTanggal;
    protected Combobox  cmbItem;
    protected Listbox   lstTransaksi;
 
    //Manipulasi Database
    public void onClick$btnTambah()
    {
        perilakuTombolCrud.tombolTambahKlik();
        Transaksi baru = new Transaksi();
        listTransaksi.add(baru);
        objekBarisTransaksi = (Transaksi) listTransaksi.get(listTransaksi.size()-1);
        objekBarisTransaksi.setIdpengguna(tipedata.keInt(Sessions.getCurrent().getAttribute("id")));
    }

    public void onClick$btnBatal()
    {
        perilakuTombolCrud.tombolBatalKlik();
        listTransaksi.remove(listTransaksi.size()-1);
        objekBarisTransaksi = new Transaksi();
    }

    public void onClick$btnSimpan() throws InterruptedException
    {
        if (perilakuTombolCrud.cekIsi())
        {
            trans.setErr(0);
            trans.setO(objekBarisTransaksi);
            trans.simpan();
            if (trans.getErr() == 0) perilakuTombolCrud.tombolSimpanKlik();
        }
    }

    public void onClick$btnHapus() throws InterruptedException
    {
        trans.setErr(0);
        trans.setO(objekBarisTransaksi);
        trans.hapus();
        if (trans.getErr() == 0)
        {
            listTransaksi.remove(lstTransaksi.getSelectedIndex());
            objekBarisTransaksi = new Transaksi();
            perilakuTombolCrud.tombolHapusKlik();
        }
    }
}

Addition:
1. Add the script
@NamedQuery(name = "Transaksi.findByKeteranganLike", query = "SELECT t FROM Transaksi t WHERE t.keterangan LIKE :keterangan ORDER BY t.tgltransaksi DESC, t.idtransaksi")
to "dao-persistence-pfm/dao/Transaksi.java", then save. Note: other namedquery allow to added with "ORDER BY t.tgltransaksi DESC, t.idtransaksi"
2. Right click in project "dao-persistence-pfm", then choose "Clean and Build".
3. Copy image "cari.png", "tambah.png", "simpan.png", "batal.png", and "hapus.png" to the folder "gambar"
Explanation:
1. On "Web Pages/transaksi.zul" there is

<textbox id="txtKeterangan" value="@{win$composer.objekBarisTransaksi.keterangan, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" width="500px" maxlength="100"/>
in which there is code to deal with "transaksi.java", ie: "@{win$composer.xxx}", in this line is "@{win$composer.objekBarisTransaksi}". In the "transaksi.java" must be declared and made ​​a getter and setter methods for it, as below
//deklarasi
protected Transaksi objekBarisTransaksi;


//getter dan setter
public Transaksi getObjekBarisTransaksi()

{
    return objekBarisTransaksi;
}

public void setObjekBarisTransaksi(Transaksi objekBarisTransaksi)
{
    this.objekBarisTransaksi = objekBarisTransaksi;
}
Therefore, the "transaksi.java" should be added getters and setters for the 4 pieces of objects, namely: objekBarisTransaksi, objekBarisItem, listItem, and listTransaksi by pressing the "alt + insert", select "Getters and Setters ...", then select the fourth object.
On the line there is also a "load-after" which indicates that the textbox will be filled by a value taken from "objekBarisTransaksi.keterangan" after "lstTransaksi.onSelect", "btnTampilTransaksi.onClick" or "btnTambah.onClick".
2. In the "Web Pages / transaksi.zul" is a line

<combobox id="cmbItem" model="@{win$composer.listItem, load-after='win.onCreate'}" value="@{win$composer.objekBarisTransaksi.iditem, converter='converter.combobox_value', load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick'}" autocomplete="true">
<comboitem forEach="@{win$composer.listItem}" self="@{each='objekBarisItem'}" label="@{objekBarisItem.namaitem}" value="@{objekBarisItem.iditem}" /></combobox>
to fill out and link the selected row in the combobox. Combobox "cmbItem" filled with "listItem" exist in "transaksi.java" after the "win" was created (see the "model"), then each line on the "listItem" will be saved into the "objekBarisItem". Text that appears in the combobox according to the existing combobox on the label (in this case objekBarisItem.namaitem. The value (not shown) in accordance with the existing combobox on combobox value (in this case "objekBarisItem.iditem"). By default, returned by combobox to "objekBarisTransaksi.iditem" on "transaksi.java" is the label. However, to restore the value, required converter "converter.combobox_value". On the converter it will catch the label, then the label is looking for value and that value is returned.
3. In the "Web Pages / transaksi.zul" is a line

<datebox id="dateTanggal" value="@{win$composer.objekBarisTransaksi.tgltransaksi, load-after='lstTransaksi.onSelect, btnTampilTransaksi.onClick, btnTambah.onClick', converter='converter.tanggal_date'}"  format="dd-MM-yyyy" width="500px"/>
to fill out and link the selected row into datebox. The text displayed on datebox "dateTanggal" changed its format into date-month-year. The value format is filled out are considered not valid. To restore the correct value, datebox need to use converter "converter.tanggal_date" so that the returned value has a Date data type, not String.
4. In the "Web Pages / transaksi.zul" is a line

<listbox id="lstTransaksi" rows="10" mold="paging" pageSize="10" pagingPosition="top" style="margin-top:5px" model="@{win$composer.listTransaksi, load-after='win.onCreate, btnTampilTransaksi.onClick, btnSimpan.onClick, btnTambah.onClick, btnBatal.onClick'}" selectedItem="@{win$composer.objekBarisTransaksi}">
...
<listitem forEach="@{win$composer.listTransaksi}" self="@{each='objekBarisTransaksi'}" value="@{objekBarisTransaksi}" >
<listcell label="@{objekBarisTransaksi.idtransaksi}"/>
...
</listitem>
</listbox>
to fill out and link the selected row in the listbox. Listbox "lstTransaksi" filled with "listTransaksi" exist in "transaksi.java" after the actions at "load-after" occurs (see the "model"), then each line on the "listTransaksi" will be saved into "objekBarisTransaksi ". Each row value object, in this case "objekBarisTransaksi". To show it can be done through listcell. So, although shown only one column only, but later returned values ​​are objects, not just one column. For listbox display per page, it must be included "mold" (should be "paging"), "rows" and "PageSize". "pagingPosition" is optional. When one row selected, then the listbox will send the value (in the form of objects) to "transaksi.java" (see "selectedItem"). After that, the controls that use "lstTransaksi.onSelect" on "load-after" will feature as listed in the "value" is, for example: "intNominal" that the "value" was written "@{win$composer.objekBarisTransaksi.nominal}" will display the nominal transaction.
5. In "Sources Packages / transaksi.java" is a line

listTransaksi = emTabel.createNamedQuery( "Transaksi.findAll" )
          .setHint("eclipselink.refresh", "true")
          .setFirstResult(0)
          .setMaxResults(20)
          .getResultList();
to display the results starting from "FirstResult", as "MaxResults" line. This line is to replace the "limit " that can not be used to namedquery, because the form "select t from transactions t WHERE t.keterangan =: description limit 0, 20" are not allowed.
6. In "Sources Packages / transaksi.java" is a line

listTransaksi = emTabel.createNamedQuery( "Transaksi.findByKeteranganLike" )
       .setHint("eclipselink.refresh", "true")
       .setParameter("keterangan", "%" + txtCariTransaksi.getValue() + "%")
       .getResultList();
to display results that match the pattern. In the parameter "description" using the pattern %value%. This line is to deal with "like" that can not be directly used in the namedquery, because the form "select t from transactions t WHERE t.keterangan = '%:description%'" is not allowed.
7. In "Sources Packages / transaksi.java" there is method "reset" that contains code to register the "btnBatal", "btnTambah", "btnSimpan", and "btnHapus" to the object and the method perilakuTombolCrud "OnCreate $ win" there is code to register other controls to the object perilakuTombolCrud, so that will summarize and simplify writing of code settings. Ease those found on the "reset", "OnCreate$win", "onSelect$lstTransaksi", "onClick$btnTambah", "onClick$btnBatal", "onClick$btnHapus", and "onClick$btnSimpan".
8. To make the process of create-update-delete, the existing pattern in the method "onClick$btnTambah", "onClick$btnBatal", "onClick$btnHapus", and "onClick$btnSimpan" is right, it's just that the method "onClick$btnTambah" added

objekBarisTransaksi.setIdpengguna(tipedata.keInt(Sessions.getCurrent().getAttribute("id")));
because the form does not require input "idpengguna". "idpengguna" obtained from the Session when the user entered into the system. Therefore, "idpengguna" should be set from the method "onClick$btnTambah".

No comments:

Post a Comment