Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package Controllers;
  2.  
  3. import Controllers.Main;
  4. import Models.User;
  5. import Models.DB;
  6. import java.io.IOException;
  7. import java.math.BigInteger;
  8. import java.net.URL;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.ResourceBundle;
  16. import java.util.regex.Pattern;
  17. import javafx.collections.FXCollections;
  18. import javafx.collections.ObservableList;
  19. import javafx.event.ActionEvent;
  20. import javafx.fxml.FXML;
  21. import javafx.fxml.Initializable;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.control.ComboBox;
  24. import javafx.scene.control.Label;
  25. import javafx.scene.control.PasswordField;
  26.  
  27. public class UsersCreate implements Initializable {
  28.    
  29.     @FXML
  30.     private TextField UserNameField;
  31.     @FXML
  32.     private TextField NameField;
  33.     @FXML
  34.     private TextField MailField;
  35.     @FXML
  36.     private ComboBox RoleField;
  37.     @FXML
  38.     private PasswordField PasswordField;
  39.     @FXML
  40.     private PasswordField ConfirmPasswordField;
  41.     @FXML
  42.     private Label ValidationLabel;
  43.    
  44.     @Override
  45.     public void initialize(URL url, ResourceBundle rb){        
  46.         List<String> list = new ArrayList<>();
  47.         list.add("Manager");
  48.         list.add("Servicedesk");
  49.         list.add("Traveler");
  50.         ObservableList obList = FXCollections.observableList(list);
  51.         RoleField.getItems().clear();
  52.         RoleField.setItems(obList);
  53.     }    
  54.    
  55.     /**
  56.      * Function to create/edit a user. After saving redirects back to Users.FXML
  57.      * @throws IOException
  58.      */
  59.     @FXML
  60.     private boolean SaveUser(ActionEvent event) throws IOException, SQLException, NoSuchAlgorithmException{
  61.         System.out.println("Starting to save..");
  62.         //use this variable to check if user can be registered
  63.         boolean passed = true;
  64.         String validationErrors = "";
  65.        
  66.         //nieuwe user aanmaken
  67.         User newUser = new User();
  68.        
  69.         //To String after every getText so it can be checked on being empty
  70.         newUser.setUsername(UserNameField.getText().toString());
  71.         newUser.setName(NameField.getText().toString());
  72.         newUser.setRole(RoleField.getSelectionModel().getSelectedItem().toString());
  73.         newUser.setEmail(MailField.getText().toString());
  74.         newUser.setPassword(PasswordField.getText().toString());
  75.         final String confirmPassword = ConfirmPasswordField.getText().toString();
  76.        
  77.        
  78.         System.out.println("Validation...");
  79.         //values can not be null
  80.         if(!newUser.isValid()){
  81.             ValidationLabel.setText("Fill in all fields");
  82.             return false;
  83.         }
  84.        
  85.         System.out.println("Validating Email...");
  86.         //User needs valid Email
  87.         if(!newUser.hasValidEmail()){
  88.             ValidationLabel.setText("E-mail is invalid");
  89.             return false;
  90.         }
  91.        
  92.         System.out.println("Validating Passwords...");
  93.         //password and confirmpassword has to be same values
  94.         if(newUser.Password() == null ? confirmPassword != null : !newUser.Password().equals(User.HashPassword(confirmPassword))){
  95.             ValidationLabel.setText("Passwords do not match.");
  96.             return false;
  97.         }
  98.        
  99.         if(!newUser.Exists()){
  100.             newUser.Save();
  101.         }else{
  102.             ValidationLabel.setText("User already exists with this username/email");
  103.             return false;
  104.         }
  105.                
  106.         //Navigate back to the list
  107.         Main.GoToScreen("Users.fxml");
  108.         return true;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement