Tuesday, April 19, 2011

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

Creating a container for all objects that can be used (result: object-pfm.jar)
Actually at the time of implementation, the project was made ​​in the midst of development, not before making project, but, as it requires a process and the modified source code, it does not need to be shown all the process. Only the last process that is so, it is shown. So, readers do not have a headache, just developers that feel. The explanation will be divided per file. Maybe there is some code that is never used in this tutorial at all, but still displayed because it is important to note and add insight.

The first step
1. Create a new project named "object-PFM".

2. Make three packages, each named "converter", "formater" and "object". Package "coverter" contains the object to convert the value of control in "*. zul" for use in "*. java". Package "formater" contains the object to perform data type conversions and formatting. Package "object" contains objects other than those contained in the other two packages.
3. Add library "ZK363" and "EclipseLink".
Package "converter"
In each object there are 2 pieces of the method that returns a value, namely: "coerceToBean" and "coerceToUi". "coerceToBean" will return the value to "*. java", while "coerceToUi" will return the value to "*. zul".
usage
example:
in "*. zul" -> "dtboxtgl" displays the value returned by "coerceToUi" value based on the "objekBarisTanggal.tgl"
<datebox id="dtboxtgl" value="@{composer$win.objekBarisTanggal.tgl" />

in "*.java" -> "dtboxtgl.getValue()" show value returned by  "coerceToBean"
import org.zkoss.zul.Datebox;
public class tanggal extends GenericForwardComposer
{
private Datebox dtboxtgl;
...
Messagebox.show("" + dtboxtgl.getValue());
...
}

[boolean_ya_string.java] : toBean -> boolean value true/false, toUi -> string "ya"/"tidak"
package converter;
import formater.tipedata;
import org.zkoss.zk.ui.Component;


public class boolean_ya_string implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return tipedata.keBoolean(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return (tipedata.keBoolean(o)) ? "ya" : "tidak";
    }
}
[combobox_value.java] : toBean -> item value (bound text), toUi -> item label
package converter;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;


public class combobox_value implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return ((Combobox) c).getSelectedItem().getValue();
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return objek.combobox.cariLabel((Combobox) c, o);
    }
}
[cuplik_string.java] : toBean -> just what it is, toUi -> 100 first string
package converter;
import org.zkoss.zk.ui.Component;


public class cuplik_string implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return o.toString();
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return (o.toString().length() > 100) ? o.toString().substring(0, 100) + " ..." : o.toString();
    }
}
[int_.java] : toBean dan toUi -> integer value, for other data type, like: short, double, string, dsb, have same format, except for Date data type
package converter;
import formater.tipedata;
import org.zkoss.zk.ui.Component;


public class int_ implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return tipedata.keInt(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return tipedata.keInt(o);
    }
}
[tanggal_date.java] : toBean dan toUi -> Date data type
package converter;
import formater.tipedata;
import java.util.Date;
import java.sql.Timestamp;
import org.zkoss.zk.ui.Component;


public class tanggal_date implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return tipedata.keDate(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        if (o instanceof Timestamp)
        {
            final Timestamp timestamp = (Timestamp) o;
            return new Date(timestamp.getTime());
        }
        else if (o instanceof Date)
        {
            return (Date) o;
        }
        return null;
    }
}
[tanggal_string.java] : toBean dan toUi -> date string
package converter;
import formater.format;
import java.util.Date;
import java.sql.Timestamp;
import org.zkoss.zk.ui.Component;


public class tanggal_string implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return format.FormatTanggal(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        Date tgl = null;
        if (o instanceof Timestamp)
        {
            final Timestamp timestamp = (Timestamp) o;
            tgl = new Date(timestamp.getTime());
        }
        else if (o instanceof Date)
        {
            tgl = (Date) o;
        }
        return format.FormatTanggal(tgl);
    }
}
[terbilang_string.java] : toBean -> double value, toUi -> spelled string
package converter;
import formater.format;
import formater.tipedata;
import org.zkoss.zk.ui.Component;


public class terbilang_string implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return tipedata.keDouble(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return format.terbilang(tipedata.keDouble(o));
    }
}
[uang_string.java] : toBean -> double value, toUi -> numbers with thousands separator
package converter;
import formater.format;
import org.zkoss.zk.ui.Component;


public class uang_string implements org.zkoss.zkplus.databind.TypeConverter
{
    @Override
    public Object coerceToBean(Object o, Component c)
    {
        return format.UnFormatUang(o);
    }


    @Override
    public Object coerceToUi(Object o, Component c)
    {
        return format.FormatUang(o);
    }
}
Paket "formater"This package is divided into two objects, namely: object to granting a format that is displayed and the object for conversion from one data type to another data type.

[format.java]
package formater;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class format
{
    //membuat format penulisan
    private static final String[] RCODE = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    private static final int[]    BVAL  = {1000, 900, 500, 400,  100,   90,  50, 40,   10,    9,   5,   4,    1};
    private static String[] bulan                   = new String[] { "", "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember" };
    private static SimpleDateFormat Tanggal         = new SimpleDateFormat("dd-MM-yyyy");
    private static SimpleDateFormat TanggalMysql    = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat Haripekan       = new SimpleDateFormat("dd-MM-yyyy E");
    private static DecimalFormat UangKoma           = new DecimalFormat("#,###.00");
    private static DecimalFormat Uang               = new DecimalFormat("#,###");

    //implementasi
    public static String FormatPeriodeLengkap(Object obj)
    {
        String [] temp = obj.toString().split("-");
        return (obj == null) ? "00 0000" : bulan[tipedata.keInt(temp[1])] + " " + temp[0];
    }

    public static String FormatPeriodeSimpel(Object obj)
    {
        String [] temp = obj.toString().split("-");
        return (obj == null) ? "00 0000" : bulan[tipedata.keInt(temp[1])] + " " + temp[0];
    }

    public static String FormatUang(Object obj)
    {
        return PemisahRibuan(tipedata.keDouble(obj));
    }

    public static String FormatUangKoma(Object obj)
    {
        return PemisahRibuanKoma(tipedata.keDouble(obj));
    }

    public static String FormatTanggal(Object obj)
    {
        return (obj == null) ? "00-00-0000" : Tanggal.format(obj);
    }

    public static String FormatTanggalMysql(Object obj)
    {
        return (obj == null) ? "00-00-0000" : TanggalMysql.format(obj);
    }

    public static String FormatTanggalLengkap(Object obj)
    {
        String [] temp = FormatTanggal(obj).split("-");
        return (obj == null) ? "00 00 0000" : temp[0] + " " + bulan[tipedata.keInt(temp[1])] + " " + temp[2];
    }

    public static String FormatHaripekan(Object obj)
    {
        return (obj == null) ? "00-00-0000" : Haripekan.format(obj);
    }

    public static String FormatHaripekanLengkap(Object obj)
    {
        String [] temp = FormatHaripekan(obj).split("-");
        return (obj == null) ? "00 00 0000" : temp[0] + " " + bulan[tipedata.keInt(temp[1])] + " " + temp[2];
    }

    public static double UnFormatUang(Object obj)
    {
        return tipedata.keDouble(UnPemisahRibuan(obj.toString()));
    }

    public static double UnFormatUangKoma(Object obj)
    {
        return tipedata.keDouble(UnPemisahRibuanKoma(obj.toString()));
    }

    public static String PemisahRibuan(double angka)
    {
        return Uang.format(angka).replace(",", ".");
    }

    public static String PemisahRibuanKoma(double angka)
    {
        return UangKoma.format(angka).replace(",", "*").replace(".", ",").replace("*", ".");
    }

    public static String UnPemisahRibuan(String angka)
    {
        return angka.replace(".", "");
    }

    public static String UnPemisahRibuanKoma(String angka)
    {
        return angka.replace(".", "").replace(",", ".");
    }

    public static String terbilang(double l)
    {
        long L = Double.valueOf(l).longValue();
        int panjang = Long.toString(L).length();
        if (panjang <= 6)
        {
            String[] bil = new String[]{"", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas"};

            if (L <= 0)
            {
                return "";
            }
            else if (L < 12)
            {
                String ret  = bil[(int) L];
                bil         = null;
                return ret + " ";
            }
            else if (L < 20)
            {
                return terbilang(L - 10) + "belas ";
            }
            else if (L < 100)
            {
                return terbilang(L / 10) + "puluh " + terbilang(L % 10);
            }
            else if (L < 200)
            {
                return "seratus " + terbilang(l - 100);
            }
            else if (L < 1000)
            {
                return terbilang(L / 100) + "ratus " + terbilang(L % 100);
            }
            else if (L < 2000)
            {
                return "seribu " + terbilang(L - 1000);
            }
            else
            {
                return terbilang(L / 1000) + "ribu " + terbilang(L % 1000);
            }
        }
        else
        {
            int m = panjang % 3;
            if (m == 0)
            {
                m = 3;
            }
            String s1 = Long.toString(L).substring(0, m);
            String s2 = Long.toString(L).substring(m, panjang);

            String[] bil = new String[]{"", "juta", "milyar", "trilyun", "kuadriliun", "kuintiliun", "heksiliun"};

            if (Integer.valueOf(s1) <= 0)
            {
                return terbilang(Long.valueOf(s2));
            }
            else
            {
                String ret  = bil[(panjang-3)/3];
                bil         = null;
                return terbilang(Long.valueOf(s1)) + ret + " " + terbilang(Long.valueOf(s2));
            }
        }
    }

    public static String FormatRomawi(int angka)
    {
        if (angka <= 0 || angka >= 4000) return "" + angka;
        String roman    = "";
        for (int i = 0; i < RCODE.length; i++) {
            while (angka >= BVAL[i])
            {
                angka   -= BVAL[i];
                roman   += RCODE[i];
            }
        }
        return roman;
    }
}
[tipedata.java] : conversion value to a particular data type, can handle the empty / null.
package formater;
import java.util.Calendar;
import java.util.Date;

public class tipedata
{

    //tipe data: integer
    public static byte keByte(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? (byte) 0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Byte.parseByte(obj.toString()) : ((Number) obj).byteValue());
    }

    public static short keShort(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? (short) 0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Short.parseShort(obj.toString()) : ((Number) obj).shortValue());
    }

    public static int keInt(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? 0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Integer.parseInt(obj.toString()) : ((Number) obj).intValue());
    }

    public static long keLong(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? (long) 0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Long.parseLong(obj.toString()) : ((Number) obj).longValue());
    }

    //tipe data: floating point
    public static double keDouble(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? 0.0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Double.parseDouble(obj.toString()) : ((Number) obj).doubleValue());
    }

    public static float keFloat(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? (float) 0 : ((obj.getClass().getSimpleName().equalsIgnoreCase("string")) ? Float.parseFloat(obj.toString()) : ((Number) obj).floatValue());
    }

    //tipe data: date
    public static Date keDate(Object obj)
    {
        return (keString(obj).equalsIgnoreCase("")) ? tanggalSekarang() : Date.class.cast(obj);
    }

    public static Date tanggalSekarang()
    {
        Calendar tglSekarang = Calendar.getInstance();
        return tglSekarang.getTime();
    }

    //tipe data: string
    public static String keString(Object obj)
    {
        return (obj == null) ? "" : obj.toString();
    }

    //tipe data: boolean
    public static boolean keBoolean(Object obj)
    {
        if (obj == null) return false;
        if (obj.getClass().getSimpleName().equalsIgnoreCase("string"))
            return (keString(obj).equalsIgnoreCase("true")) ? true : false;
        else if(obj.getClass().getSimpleName().equalsIgnoreCase("boolean"))
            return ((Boolean) obj);
        else if(obj.getClass().getSimpleName().equalsIgnoreCase("integer") || obj.getClass().getSimpleName().equalsIgnoreCase("int") || obj.getClass().getSimpleName().equalsIgnoreCase("long") || obj.getClass().getSimpleName().equalsIgnoreCase("byte"))
            return (keInt(obj) == 0) ? false : true;
        else if(obj.getClass().getSimpleName().equalsIgnoreCase("double") || obj.getClass().getSimpleName().equalsIgnoreCase("short"))
            return (keDouble(obj) == 0) ? false : true;
        return false;
    }
}
Paket "objek"
[combobox.java] : to find the value or label on the combobox, based on the value or label
package objek;
import java.util.Iterator;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;


public class combobox
{
    public static String cariLabel(Combobox c, Object o)
    {
        try
        {
            return c.getItemAtIndex(cariIndexByValue(c, o)).getLabel();
        }
        catch (IndexOutOfBoundsException e)
        {
            return "";
        }
    }


    public static Object cariValue(Combobox c, Object o)
    {
        try
        {
            return c.getItemAtIndex(cariIndexByValue(c, o)).getValue();
        }
        catch (IndexOutOfBoundsException e)
        {
            return "";
        }
    }


    public static int cariIndexByValue(Combobox c, Object o)
    {
        Iterator cmbItem = (c).getItems().iterator();
        for (int i=0; cmbItem.hasNext(); i++)
            if (((Comboitem) cmbItem.next()).getValue().equals(o))
            {
                cmbItem = null;
                return i;
            }
        cmbItem = null;
        return -1;
    }


    public static int cariIndexByLabel(Combobox c, Object o)
    {
        Iterator cmbItem = (c).getItems().iterator();
        for (int i=0; cmbItem.hasNext(); i++)
            if (((Comboitem) cmbItem.next()).getLabel().equals(o))
            {
                cmbItem = null;
                return i;
            }
        cmbItem = null;
        return -1;
    }   
}
[md5.java] : implement encryption md5() for user password
package objek;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class md5
{
    protected static byte[]  md5_byte   = new byte[32];


    private static String string_md5()
    {
        StringBuilder buffer = new StringBuilder();
        int byte_separo;
        for (int i = 0; i < md5_byte.length; i++)
        {
            byte_separo = (md5_byte[i] >>> 4) & 0x0F;
            buffer.append(((0 <= byte_separo) && (byte_separo <= 9))?(char)('0' + byte_separo):(char)('a' + (byte_separo - 10)));
            byte_separo = md5_byte[i] & 0x0F;
            buffer.append(((0 <= byte_separo) && (byte_separo <= 9))?(char)('0' + byte_separo):(char)('a' + (byte_separo - 10)));
        }
        return buffer.toString();
    }


    public static String decode(String teks) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
        MessageDigest   md = MessageDigest.getInstance("MD5");
        md.update(teks.getBytes("iso-8859-1"), 0, teks.length());
        md5_byte    = md.digest();
        return string_md5();
    }
}
[tombolcrud.java] : handling behavior when the form button on the save button, change, delete, cancel button and handle other behavior
package objek;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Longbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

