Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fbcn.controller;
- import fbcn.Exceptions;
- import fbcn.Utility;
- import fbcn.model.OrderedList;
- import fbcn.model.entity.Company.Company;
- import fbcn.model.entity.Company.CompanyDAO;
- import fbcn.model.entity.User.Admin;
- import fbcn.model.entity.User.Employee;
- import fbcn.model.entity.User.User;
- import fbcn.model.entity.User.UserDAO;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import java.io.IOException;
- import java.sql.Date;
- import java.time.LocalDate;
- public class Controller_UserManager{
- @FXML public Label lblUserInfo;
- @FXML public Label txfResult;
- @FXML public ListView<String> lvData;
- @FXML public ComboBox<String> chbSelectCompnayAdmin;
- @FXML public ToggleButton btnType;
- @FXML public Button btnLoadData;
- @FXML public TextField txfNickname;
- @FXML public TextField txfName;
- @FXML public TextField txfSurname;
- @FXML public TextField txfPassword;
- @FXML public DatePicker dtDate;
- @FXML public RadioButton rbtnUpdate;
- @FXML public RadioButton rbtnDelete;
- @FXML public RadioButton rbtnCreate;
- private User loggedUser;
- private OrderedList<User> orderedList;
- private String updateName;
- public void setUser(User newloggedUser){
- loggedUser = newloggedUser;
- String text = "Logged in as: " + this.loggedUser.getNickname();
- lblUserInfo.setText(text);
- Utility.setTextCompanyComboBox(chbSelectCompnayAdmin);
- txfResult.setTextFill(Color.GREEN);
- txfResult.setVisible(false);
- dtDate.setValue(LocalDate.now());
- }
- @FXML private void usrBtnOnHandlerMakeAction() throws Exception {
- UserDAO userManager = new UserDAO();
- if(rbtnDelete.isSelected()) {
- usrDeleteUser(userManager);
- }
- else if(usrCcheckIfAreNull()){
- if(rbtnCreate.isSelected()){
- usrCreateUser(userManager);
- usrClearInsets();
- usrSetOperationExit(true);
- }
- else if(rbtnUpdate.isSelected()){
- usrUpdateUser(userManager);
- usrClearInsets();
- usrSetOperationExit(true);
- usrSetEditable(false);
- }
- }
- }
- @FXML public void usrBtnOnChnageTypeHandler(){
- if(btnType.isSelected()){
- btnType.setText("admin");
- }
- else {
- btnType.setText("user");
- }
- }
- @FXML public void usrBtnloadData() throws Exception {
- UserDAO userDAO = new UserDAO();
- orderedList = userDAO.findByIdCompany(loggedUser.getCompany().getId(),loggedUser.getId());
- for (int i = 0;i<orderedList.size();i++){
- User user = orderedList.get(i);
- lvData.getItems().add("Nickname: "+user.getNickname()+"\n\tCompany : "+ new CompanyDAO().findById(user.getCompany().getId()).get(0).getName());
- chbSelectCompnayAdmin.getSelectionModel().select(user.getCompany().getId()-1);
- }
- btnLoadData.setDisable(true);
- }
- /**
- * This mehtod loads data to the listview
- * @throws Exception
- */
- @FXML private void usrLvDataMouseClick() throws Exceptions.exceptionNullDate{
- User user = orderedList.get(lvData.getSelectionModel().getSelectedIndex());
- lblUserInfo.setDisable(true);
- txfNickname.setText(user.getNickname());
- txfPassword.setText(user.getPassword());
- txfSurname.setText(user.getSecondname());
- try{
- if(user.getBirthday() == null){
- dtDate.setValue(LocalDate.now());
- throw new Exceptions.exceptionNullDate();
- }
- else dtDate.setValue(user.getBirthday().toLocalDate());
- }catch(Exceptions.exceptionNullDate e){
- System.err.println(e.getMessage());
- }
- if(rbtnUpdate.isSelected()) {
- updateName = txfNickname.getText();
- usrSetEditable(true);
- }
- usrReloadScreen(false);
- }
- public void usrCreateUser( UserDAO userManager) throws Exception {
- if(Utility.checkIfUsernameExists(txfNickname.getText())){
- if(lvData.getItems() == null){dtDate.setValue(LocalDate.now());}
- CompanyDAO companyManager = new CompanyDAO();
- try {
- if (btnType.isSelected()&&usrCcheckIfAreNull()){
- userManager.insert(new Admin(0, txfNickname.getText(), txfPassword.getText(),txfName.getText(),txfSurname.getText(),Date.valueOf(dtDate.getValue()), companyManager.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem())));
- } else if ((!btnType.isSelected()&&usrCcheckIfAreNull())){
- userManager.insert(new Employee(0, txfNickname.getText(), txfPassword.getText(), txfName.getText(), txfSurname.getText(), Date.valueOf(dtDate.getValue()), companyManager.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem())));
- }
- }catch(Exceptions.exceptionUnableToInsertData e) {
- e.printStackTrace();
- System.err.println(e.getMessage());
- }
- }
- }
- public void usrUpdateUser( UserDAO userManager) throws Exception {
- if(lvData.getItems() == null){dtDate.setValue(LocalDate.now());}
- try {
- User user =orderedList.get(lvData.getSelectionModel().getSelectedIndex());
- if (btnType.isSelected()&&usrCcheckIfAreNull()){
- user.setType("admin");
- } else if ((!btnType.isSelected())&&btnType.isSelected()&&usrCcheckIfAreNull()){
- user.setType("user");
- }
- user.getId();
- user.setNickname(txfNickname.getText());
- user.setPassword(txfPassword.getText());
- user.setName(txfName.getText());
- user.setSecondname(txfSurname.getText());
- user.setBirthday(Date.valueOf(dtDate.getValue()));
- CompanyDAO companyDAO = new CompanyDAO();
- user.setCompany(companyDAO.findIdByName(chbSelectCompnayAdmin.getSelectionModel().getSelectedItem()));
- userManager.update(user);
- }catch(Exceptions.exceptionUnableToUpdateData e) {
- e.printStackTrace();
- System.err.println(e.getMessage());
- }
- lvData.getItems().clear();
- btnLoadData.setVisible(true);
- usrSetOperationExit(true);
- }
- public void usrDeleteUser(UserDAO userManager)throws Exception{
- User user = orderedList.get(lvData.getSelectionModel().getSelectedIndex());
- System.out.println(user.getNickname());
- userManager.delete(user);
- usrReloadScreen(true);
- usrLvDataMouseClick();
- usrSetOperationExit(true);
- lvData.getItems().clear();
- btnLoadData.setVisible(true);
- }
- public boolean usrCcheckIfAreNull(){
- try{
- if(txfNickname.getText().isEmpty()||txfPassword.getText().isEmpty()){
- throw new Exceptions.exceptionNullInsertUsers();
- }
- }catch (Exceptions.exceptionNullInsertUsers e){
- e.printStackTrace();
- System.err.println(e.getMessage());
- txfResult.setTextFill(Color.RED);
- txfResult.setText(e.getMessage());
- txfResult.setVisible(true);
- return false;
- }
- return true;
- }
- @FXML private void usrRbOnClickCreate(){
- usrSetEditable(true);
- usrChangeRadioButtons(rbtnCreate);
- usrModifyInterfaceWithRadioButton(false);
- usrClearInsets();
- }
- @FXML private void usrRbOnClickDelete(){
- usrSetEditable(false);
- usrChangeRadioButtons(rbtnDelete);
- usrModifyInterfaceWithRadioButton(true);
- }
- @FXML private void usrRbOnClickUpdate(){
- usrChangeRadioButtons(rbtnUpdate);
- usrSetEditable(false);
- usrModifyInterfaceWithRadioButton(true);
- txfNickname.setDisable(true);
- }
- public void usrSetOperationExit(boolean result){
- if(result){
- txfResult.setVisible(true);
- txfResult.setText("Operation made successfuly!");
- txfResult.setTextFill(Color.GREEN);
- }else{
- txfResult.setVisible(true);
- txfResult.setText("The username you are trying to create already exists.");
- txfResult.setTextFill(Color.RED);
- }
- }
- public void usrChangeRadioButtons(RadioButton button){
- RadioButton [] radioArray = {rbtnDelete,rbtnUpdate,rbtnCreate};
- for (RadioButton x: radioArray) {
- if(button.equals(x)) x.setSelected(true);
- else x.setSelected(false);
- }
- }
- public void usrModifyInterfaceWithRadioButton(Boolean x){
- if(x){
- usrViewcontrolListView(true);
- usrReloadScreen(true);
- txfResult.setVisible(false);
- usrClearInsets();
- }else {
- usrReloadScreen(false);
- usrViewcontrolListView(false);
- }
- txfResult.setVisible(false);
- }
- public void usrViewcontrolListView(Boolean x){
- if(x){
- lvData.setVisible(true);
- lvData.getItems().clear();
- }
- else lvData.setVisible(false);
- }
- public void usrReloadScreen(Boolean disable){
- chbSelectCompnayAdmin.setDisable(disable);
- btnLoadData.setVisible(disable);
- txfNickname.setDisable(disable);
- txfPassword.setDisable(disable);
- txfName.setDisable(disable);
- txfSurname.setDisable(disable);
- dtDate.setDisable(disable);
- btnLoadData.setDisable(false);
- }
- public void usrClearInsets(){
- txfNickname.setText(null);
- txfPassword.setText(null);
- txfResult.setText(null);
- txfSurname.setText(null);
- dtDate.setValue(LocalDate.now());
- }
- public void usrSetEditable(Boolean result){
- txfPassword.setEditable(result);
- txfName.setEditable(result);
- txfSurname.setEditable(result);
- txfNickname.setEditable(result);
- dtDate.setEditable(result);
- }
- @FXML public void lblExit() throws IOException {
- Utility utility = new Utility();
- utility.openControllerManager(loggedUser,btnLoadData);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement