You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
6.2 KiB
Java
173 lines
6.2 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package id.amigogroup.posterminal.keamanan;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import id.amigogroup.posterminal.SystemValue;
|
|
import id.amigogroup.posterminal.api.AmigoPosRx;
|
|
import id.amigogroup.posterminal.model.Karyawan;
|
|
import id.amigogroup.posterminal.util.AlertUtil;
|
|
import id.amigogroup.posterminal.util.Fucout;
|
|
import io.reactivex.Observable;
|
|
import io.reactivex.Observer;
|
|
import io.reactivex.disposables.Disposable;
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.ResourceBundle;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import javafx.event.ActionEvent;
|
|
import javafx.fxml.FXML;
|
|
import javafx.fxml.FXMLLoader;
|
|
import javafx.fxml.Initializable;
|
|
import javafx.scene.Parent;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.PasswordField;
|
|
import javafx.scene.control.TextField;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.layout.AnchorPane;
|
|
import javafx.stage.Stage;
|
|
import okhttp3.ResponseBody;
|
|
import retrofit2.HttpException;
|
|
|
|
/**
|
|
* FXML Controller class
|
|
*
|
|
* @author ronal
|
|
*/
|
|
public class FormLoginController implements Initializable {
|
|
|
|
@FXML
|
|
private AnchorPane apMain;
|
|
@FXML
|
|
private TextField fldNamaPengguna;
|
|
@FXML
|
|
private PasswordField fldKataSandi;
|
|
|
|
private final AmigoPosRx posRx = new AmigoPosRx();
|
|
|
|
/**
|
|
* Initializes the controller class.
|
|
*/
|
|
@Override
|
|
public void initialize(URL url, ResourceBundle rb) {
|
|
|
|
}
|
|
|
|
public Stage generateNewWindow(String url) throws IOException {
|
|
Stage stage = new Stage();
|
|
Parent root;
|
|
root = FXMLLoader.load(getClass().getResource(url));
|
|
Scene scene = new Scene(root);
|
|
stage.setTitle(Fucout.APP_TITLE);
|
|
stage.getIcons().add(new Image(Fucout.APP_ICON));
|
|
stage.setScene(scene);
|
|
|
|
return stage;
|
|
}
|
|
|
|
@FXML
|
|
public void btnLoginOnAction(ActionEvent event) {
|
|
if (!fldNamaPengguna.getText().equals("") && !fldKataSandi.getText().equals("")) {
|
|
String nik = fldNamaPengguna.getText();
|
|
|
|
Observable.zip(
|
|
posRx.getKaryawanByNik(fldNamaPengguna.getText()),
|
|
posRx.loginKaryawanByNikPin(nik, fldKataSandi.getText()),
|
|
(Karyawan k, ResponseBody response) -> {
|
|
return new KaryawanLogin(k,response);
|
|
}).subscribe(karyawanLoginObserver);
|
|
} else {
|
|
Alert alert = AlertUtil.getAlertWarning(
|
|
"Data Login Tidak Lengkap",
|
|
"Kolom username dan password harus terisi");
|
|
alert.initOwner(apMain.getScene().getWindow());
|
|
alert.show();
|
|
}
|
|
}
|
|
|
|
Observer<KaryawanLogin> karyawanLoginObserver = new Observer<>() {
|
|
@Override
|
|
public void onSubscribe(Disposable dspsbl) {
|
|
}
|
|
|
|
@Override
|
|
public void onNext(KaryawanLogin k) {
|
|
//kasir, Asko produk, Asko SDM, Pemko
|
|
if (k != null && Fucout.isAllowedLogin(k.karyawan.getIdPekerjaan())) {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
try {
|
|
JsonNode jsonNode = mapper.readTree(k.result.string());
|
|
ObjectNode objectNode = jsonNode.deepCopy();
|
|
|
|
if(objectNode.get("status").asBoolean()){
|
|
SystemValue.karyawanLogin = k.karyawan;
|
|
generateNewWindow("/fxml/FormUtama.fxml").show();
|
|
|
|
Stage thisStage = (Stage) apMain.getScene().getWindow();
|
|
thisStage.hide();
|
|
}
|
|
else{
|
|
Alert alert = AlertUtil.getAlertError(
|
|
AlertUtil.ERROR_KARYAWAN_TIDAK_BOLEH_AKSES_TITLE,
|
|
objectNode.get("message").asText());
|
|
alert.initOwner(apMain.getScene().getWindow());
|
|
alert.show();
|
|
}
|
|
} catch (IOException ex) {
|
|
Alert alert = AlertUtil.getAlertError(
|
|
AlertUtil.ERROR_KARYAWAN_TIDAK_BOLEH_AKSES_TITLE,
|
|
"Terjadi error yang tidak diduga sehingga karyawan tidak boleh mengakses.");
|
|
alert.initOwner((Stage) apMain.getScene().getWindow());
|
|
alert.show();
|
|
|
|
Logger.getLogger(FormLoginController.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
} else {
|
|
Alert alert = AlertUtil.getAlertError(
|
|
AlertUtil.ERROR_KARYAWAN_TIDAK_BOLEH_AKSES_TITLE,
|
|
"Karyawan dengan nomor tersebut tidak berwenang untuk mengakses aplikasi ini.");
|
|
alert.initOwner((Stage) apMain.getScene().getWindow());
|
|
alert.show();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(Throwable error) {
|
|
if (error instanceof HttpException) {
|
|
switch (((HttpException) error).code()) {
|
|
case HttpURLConnection.HTTP_NOT_FOUND:
|
|
Alert alert = AlertUtil.getAlertError(
|
|
AlertUtil.ERROR_TIDAK_DITEMUKAN_TITLE,
|
|
"Karyawan dengan nomor tersebut tidak ditemukan.");
|
|
alert.initOwner(apMain.getScene().getWindow());
|
|
alert.show();
|
|
}
|
|
} else {
|
|
AmigoPosRx.handleGenericError(error, apMain.getScene().getWindow());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
}
|
|
};
|
|
|
|
private class KaryawanLogin{
|
|
private Karyawan karyawan;
|
|
private ResponseBody result;
|
|
|
|
public KaryawanLogin(Karyawan karyawan, ResponseBody result) {
|
|
this.karyawan = karyawan;
|
|
this.result = result;
|
|
}
|
|
}
|
|
}
|