You are on page 1of 4

/*

* GiroEditAction.java
* Part of project Vega
*
* Author: Tantowi Mustofa. tantowi@spjbaut.com
*/
package com.tantowi.vega.finance;

import com.tantowi.nebula.DbConnection;
import com.tantowi.nebula.Forms;
import com.tantowi.nebula.HttpNotFoundException;
import com.tantowi.nebula.HttpBadRequestException;
import com.tantowi.vega.VegaAction;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.ServletException;

/**
*
* @author dhanang
*/
public class GiroEditAction extends VegaAction {

int fid = 0;
String fnomor = "";
LocalDate ftanggal = LocalDate.now();
LocalDate fjtempo = LocalDate.now();
int fbank = 0;
String fmtu = "";
String fket = "";
String fkepada = "";
double fjumlah = 0;

@Override
public void doGet() throws ServletException, IOException, SQLException {
try (DbConnection db = this.getDbConnection()) {
String cnomor = (String) request.getAttribute("nomor");
if (cnomor == null || cnomor.isEmpty()) {
throw new HttpNotFoundException("");
}
if (!cnomor.equals("~")) {
db.query("select
id,nomor,tanggal,jtempo,kepada,jumlah,bank_id,keterangan,mtu from finance_giro_req
where nomor=?");
db.setString(1, cnomor);
db.execute();
if (!db.next()) {
throw new HttpNotFoundException("Nomor Not found" + cnomor);
}
fid = db.getInt(1);
fnomor = db.getString(2);
ftanggal = db.getLocalDate(3);
fjtempo = db.getLocalDate(4);
fkepada = db.getString(5);
fjumlah = db.getDouble(6);
fbank = db.getInt(7);
fket = db.getString(8);
fmtu = db.getString(9);

}
display(db);
}
}

@Override
public void doPost() throws ServletException, IOException, SQLException {
try (DbConnection db = this.getDbConnection()) {
fid = form.getInt("id");
fnomor = form.getString("nomor").toUpperCase();
ftanggal = form.getLocalDate("tanggal");
fjtempo = form.getLocalDate("jtempo");
fkepada = form.getString("kepada");
fbank = form.getInt("bank");
fjumlah = form.getDouble("jumlah");
fmtu = form.getString("mtu");
fket = form.getString("keterangan");
String[] errors = form.getErrors();
if (errors.length > 0) {
for (String fld : errors) {
request.setAttribute(fld + "_status", "has Error");
}
display(db);
return;

String action = form.getString("action").toLowerCase();


boolean rs = false;
if (action.equals("save")) {
rs = doSave(db);
} else if (action.equals("delete")) {
rs = doDelete(db);
} else {
throw new HttpBadRequestException("invalid action :" + action);
}
if (!rs) {
display(db);
return;
}
db.commit();
redirect("./");
}

void display(DbConnection db) throws SQLException, IOException, ServletException


{
form.setInt("id", fid);
form.setString("nomor", fnomor);
form.setLocalDate("Tanggal", ftanggal);
form.setLocalDate("jtempo", fjtempo);
String fbankitems = getBankItems(db, fbank);
form.setString("Bank_items", fbankitems);
form.setString("Kepada", fkepada);
form.setString("Keterangan", fket);
String sfmtu = getMataUang(db, "IDR");
form.setString("mtu", sfmtu);
form.setString("jumlah", format(fjumlah));
forwardView("/finance/giroedit.jsp");

boolean doSave(DbConnection db) throws SQLException {


int oid = 0;
db.query("select id from finance_giro_req where id =?");
db.setString(1, fnomor);
db.execute();
if (db.next()) {
oid = db.getInt(1);
}
if (oid != fid && oid != 0) {
request.setAttribute("error message", "Nomor ini sudah ada..");
return false;
}
if (fid == 0) {
db.query("insert into finance_giro_req
(nomor,tanggal,jtempo,kepada,bank,jumlah,mtu,keterangan) vaues (?,?,?,?,?,?,?,?)");
db.setString(1, fnomor);
db.setLocalDate(2, ftanggal);
db.setLocalDate(3, fjtempo);
db.setString(4, fkepada);
db.setInt(5, fbank);
db.setDouble(6, fjumlah);
db.setString(7, fmtu);
db.setString(8, fket);
db.execute();
} else {
db.query("update finance_giro_req set
nomor=?,tanggal=?,jtempo=?,kepada=?,bank=?,jumlah=0,mtu=?,keterangan=? where
id=?");
db.setString(1, fnomor);
db.setLocalDate(2, ftanggal);
db.setLocalDate(3, fjtempo);
db.setString(4, fkepada);
db.setInt(5, fbank);
db.setDouble(6, fjumlah);
db.setString(7, fmtu);
db.setString(8, fket);
db.setInt(9,fid);
db.execute();
}

return true;
}

boolean doDelete(DbConnection db) throws SQLException {


try {
db.query("delete from finance_giro_req where id=?");
db.setInt(1, fid);
db.execute();
return true;
} catch (Exception ex) {
request.setAttribute("Error message", "Tidak bisa Menghapus.." + fnomor);
return false;
}

public static String getBankItems(DbConnection db, int def) throws SQLException


{
List<String> ids = new ArrayList<>();
List<String> texts = new ArrayList<>();
String sql = "select id,nomor_rek,bank,cabang from finance_bank order by id";
db.query(sql);
db.execute();
while (db.next()) {
int id = db.getInt(1);
String norek = db.getString(2);
String bank = db.getString(3);
String cabang = db.getString(4);
ids.add(String.valueOf(id));
texts.add(norek + " | " + bank + " | " + cabang + " | ");

}
return Forms.buildSelectItems(ids, texts, String.valueOf(def));
}

public static String getMataUang(DbConnection db, String def) throws


SQLException {
List<String> ids = new ArrayList<>();
List<String> texts = new ArrayList<>();
ids.add("IDR");
ids.add("SGD");
ids.add("USD");
texts.add("Indonesia");
texts.add("Singapore");
texts.add("Amerika");
return Forms.buildSelectItems(ids, texts, def);
}
}

You might also like