Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.43 KB | None | 0 0
  1. package fbcn.controller;
  2.  
  3. import fbcn.Exceptions;
  4. import fbcn.Utility;
  5. import fbcn.model.OrderedList;
  6. import fbcn.model.entity.Company.Company;
  7. import fbcn.model.entity.Company.CompanyDAO;
  8. import fbcn.model.entity.User.Admin;
  9. import fbcn.model.entity.User.Employee;
  10. import fbcn.model.entity.User.User;
  11. import fbcn.model.entity.User.UserDAO;
  12. import javafx.fxml.FXML;
  13. import javafx.fxml.FXMLLoader;
  14. import javafx.scene.Parent;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.*;
  17. import javafx.scene.paint.Color;
  18. import javafx.stage.Stage;
  19. import java.io.IOException;
  20. import java.sql.Date;
  21. import java.time.LocalDate;
  22.  
  23.  
  24. public class Controller_UserManager{
  25.  
  26.     @FXML public Label lblUserInfo;
  27.     @FXML public Label txfResult;
  28.     @FXML public ListView<String> lvData;
  29.     @FXML public ComboBox<String> chbSelectCompnayAdmin;
  30.     @FXML public ToggleButton btnType;
  31.     @FXML public Button btnLoadData;
  32.     @FXML public TextField txfNickname;
  33.     @FXML public TextField txfName;
  34.     @FXML public TextField txfSurname;
  35.     @FXML public TextField txfPassword;
  36.     @FXML public DatePicker dtDate;
  37.     @FXML public RadioButton rbtnUpdate;
  38.     @FXML public RadioButton rbtnDelete;
  39.     @FXML public RadioButton rbtnCreate;
  40.  
  41.     private User loggedUser;
  42.     private OrderedList<User> orderedList;
  43.     private String updateName;
  44.  
  45.  
  46.  
  47.     public void setUser(User newloggedUser){
  48.         loggedUser = newloggedUser;
  49.         String text = "Logged in as: " + this.loggedUser.getNickname();
  50.         lblUserInfo.setText(text);
  51.         Utility.setTextCompanyComboBox(chbSelectCompnayAdmin);
  52.         txfResult.setTextFill(Color.GREEN);
  53.         txfResult.setVisible(false);
  54.         dtDate.setValue(LocalDate.now());
  55.     }
  56.  
  57.     @FXML private void usrBtnOnHandlerMakeAction() throws Exception {
  58.         UserDAO userManager = new UserDAO();
  59.         if(rbtnDelete.isSelected()) {
  60.             usrDeleteUser(userManager);
  61.         }
  62.         else if(usrCcheckIfAreNull()){
  63.             if(rbtnCreate.isSelected()){
  64.                 usrCreateUser(userManager);
  65.                 usrClearInsets();
  66.                 usrSetOperationExit(true);
  67.             }
  68.             else if(rbtnUpdate.isSelected()){
  69.                 usrUpdateUser(userManager);
  70.                 usrClearInsets();
  71.                 usrSetOperationExit(true);
  72.                 usrSetEditable(false);
  73.             }
  74.         }
  75.     }
  76.     @FXML public void usrBtnOnChnageTypeHandler(){
  77.         if(btnType.isSelected()){
  78.             btnType.setText("admin");
  79.         }
  80.         else {
  81.             btnType.setText("user");
  82.         }
  83.     }
  84.  
  85.     @FXML public void usrBtnloadData() throws Exception {
  86.         UserDAO userDAO = new UserDAO();
  87.         orderedList = userDAO.findByIdCompany(loggedUser.getCompany().getId(),loggedUser.getId());
  88.         for (int i = 0;i<orderedList.size();i++){
  89.             User user = orderedList.get(i);
  90.             lvData.getItems().add("Nickname: "+user.getNickname()+"\n\tCompany : "+ new CompanyDAO().findById(user.getCompany().getId()).get(0).getName());
  91.             chbSelectCompnayAdmin.getSelectionModel().select(user.getCompany().getId()-1);
  92.         }
  93.         btnLoadData.setDisable(true);
  94.     }
  95.  
  96.     /**
  97.      * This mehtod loads data to the listview
  98.      * @throws Exception
  99.      */
  100.     @FXML private void usrLvDataMouseClick() throws Exceptions.exceptionNullDate{
  101.         User user = orderedList.get(lvData.getSelectionModel().getSelectedIndex());
  102.         lblUserInfo.setDisable(true);
  103.         txfNickname.setText(user.getNickname());
  104.         txfPassword.setText(user.getPassword());
  105.         txfSurname.setText(user.getSecondname());
  106.         try{
  107.             if(user.getBirthday() == null){
  108.                 dtDate.setValue(LocalDate.now());
  109.                 throw new Exceptions.exceptionNullDate();
  110.             }
  111.             else dtDate.setValue(user.getBirthday().toLocalDate());
  112.         }catch(Exceptions.exceptionNullDate e){
  113.             System.err.println(e.getMessage());
  114.         }
  115.  
  116.         if(rbtnUpdate.isSelected()) {
  117.             updateName = txfNickname.getText();
  118.             usrSetEditable(true);
  119.         }
  120.  
  121.         usrReloadScreen(false);
  122.     }
  123.  
  124.     public void usrCreateUser( UserDAO userManager) throws Exception {
  125.         if(Utility.checkIfUsernameExists(txfNickname.getText())){
  126.             if(lvData.getItems() == null){dtDate.setValue(LocalDate.now());}
  127.             CompanyDAO companyManager = new CompanyDAO();
  128.             try {
  129.                 if (btnType.isSelected()&&usrCcheckIfAreNull()){
  130.                     userManager.insert(new Admin(0, txfNickname.getText(), txfPassword.getText(),txfName.getText(),txfSurname.getText(),Date.valueOf(dtDate.getValue()), companyManager.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem())));
  131.                 } else if ((!btnType.isSelected()&&usrCcheckIfAreNull())){
  132.                     userManager.insert(new Employee(0, txfNickname.getText(), txfPassword.getText(), txfName.getText(), txfSurname.getText(), Date.valueOf(dtDate.getValue()), companyManager.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem())));
  133.                 }
  134.             }catch(Exceptions.exceptionUnableToInsertData e) {
  135.                 e.printStackTrace();
  136.                 System.err.println(e.getMessage());
  137.             }
  138.         }
  139.     }
  140.  
  141.     public void usrUpdateUser( UserDAO userManager) throws Exception {
  142.         if(lvData.getItems() == null){dtDate.setValue(LocalDate.now());}
  143.         try {
  144.             User user =orderedList.get(lvData.getSelectionModel().getSelectedIndex());
  145.             if (btnType.isSelected()&&usrCcheckIfAreNull()){
  146.                 user.setType("admin");
  147.             } else if ((!btnType.isSelected())&&btnType.isSelected()&&usrCcheckIfAreNull()){
  148.                 user.setType("user");
  149.             }
  150.             user.getId();
  151.             user.setNickname(txfNickname.getText());
  152.             user.setPassword(txfPassword.getText());
  153.             user.setName(txfName.getText());
  154.             user.setSecondname(txfSurname.getText());
  155.             user.setBirthday(Date.valueOf(dtDate.getValue()));
  156.             CompanyDAO companyDAO = new CompanyDAO();
  157.             user.setCompany(companyDAO.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem()));
  158.             userManager.update(user);
  159.         }catch(Exceptions.exceptionUnableToUpdateData e) {
  160.             e.printStackTrace();
  161.             System.err.println(e.getMessage());
  162.         }
  163.         lvData.getItems().clear();
  164.         btnLoadData.setVisible(true);
  165.         usrSetOperationExit(true);
  166.     }
  167.  
  168.     public void usrDeleteUser(UserDAO userManager)throws Exception{
  169.         User user = orderedList.get(lvData.getSelectionModel().getSelectedIndex());
  170.         System.out.println(user.getNickname());
  171.         userManager.delete(user);
  172.         usrReloadScreen(true);
  173.         usrLvDataMouseClick();
  174.         usrSetOperationExit(true);
  175.         lvData.getItems().clear();
  176.         btnLoadData.setVisible(true);
  177.     }
  178.  
  179.     public boolean usrCcheckIfAreNull(){
  180.         try{
  181.             if(txfNickname.getText().isEmpty()||txfPassword.getText().isEmpty()){
  182.                 throw new Exceptions.exceptionNullInsertUsers();
  183.             }
  184.         }catch (Exceptions.exceptionNullInsertUsers e){
  185.             e.printStackTrace();
  186.             System.err.println(e.getMessage());
  187.             txfResult.setTextFill(Color.RED);
  188.             txfResult.setText(e.getMessage());
  189.             txfResult.setVisible(true);
  190.             return false;
  191.         }
  192.         return  true;
  193.     }
  194.  
  195.     @FXML private void usrRbOnClickCreate(){
  196.         usrSetEditable(true);
  197.         usrChangeRadioButtons(rbtnCreate);
  198.         usrModifyInterfaceWithRadioButton(false);
  199.         usrClearInsets();
  200.     }
  201.  
  202.     @FXML private void usrRbOnClickDelete(){
  203.         usrSetEditable(false);
  204.         usrChangeRadioButtons(rbtnDelete);
  205.         usrModifyInterfaceWithRadioButton(true);
  206.     }
  207.  
  208.     @FXML private void usrRbOnClickUpdate(){
  209.         usrChangeRadioButtons(rbtnUpdate);
  210.         usrSetEditable(false);
  211.         usrModifyInterfaceWithRadioButton(true);
  212.         txfNickname.setDisable(true);
  213.     }
  214.  
  215.     public void usrSetOperationExit(boolean result){
  216.         if(result){
  217.             txfResult.setVisible(true);
  218.             txfResult.setText("Operation made successfuly!");
  219.             txfResult.setTextFill(Color.GREEN);
  220.         }else{
  221.             txfResult.setVisible(true);
  222.             txfResult.setText("The username you are trying to create already exists.");
  223.             txfResult.setTextFill(Color.RED);
  224.         }
  225.     }
  226.  
  227.     public void usrChangeRadioButtons(RadioButton button){
  228.         RadioButton [] radioArray = {rbtnDelete,rbtnUpdate,rbtnCreate};
  229.         for (RadioButton x: radioArray) {
  230.             if(button.equals(x)) x.setSelected(true);
  231.             else x.setSelected(false);
  232.         }
  233.     }
  234.  
  235.     public void usrModifyInterfaceWithRadioButton(Boolean x){
  236.         if(x){
  237.             usrViewcontrolListView(true);
  238.             usrReloadScreen(true);
  239.             txfResult.setVisible(false);
  240.             usrClearInsets();
  241.         }else {
  242.             usrReloadScreen(false);
  243.             usrViewcontrolListView(false);
  244.         }
  245.         txfResult.setVisible(false);
  246.     }
  247.  
  248.     public void usrViewcontrolListView(Boolean x){
  249.         if(x){
  250.             lvData.setVisible(true);
  251.             lvData.getItems().clear();
  252.         }
  253.         else  lvData.setVisible(false);
  254.     }
  255.  
  256.     public void usrReloadScreen(Boolean disable){
  257.         chbSelectCompnayAdmin.setDisable(disable);
  258.         btnLoadData.setVisible(disable);
  259.         txfNickname.setDisable(disable);
  260.         txfPassword.setDisable(disable);
  261.         txfName.setDisable(disable);
  262.         txfSurname.setDisable(disable);
  263.         dtDate.setDisable(disable);
  264.         btnLoadData.setDisable(false);
  265.     }
  266.  
  267.     public void usrClearInsets(){
  268.         txfNickname.setText(null);
  269.         txfPassword.setText(null);
  270.         txfResult.setText(null);
  271.         txfSurname.setText(null);
  272.         dtDate.setValue(LocalDate.now());
  273.     }
  274.  
  275.     public void usrSetEditable(Boolean result){
  276.         txfPassword.setEditable(result);
  277.         txfName.setEditable(result);
  278.         txfSurname.setEditable(result);
  279.         txfNickname.setEditable(result);
  280.         dtDate.setEditable(result);
  281.     }
  282.  
  283.     @FXML public void lblExit() throws IOException {
  284.         Utility utility = new Utility();
  285.         utility.openControllerManager(loggedUser,btnLoadData);
  286.     }
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement