|
3 | 3 | import javax.swing.SwingUtilities; |
4 | 4 |
|
5 | 5 | import org.pwss.exception.user.LoginException; |
| 6 | +import org.pwss.exception.user.UserExistsLookupException; |
6 | 7 | import org.pwss.model.service.AuthService; |
7 | 8 | import org.pwss.navigation.NavigationEvents; |
8 | 9 | import org.pwss.navigation.Screen; |
9 | | -import org.pwss.view.screen.LoginView; |
| 10 | +import org.pwss.view.screen.LoginScreen; |
10 | 11 |
|
11 | 12 | import com.fasterxml.jackson.core.JsonProcessingException; |
12 | 13 |
|
13 | | -public class LoginPresenter extends BasePresenter<LoginView> { |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | + |
| 16 | +public class LoginPresenter extends BasePresenter<LoginScreen> { |
| 17 | + /** |
| 18 | + * Indicates whether the application is in create user mode (i.e., no users exist yet). |
| 19 | + */ |
| 20 | + private final boolean createUserMode; |
| 21 | + |
14 | 22 | private final AuthService authService; |
15 | 23 |
|
16 | | - public LoginPresenter(LoginView view, AuthService authService) { |
| 24 | + /** |
| 25 | + * Constructs a LoginPresenter with the specified view and authentication service. |
| 26 | + * |
| 27 | + * @param view The LoginView instance to be managed by this presenter. |
| 28 | + * @param authService The AuthService instance for handling authentication operations. |
| 29 | + */ |
| 30 | + public LoginPresenter(LoginScreen view, AuthService authService) { |
17 | 31 | super(view); |
18 | 32 | this.authService = authService; |
| 33 | + this.createUserMode = checkUserExists(); |
19 | 34 | } |
20 | 35 |
|
21 | 36 | @Override |
22 | 37 | protected void initListeners() { |
23 | | - getView().getLoginButton().addActionListener(e -> performLogin()); |
24 | | - getView().getCancelButton().addActionListener(e -> System.exit(0)); |
| 38 | + getScreen().getProceedButton().addActionListener(e -> onProceedButtonClick()); |
| 39 | + getScreen().getCancelButton().addActionListener(e -> System.exit(0)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Checks if any users exist in the system. |
| 44 | + * If no users exist, updates the view to prompt for user creation. |
| 45 | + * |
| 46 | + * @return true if no users exist (create user mode), false otherwise. |
| 47 | + */ |
| 48 | + private boolean checkUserExists() { |
| 49 | + try { |
| 50 | + if (!authService.userExists()) { |
| 51 | + // Notify user that no users exist and they need to create one |
| 52 | + getScreen().showInfo("No user found.\nCreate one by entering a username and password."); |
| 53 | + // Update message label to indicate user creation |
| 54 | + getScreen().setMessage("Create a user for the first login"); |
| 55 | + // Change button text to "Register" |
| 56 | + getScreen().getProceedButton().setText("Register"); |
| 57 | + return true; |
| 58 | + } else { |
| 59 | + // Update message label to indicate normal login |
| 60 | + getScreen().setMessage("Login with your username and password."); |
| 61 | + // Change button text to "Login" |
| 62 | + getScreen().getProceedButton().setText("Login"); |
| 63 | + return false; |
| 64 | + } |
| 65 | + } catch (UserExistsLookupException | ExecutionException e) { |
| 66 | + SwingUtilities.invokeLater(() -> |
| 67 | + screen.showError("Error checking user existence: " + e.getMessage())); |
| 68 | + } catch (InterruptedException e) { |
| 69 | + Thread.currentThread().interrupt(); |
| 70 | + SwingUtilities.invokeLater(() -> |
| 71 | + screen.showError("Operation interrupted: " + e.getMessage())); |
| 72 | + } catch (Exception e) { |
| 73 | + SwingUtilities.invokeLater(() -> |
| 74 | + screen.showError("An unexpected error occurred: " + e.getMessage())); |
| 75 | + } |
| 76 | + throw new IllegalStateException("Failed to determine user existence due to an error."); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Handles the proceed button click event. |
| 81 | + * Validates input and either creates a new user or performs login based on the mode. |
| 82 | + */ |
| 83 | + private void onProceedButtonClick() { |
| 84 | + if (!validateInput()) { |
| 85 | + return; // Input validation failed, do not proceed |
| 86 | + } |
| 87 | + |
| 88 | + if (createUserMode) { |
| 89 | + // Handle user creation logic here and then login |
| 90 | + createUserAndLogin(); |
| 91 | + } else { |
| 92 | + // Handle normal login logic here |
| 93 | + performLogin(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Validates the input fields for username and password. |
| 99 | + * |
| 100 | + * @return true if both fields are non-empty, false otherwise. |
| 101 | + */ |
| 102 | + private boolean validateInput() { |
| 103 | + String username = getScreen().getUsername(); |
| 104 | + String password = getScreen().getPassword(); |
| 105 | + |
| 106 | + if (username == null || username.trim().isEmpty()) { |
| 107 | + getScreen().showError("Username cannot be empty."); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (password == null || password.trim().isEmpty()) { |
| 112 | + getScreen().showError("Password cannot be empty."); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Creates a new user and then performs login if creation is successful. |
| 121 | + */ |
| 122 | + private void createUserAndLogin() { |
| 123 | + if (createUser()) { |
| 124 | + performLogin(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Creates a new user with the provided username and password. |
| 130 | + * |
| 131 | + * @return true if user creation is successful, false otherwise. |
| 132 | + */ |
| 133 | + private boolean createUser() { |
| 134 | + String username = getScreen().getUsername(); |
| 135 | + String password = getScreen().getPassword(); |
| 136 | + |
| 137 | + try { |
| 138 | + return authService.createUser(username, password); |
| 139 | + } catch (JsonProcessingException e) { |
| 140 | + SwingUtilities.invokeLater(() -> |
| 141 | + screen.showError("Error preparing user creation request: " + e.getMessage())); |
| 142 | + return false; |
| 143 | + } catch (Exception e) { |
| 144 | + SwingUtilities.invokeLater(() -> |
| 145 | + screen.showError("An unexpected error occurred: " + e.getMessage())); |
| 146 | + return false; |
| 147 | + } |
25 | 148 | } |
26 | 149 |
|
| 150 | + /** |
| 151 | + * Performs the login operation using the provided username and password. |
| 152 | + * Displays success or error messages based on the outcome. |
| 153 | + */ |
27 | 154 | private void performLogin() { |
28 | | - String username = view.getUsername(); |
29 | | - String password = view.getPassword(); |
| 155 | + String username = getScreen().getUsername(); |
| 156 | + String password = getScreen().getPassword(); |
30 | 157 |
|
31 | 158 | try { |
32 | 159 | boolean loginSuccess = authService.login(username, password); |
33 | 160 |
|
34 | 161 | SwingUtilities.invokeLater(() -> { |
35 | 162 | if (loginSuccess) { |
36 | | - view.showSuccess("Login successful!"); |
| 163 | + if (createUserMode) { |
| 164 | + screen.showInfo("User created and logged in successfully!"); |
| 165 | + } else { |
| 166 | + screen.showInfo("Logged in successfully!"); |
| 167 | + } |
37 | 168 | NavigationEvents.navigateTo(Screen.HOME); |
38 | 169 | } else { |
39 | | - view.showError("Invalid username or password."); |
| 170 | + screen.showError("Invalid username or password."); |
40 | 171 | } |
41 | 172 | }); |
42 | 173 |
|
43 | 174 | } catch (JsonProcessingException e) { |
44 | 175 | SwingUtilities.invokeLater(() -> |
45 | | - view.showError("Error preparing login request: " + e.getMessage())); |
| 176 | + screen.showError("Error preparing login request: " + e.getMessage())); |
46 | 177 | } catch (LoginException e) { |
47 | 178 | SwingUtilities.invokeLater(() -> |
48 | | - view.showError("Login request failed: " + e.getMessage())); |
| 179 | + screen.showError("Login request failed: " + e.getMessage())); |
49 | 180 | } catch (Exception e) { |
50 | 181 | SwingUtilities.invokeLater(() -> |
51 | | - view.showError("An unexpected error occurred: " + e.getMessage())); |
| 182 | + screen.showError("An unexpected error occurred: " + e.getMessage())); |
52 | 183 | } |
53 | 184 | } |
54 | 185 | } |
0 commit comments