public class tombolcrud
{
    private Button          tombolTambah;
    private Button          tombolSimpan;
    private Button          tombolHapus;
    private Button          tombolBatal;
    private LinkedList      objekZul = new LinkedList();
    private Object          objekFokus;

    //getter dan setter
    public void tambahObjekZul(Object objek, String keterangan, boolean isCek)
    {
        objekZul.addLast(Arrays.asList(objek, keterangan, isCek));
    }

    public void hapusObjekZul()
    {
        objekZul.clear();
    }

    public Object getObjekFokus()
    {
        return objekFokus;
    }

    public void setObjekFokus(Object objekFokus)
    {
        this.objekFokus = objekFokus;
    }

    public Button getTombolBatal()
    {
        return tombolBatal;
    }

    public void setTombolBatal(Button tombolBatal)
    {
        this.tombolBatal = tombolBatal;
    }

    public Button getTombolHapus()
    {
        return tombolHapus;
    }

    public void setTombolHapus(Button tombolHapus)
    {
        this.tombolHapus = tombolHapus;
    }

    public Button getTombolSimpan()
    {
        return tombolSimpan;
    }

    public void setTombolSimpan(Button tombolSimpan)
    {
        this.tombolSimpan = tombolSimpan;
    }

    public Button getTombolTambah()
    {
        return tombolTambah;
    }

    public void setTombolTambah(Button tombolTambah)
    {
        this.tombolTambah = tombolTambah;
    }

    //manipulasi status tombol
    public void keadaanAwal()
    {
        tombolTambah.setDisabled(false);
        tombolSimpan.setDisabled(true);
        tombolHapus.setDisabled(true);
        tombolBatal.setDisabled(true);
        tombolTambah.setFocus(true);
    }

    public void dataTerpilih()
    {
        tombolTambah.setDisabled(false);
        tombolSimpan.setDisabled(false);
        tombolHapus.setDisabled(false);
        tombolBatal.setDisabled(true);
    }

    public void tombolTambahKlik()
    {
        tombolTambah.setDisabled(true);
        tombolSimpan.setDisabled(false);
        tombolHapus.setDisabled(true);
        tombolBatal.setDisabled(false);
        siapIsi(true);
    }

    public void tombolSimpanKlik()
    {
        keadaanAwal();
        siapIsi(false);
    }

    public void tombolHapusKlik()
    {
        keadaanAwal();
        siapIsi(false);
    }

    public void tombolBatalKlik()
    {
        keadaanAwal();
        siapIsi(false);
    }

    public void fokus()
    {
        if (objekFokus != null)
        {
            if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("textbox"))
                ((Textbox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("intbox"))
                ((Intbox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("combobox"))
                ((Combobox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("datebox"))
                ((Datebox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("doublebox"))
                ((Doublebox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("listbox"))
                ((Listbox) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("button"))
                ((Button) objekFokus).setFocus(true);
            else if (objekFokus.getClass().getSimpleName().equalsIgnoreCase("checkbox"))
                ((Checkbox) objekFokus).setFocus(true);
        }
    }

    public void fokusIdx(int index)
    {
        if (index > -1 && index < objekZul.size())
        {
            Object oz = ((List) objekZul.get(index)).get(0);
            if (oz != null)
                if (oz.getClass().getSimpleName().equalsIgnoreCase("textbox"))
                    ((Textbox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("intbox"))
                    ((Intbox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("combobox"))
                    ((Combobox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("datebox"))
                    ((Datebox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("doublebox"))
                    ((Doublebox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("listbox"))
                    ((Listbox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("button"))
                    ((Button) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("checkbox"))
                    ((Checkbox) oz).setFocus(true);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("bandbox"))
                    ((Bandbox) oz).setFocus(true);
        }
    }

    public void siapIsi(boolean status)
    {
        Iterator    it;
        List        l;
        for (it = objekZul.iterator(); it.hasNext();)
        {
            l           = (List) it.next();
            Object oz   = l.get(0);
                if (oz.getClass().getSimpleName().equalsIgnoreCase("textbox"))
                    ((Textbox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("intbox"))
                    ((Intbox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("combobox"))
                    ((Combobox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("datebox"))
                    ((Datebox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("doublebox"))
                    ((Doublebox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("longbox"))
                    ((Longbox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("listbox"))
                    ((Listbox) oz).setVisible(status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("button"))
                    ((Button) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("checkbox"))
                    ((Checkbox) oz).setDisabled(!status);
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("bandbox"))
                    ((Bandbox) oz).setDisabled(!status);
            oz      = null;
        }
        if (status) fokus();
    }

    public boolean cekIsi() throws InterruptedException
    {
        Iterator    it;
        List        l;
        for (it = objekZul.iterator(); it.hasNext();)
        {
            l           = (List) it.next();
            if ((Boolean) l.get(2))
            {
                Object oz   = l.get(0);
                if (oz.getClass().getSimpleName().equalsIgnoreCase("textbox"))
                {
                    if (((Textbox) oz).getValue().isEmpty())
                    {
                        Messagebox.show(l.get(1).toString() + " tidak boleh kosong", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Textbox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("intbox"))
                {
                    if (((Intbox) oz).getValue() == null)
                    {
                        Messagebox.show(l.get(1).toString() + " tidak boleh kosong", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Intbox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("combobox"))
                {
                    if (((Combobox) oz).getSelectedIndex() == -1)
                    {
                        Messagebox.show(l.get(1).toString() + " harus ada yang dipilih", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Combobox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("datebox"))
                {
                    if (((Datebox) oz).getValue() == null)
                    {
                        Messagebox.show(l.get(1).toString() + " tidak boleh kosong", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Datebox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("doublebox"))
                {
                    if (((Doublebox) oz).getValue() == null)
                    {
                        Messagebox.show(l.get(1).toString() + " tidak boleh kosong", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Doublebox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("longbox"))
                {
                    if (((Longbox) oz).getValue() == null)
                    {
                        Messagebox.show(l.get(1).toString() + " tidak boleh kosong", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Longbox) oz).setFocus(true);
                        return false;
                    }
                }
                else if (oz.getClass().getSimpleName().equalsIgnoreCase("listbox"))
                {
                    if (((Listbox) oz).getSelectedIndex() == -1)
                    {
                        Messagebox.show(l.get(1).toString() + " harus ada yang dipilih", "Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
                        ((Listbox) oz).setFocus(true);
                        return false;
                    }
                }
                oz      = null;
            }
        }
        return true;
    }

    public boolean konfirmasiHapus(EntityManager em, String namedQuery_cek, String namaParameter_cek, Object nilaiParameter_cek, String tabelRef) throws InterruptedException
    {
        List    listcek;
        listcek = em.createNamedQuery(namedQuery_cek)
                    .setHint("eclipselink.refresh", "true")
                    .setParameter(namaParameter_cek, nilaiParameter_cek)
                    .getResultList();
        if (listcek.isEmpty())
        {
            listcek = null;
            return konfirmasiHapusSimpel();
        }
        else
        {
            listcek = null;
            Messagebox.show("Maaf, record/baris ini sudah digunakan pada [" + tabelRef + "], sehingga tidak dapat dihapus.");
            return false;
        }
    }

    public boolean konfirmasiHapusSimpel() throws InterruptedException
    {
        if (Messagebox.show("Apakah yakin ingin menghapus record/baris ini?", "Konfirmasi penghapusan", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES)
           return true;
        else
            return false;
    }
}
[transaksi.java] : handle persistence behavior during insert transactions, change and delete
package objek;
import javax.persistence.EntityManager;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.zkoss.zul.Messagebox;


public class transaksi
{
    private EntityManager   em;
    private Object          o;
    private Object          om;
    private int             err;
    private String          lasterr;

    //Manage em
    public void reset()
    {
        getEm().clear();
    }

    public void gabung()
    {
        setOm(getEm().merge(getO()));
    }

    public void persiapan()
    {
        setErr(0);
        if(!getEm().getTransaction().isActive()) getEm().getTransaction().begin();
    }

    public void tutup()
    {
        getEm().close();
      
    }
  
    public void eksekusi()
    {
        try
        {
            getEm().flush();
            getEm().getTransaction().commit();
            getEm().refresh(getO());
        }
        catch (DatabaseException ex)
        {
            err++;
            lasterr = ex.getMessage();
        }
        catch (RuntimeException ex)
        {
            err++;
            lasterr = ex.getMessage();
        }
        catch (Throwable t) //menangkap semua error
        {
            err++;
            lasterr = t.getMessage();
        }
    }

    public void eksekusihapus()
    {
        try
        {
            getEm().flush();
            getEm().getTransaction().commit();
        }
        catch (DatabaseException ex)
        {
            err++;
            lasterr = ex.getMessage();
        }
        catch (RuntimeException ex)
        {
            err++;
            lasterr = ex.getMessage();
        }
        catch (Throwable t) //menangkap semua error
        {
            err++;
            lasterr = t.getMessage();
        }
    }
  
    //Manipulasi database - pesimis
    public void update()
    {
        persiapan();
        gabung();
        eksekusi();
        tampilErr();
    }

    public void simpan()
    {
        persiapan();
        getEm().persist(getO());
        eksekusi();
        tampilErr();
    }

    public void hapus()
    {
        persiapan();
        getEm().remove(getO());
        eksekusihapus();
        tampilErr();
    }

    public void gabunghapus()
    {
        persiapan();
        gabung();
        getEm().remove(getOm());
        eksekusihapus();
        tampilErr();
    }

    //Manipulasi database - optimis
    public void simpanoptimis()
    {
        getEm().persist(getO());
        eksekusi();
    }

    public void updateoptimis()
    {
        gabung();
        eksekusi();
    }

    public void hapusoptimis()
    {
        getEm().remove(getO());
        eksekusihapus();
    }

    public void gabunghapusoptimis()
    {
        gabung();
        getEm().remove(getOm());
        eksekusihapus();
    }

    //setter-getter
    public EntityManager getEm()
    {
        return em;
    }

    public void setEm(EntityManager em)
    {
        this.em = em;
    }

    public Object getO()
    {
        return o;
    }

    public void setO(Object o)
    {
        this.o = o;
    }

    public Object getOm()
    {
        return om;
    }

    public void setOm(Object om)
    {
        this.om = om;
    }

    public int getErr()
    {
        return err;
    }

    public void setErr(int err)
    {
        this.err = err;
    }

    private void tampilErr()
    {
        if (err > 0)
        {
            try
            {
                Messagebox.show("Terdapat " + err + " kesalahan yang dijumpai pada operasi. Mohon cek ulang datanya. Operasi dibatalkan. Pesan Kesalahan <<" + lasterr + ">>");
            }
            catch (InterruptedException ex)
            {
            }
        }
    }
}

No comments:

Post a Comment