Advertisement
Guest User

MainSceneController

a guest
Feb 27th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. package nl.tudelft.oopp.demo.controllers;
  2.  
  3. import javafx.fxml.FXML;
  4. import javafx.fxml.FXMLLoader;
  5. import javafx.scene.Node;
  6. import javafx.scene.Parent;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Alert;
  9. import javafx.scene.control.PasswordField;
  10. import javafx.scene.control.TextField;
  11. import javafx.scene.layout.Region;
  12. import javafx.stage.Stage;
  13. import nl.tudelft.oopp.demo.communication.ServerCommunication;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import java.io.IOException;
  19. import java.math.BigInteger;
  20. import java.security.MessageDigest;
  21. import java.security.NoSuchAlgorithmException;
  22.  
  23. public class MainSceneController {
  24. /**
  25. * Handles clicking the button.
  26. */
  27. public void buttonClicked() {
  28. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  29. alert.setTitle("Quote for you");
  30. alert.setHeaderText(null);
  31. alert.setContentText(ServerCommunication.getQuote());
  32. alert.showAndWait();
  33. }
  34.  
  35. /**
  36. * Redirects to the page showing buttons to buildings and reservation history
  37. * @param event
  38. * @throws IOException
  39. */
  40. public void displayBuildings(javafx.event.ActionEvent event) throws IOException {
  41. FXMLLoader loader = new FXMLLoader(getClass().getResource("/buildingsPage.fxml"));
  42. Parent buildingPageParent = (Parent) loader.load();
  43.  
  44. Scene buildingPageScene = new Scene(buildingPageParent);
  45.  
  46. Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
  47. appStage.setScene(buildingPageScene);
  48. appStage.show();
  49.  
  50. }
  51.  
  52. public void createAccount(javafx.event.ActionEvent event) throws IOException {
  53. FXMLLoader loader = new FXMLLoader(getClass().getResource("/createAccountPage.fxml"));
  54. Parent buildingPageParent = (Parent) loader.load();
  55.  
  56. Scene buildingPageScene = new Scene(buildingPageParent);
  57.  
  58. Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
  59. appStage.setScene(buildingPageScene);
  60. appStage.show();
  61. }
  62.  
  63. public void goToAdmin(javafx.event.ActionEvent event) throws IOException {
  64. FXMLLoader loader = new FXMLLoader(getClass().getResource("/AdminPage.fxml"));
  65. Parent buildingPageParent = (Parent) loader.load();
  66.  
  67. Scene buildingPageScene = new Scene(buildingPageParent);
  68.  
  69. Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
  70. appStage.setScene(buildingPageScene);
  71. appStage.show();
  72. }
  73.  
  74. @FXML
  75. private TextField userField;
  76. @FXML
  77. private PasswordField passwordField;
  78.  
  79. /**
  80. * Hashes password into an encoded MD5 String
  81. * @param input
  82. * @return
  83. */
  84. public static String getMd5(String input) {
  85.  
  86. try {
  87. MessageDigest md = MessageDigest.getInstance("MD5");
  88.  
  89. byte[] messageDigest = md.digest(input.getBytes());
  90.  
  91. BigInteger no = new BigInteger(1, messageDigest);
  92.  
  93. String hashtext = no.toString(16);
  94. while (hashtext.length() < 32) {
  95. hashtext = "0" + hashtext;
  96. }
  97. return hashtext;
  98. } catch (NoSuchAlgorithmException e) {
  99. throw new RuntimeException(e);
  100. }
  101. }
  102.  
  103. /**
  104. * Checks with the userRepository if a user has valid login credentials
  105. * @param event
  106. * @throws IOException
  107. */
  108. public void checkIfUserIsCorrect(javafx.event.ActionEvent event) throws IOException, JSONException {
  109.  
  110. StringBuilder result = new StringBuilder();
  111. String userList = ServerCommunication.getUserList();
  112. ServerCommunication.addBuilding();
  113.  
  114. JSONArray jsonArray = new JSONArray(userList);
  115. for(int i=0; i<jsonArray.length(); i++) {
  116. JSONObject jsonObject = jsonArray.getJSONObject(i);
  117. String user_id = jsonObject.getString("netId");
  118.  
  119. if (user_id.equals(userField.getText())) {
  120. String password = jsonObject.getString("password");
  121. if (password.equals(getMd5(passwordField.getText()))) {
  122. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  123. alert.setTitle(null);
  124. alert.setHeaderText(null);
  125. alert.setContentText("Welcome " + user_id);
  126. alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
  127. alert.showAndWait();
  128. int accountlevel = jsonObject.getInt("level");
  129. if (accountlevel == 1) {
  130. goToAdmin(event);
  131. } else displayBuildings(event);
  132. break;
  133.  
  134.  
  135. }
  136. }
  137.  
  138. if (i == jsonArray.length() - 1) {
  139.  
  140. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  141. alert.setTitle(null);
  142. alert.setHeaderText(null);
  143. alert.setContentText("Login failed. Try again!");
  144. alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
  145. alert.showAndWait();
  146. }
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement