Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1.  
  2.     private void login() {
  3.         if (!validateAllInput()) {
  4.             return;
  5.         }
  6.  
  7.         try {
  8.             String username = usernameField.getText();
  9.             String password = String.valueOf(passwordField.getPassword());
  10.            
  11.             UserDao userDao = new UserDaoImpl();
  12.             User user = userDao.findBy(username);
  13.             boolean granted = isGranted(user, password, username);
  14.            
  15.             if (granted) {
  16.                 startMainActivity(user);
  17.             }
  18.         } catch (SQLException e) {
  19.             e.printStackTrace();
  20.             Throwable cause = e.getCause();
  21.             if (cause instanceof ConnectException) {
  22.                 showWarning("Tidak dapat menghubungkan ke database server");
  23.                 return;
  24.             }
  25.             showWarning("Tidak bisa menghubungkan ke database \n\n"
  26.                     + "Error : " + e.getMessage());
  27.         }
  28.     }
  29.  
  30.     private boolean isGranted(User user, String password, String username) {
  31.         if (user == null) {
  32.             showWarning("Tidak menemukan user dengan username : " + username);
  33.             return false;
  34.         }
  35.  
  36.         if (!user.getPassword().equals(password)) {
  37.             showWarning("Password salah");
  38.             return false;
  39.         }
  40.  
  41.         return true;
  42.     }
  43.  
  44.     private void startMainActivity(User user) {
  45.         Session.getInstance().setUser(user);
  46.         MainActivity frame = new MainActivity();
  47.         frame.setVisible(true);
  48.         frame.setLocationRelativeTo(null);
  49.         dispose();
  50.     }
  51.  
  52.     private boolean validateAllInput() {
  53.         return validateUsername() && validatePassword();
  54.     }
  55.  
  56.     private boolean validateUsername() {
  57.         String username = usernameField.getText();
  58.  
  59.         if (username.isEmpty()) {
  60.             showWarning("Username masih kosong");
  61.             return false;
  62.         }
  63.  
  64.         return true;
  65.     }
  66.  
  67.     private boolean validatePassword() {
  68.         String password = String.valueOf(passwordField.getPassword());
  69.  
  70.         if (password.isEmpty()) {
  71.             showWarning("Password masih kosong");
  72.             return false;
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     private void showWarning(String message) {
  79.         JOptionPane.showMessageDialog(this, message, "Gagal",
  80.                 JOptionPane.ERROR_MESSAGE);
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement