From f402f31d14e7f81cb60ff554032ec2a0ab9fb936 Mon Sep 17 00:00:00 2001 From: Samantha Prestrelski Date: Tue, 5 Dec 2023 02:36:57 -0800 Subject: [PATCH] fix: threaded acc creation and login, error handling offlineUI --- .../code/client/Controllers/Controller.java | 118 +++++++++++------- .../main/java/code/client/Model/Model.java | 26 ++-- .../code/client/View/DetailsAppFrame.java | 6 +- .../java/code/client/View/MealTagStyler.java | 6 +- app/src/main/java/code/client/View/View.java | 9 ++ .../code/server/AccountRequestHandler.java | 7 +- .../code/server/RecipeRequestHandler.java | 9 ++ 7 files changed, 110 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/code/client/Controllers/Controller.java b/app/src/main/java/code/client/Controllers/Controller.java index e00aea5..5bcee69 100644 --- a/app/src/main/java/code/client/Controllers/Controller.java +++ b/app/src/main/java/code/client/Controllers/Controller.java @@ -13,6 +13,7 @@ import java.net.URL; import javafx.animation.*; +import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.geometry.Pos; @@ -116,14 +117,12 @@ private void handleRecipePostButton(ActionEvent event) throws IOException { recipeWriter.writeRecipe(postedRecipe); String recipe = writer.toString(); - // Debugging - // System.out.println("Posting: " + recipe); String response = model.performRecipeRequest("POST", recipe, null); if (response.contains("Offline")) { - + AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again."); } else if (response.contains("Error")) { - + AppAlert.show("Error", "Something went wrong. Please check your inputs and try again."); } } @@ -359,7 +358,6 @@ private void handleDetailedViewListeners() { try { handleRefreshButton(event); } catch (URISyntaxException | IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } }); @@ -460,7 +458,6 @@ private void handleRefreshButton(ActionEvent event) throws URISyntaxException, I new Runnable() { @Override public void run() { - String responseText = model.performChatGPTRequest("GET", mealType, ingredients); Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText); @@ -476,20 +473,6 @@ public void run() { }); thread.start(); - // AppAlert.show("Loading", "Please wait for the recipe to regenerate."); - // Thread.sleep(2000); - // String responseText = model.performChatGPTRequest("GET", mealType, - // ingredients); - // Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText); - // chatGPTrecipe.setAccountId(account.getId()); - // chatGPTrecipe.setImage(model.performDallERequest("GET", - // chatGPTrecipe.getTitle())); - - // // Changes UI to Detailed Recipe Screen - // view.goToDetailedView(chatGPTrecipe, false); - // view.getDetailedView().getRecipeDetailsUI().setEditable(false); - // handleDetailedViewListeners(); - } catch (Exception exception) { AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again."); exception.printStackTrace(); @@ -509,17 +492,42 @@ private void handleCreateAcc(ActionEvent event) { if (username.isEmpty() || password.isEmpty()) { // Display an error message if username or password is empty showErrorPane(grid, "Error. Please provide a username and password."); - } else if (isUsernameTaken(username, password)) { - // Display an error message if the username is already taken - showErrorPane(grid, "Error. This username is already taken. Please choose another one."); } else { - // Continue with account creation logic - System.out.println("Account Created!\nUsername: " + username + "\nPassword: " + password); - model.performAccountRequest("PUT", username, password); - // Show success message - showSuccessPane(grid); - view.goToLoginUI(); + view.goToLoading(); + Thread thread = new Thread( + new Runnable() { + @Override + public void run() { + if (!isUsernameTaken(username, password) + && !view.getMainScene().equals(view.getOfflineUI())) { + // Continue with account creation logic + System.out.println( + "Username not taken!\nUsername: " + username + "\nPassword: " + password); + String response = model.performAccountRequest("PUT", username, password); + // Show success message + if (response.contains("Offline")) { + view.goToOfflineUI(); + } else { + showSuccessPane(grid); + view.goToLoginUI(); + } + + } else { + // Display an error message if the username is already taken + view.goToCreateAcc(); + Platform.runLater(new Runnable() { + @Override + public void run() { + showErrorPane(grid, + "Error. This username is already taken. Please choose another one."); + } + }); + } + } + }); + thread.start(); } + } private void showErrorPane(GridPane grid, String errorMessage) { @@ -552,13 +560,12 @@ private void showSuccessPane(GridPane grid) { private boolean isUsernameTaken(String username, String password) { // Check if the username is already taken - // temporary logic, no database yet String response = model.performAccountRequest("GET", username, password); if (response.contains("Offline")) { view.goToOfflineUI(); + return true; } - // System.out.println("Response for usernameTaken : " + response); - return (response.equals("Username is taken")); + return (!response.equals("Username is not found")); } //////////////////////////////////////// @@ -571,20 +578,39 @@ private void handleLoginButton(ActionEvent event) { // Display an error message if username or password is empty showErrorPane(grid, "Error. Please provide a username and password."); } else { - boolean loginSuccessful = performLogin(username, password); - - if (loginSuccessful) { - showLoginSuccessPane(grid, true); // useless - - goToRecipeList(); - if (!view.getLoginUI().getRememberLogin()) { - clearCredentials(); - } else { - saveCredentials(account); - } - } else { - showLoginSuccessPane(grid, false); - } + view.goToLoading(); + Thread thread = new Thread( + new Runnable() { + @Override + public void run() { + boolean loginSuccessful = performLogin(username, password); + if (loginSuccessful) { + Platform.runLater(new Runnable() { + @Override + public void run() { + goToRecipeList(); + if (!view.getLoginUI().getRememberLogin()) { + clearCredentials(); + } else { + saveCredentials(account); + } + } + }); + + } else { + Platform.runLater(new Runnable() { + @Override + public void run() { + if (!view.getMainScene().equals(view.getOfflineUI())) { + view.goToLoginUI(); + showLoginSuccessPane(grid, false); + } + } + }); + } + } + }); + thread.start(); } } diff --git a/app/src/main/java/code/client/Model/Model.java b/app/src/main/java/code/client/Model/Model.java index 832edc3..70710a7 100644 --- a/app/src/main/java/code/client/Model/Model.java +++ b/app/src/main/java/code/client/Model/Model.java @@ -15,10 +15,12 @@ import java.nio.file.*; import java.net.URLEncoder; import com.mongodb.MongoException; +import com.mongodb.MongoSocketReadException; import com.mongodb.MongoWriteException; public class Model { public String performAccountRequest(String method, String user, String password) { + String response = "Error"; try { String urlString = AppConfig.SERVER_URL + AppConfig.ACCOUNT_PATH; urlString += "?=" + user + ":" + password; @@ -40,23 +42,18 @@ public String performAccountRequest(String method, String user, String password) } BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String response = in.readLine(); + response = in.readLine(); in.close(); - return response; - } catch (MongoWriteException ex) { - ex.printStackTrace(); - return "Duplicate Key Error"; - } catch (MongoException ex) { - ex.printStackTrace(); - return "Server Offline"; } catch (Exception ex) { ex.printStackTrace(); - return "Error: " + ex.getMessage(); + response = "Error: " + ex.getMessage(); } + return response; } public String performRecipeRequest(String method, String recipe, String userId) { // Implement your HTTP request logic here and return the response + String response = "Error"; try { String urlString = AppConfig.SERVER_URL + AppConfig.RECIPE_PATH; if (userId != null) { @@ -76,23 +73,16 @@ public String performRecipeRequest(String method, String recipe, String userId) } BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String response = ""; String line; while ((line = in.readLine()) != null) { response += line + "\n"; } in.close(); - return response; - } catch (MongoWriteException ex) { - ex.printStackTrace(); - return "Duplicate Key Error"; - } catch (MongoException ex) { - ex.printStackTrace(); - return "Server Offline"; } catch (Exception ex) { ex.printStackTrace(); - return "Error: " + ex.getMessage(); + response = "Error: " + ex.getMessage(); } + return response; } public String performChatGPTRequest(String method, String mealType, String ingredients) { diff --git a/app/src/main/java/code/client/View/DetailsAppFrame.java b/app/src/main/java/code/client/View/DetailsAppFrame.java index ad81cc0..51bbc46 100644 --- a/app/src/main/java/code/client/View/DetailsAppFrame.java +++ b/app/src/main/java/code/client/View/DetailsAppFrame.java @@ -27,9 +27,9 @@ public DetailsAppFrame() { detailedUI.setStyle("-fx-background-color: #F0F8FF;"); setupGrowingUI(); - defaultButtonStyle = "-fx-font-style: italic; -fx-background-color: #FFFFFF; -fx-font-weight: bold; -fx-font: 11 arial;"; - onStyle = "-fx-font-style: italic; -fx-background-color: #90EE90; -fx-font-weight: bold; -fx-font: 11 arial;"; - offStyle = "-fx-font-style: italic; -fx-background-color: #FF7377; -fx-font-weight: bold; -fx-font: 11 arial;"; + defaultButtonStyle = "-fx-font: italic 11 arial; -fx-background-color: #FFFFFF; -fx-font-weight: bold;"; + onStyle = "-fx-font: italic 11 arial; -fx-background-color: #90EE90; -fx-font-weight: bold;"; + offStyle = "-fx-font: italic 11 arial; -fx-background-color: #FF7377; -fx-font-weight: bold;"; backToHomeButton = new Button("Back to List"); backToHomeButton.setStyle(defaultButtonStyle); diff --git a/app/src/main/java/code/client/View/MealTagStyler.java b/app/src/main/java/code/client/View/MealTagStyler.java index d27791e..0092e34 100644 --- a/app/src/main/java/code/client/View/MealTagStyler.java +++ b/app/src/main/java/code/client/View/MealTagStyler.java @@ -8,19 +8,19 @@ public static void styleTags(Recipe recipe, Button mealType) { switch (recipe.getMealTag().toLowerCase()) { case "breakfast": mealType.setStyle( - "-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #FF7276; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); + "-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #FF7276; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); mealType.setText("Breakfast"); break; case "lunch": mealType.setStyle( - "-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FFFF; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); + "-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FFFF; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); mealType.setText("Lunch"); break; case "dinner": mealType.setStyle( - "-fx-text-fill: black; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FF00; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); + "-fx-text-fill: #000000; -fx-font: 12 arial; -fx-font-weight: bold; -fx-background-color: #00FF00; -fx-border-width: 0; -fx-background-radius: 150; -fx-pref-width: 100; -fx-pref-height: 50;"); mealType.setText("Dinner"); break; } diff --git a/app/src/main/java/code/client/View/View.java b/app/src/main/java/code/client/View/View.java index 48abeb8..459db44 100644 --- a/app/src/main/java/code/client/View/View.java +++ b/app/src/main/java/code/client/View/View.java @@ -4,6 +4,7 @@ import java.net.URISyntaxException; import code.server.Recipe; +import javafx.scene.Parent; import javafx.scene.Scene; public class View { @@ -26,6 +27,10 @@ public View() throws IOException, URISyntaxException { loadingUI = new LoadingUI(); } + public Parent getMainScene() { + return mainScene.getRoot(); + } + public void setScene(Scene scene) { mainScene = scene; } @@ -59,6 +64,10 @@ public void goToOfflineUI() { mainScene.setRoot(offlineScreen); } + public OfflineUI getOfflineUI() { + return offlineScreen; + } + public RecipeListUI getRecipeButtons() { return home.getRecipeList(); } diff --git a/app/src/main/java/code/server/AccountRequestHandler.java b/app/src/main/java/code/server/AccountRequestHandler.java index 20816cf..c21d217 100644 --- a/app/src/main/java/code/server/AccountRequestHandler.java +++ b/app/src/main/java/code/server/AccountRequestHandler.java @@ -1,6 +1,8 @@ package code.server; +import com.mongodb.MongoSocketReadException; import com.mongodb.MongoTimeoutException; +import com.mongodb.MongoWriteException; import com.sun.net.httpserver.*; import java.io.*; @@ -30,7 +32,10 @@ public void handle(HttpExchange httpExchange) throws IOException { } else { throw new Exception("Not valid request method."); } - } catch (MongoTimeoutException e) { + } catch (MongoWriteException ex) { + ex.printStackTrace(); + response = "Duplicate Key Error"; + } catch (MongoSocketReadException | MongoTimeoutException e) { response = "Server Offline"; } catch (Exception e) { response = "Error"; diff --git a/app/src/main/java/code/server/RecipeRequestHandler.java b/app/src/main/java/code/server/RecipeRequestHandler.java index 0e3f3c1..dafc403 100644 --- a/app/src/main/java/code/server/RecipeRequestHandler.java +++ b/app/src/main/java/code/server/RecipeRequestHandler.java @@ -1,5 +1,7 @@ package code.server; +import com.mongodb.MongoSocketReadException; +import com.mongodb.MongoWriteException; import com.sun.net.httpserver.*; import code.client.Model.RecipeCSVWriter; @@ -31,7 +33,14 @@ public void handle(HttpExchange httpExchange) throws IOException { } else { throw new Exception("Not valid request method."); } + } catch (MongoWriteException ex) { + ex.printStackTrace(); + response = "Duplicate Key Error"; + } catch (MongoSocketReadException ex) { + ex.printStackTrace(); + response = "Server Offline"; } catch (Exception e) { + response = "Error"; System.out.println("An erroneous request"); e.printStackTrace(); }