<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package projectguru.controllers;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Callback;
import projectguru.entities.Privileges;

import projectguru.entities.User;
import projectguru.handlers.LoggedUser;
import projectguru.handlers.exceptions.EntityDoesNotExistException;
import projectguru.handlers.exceptions.StoringException;
import projectguru.utils.FormLoader;

/**
 * FXML Controller class
 *
 * @author ZM
 */
public class FormUserAccountsController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private PasswordField pfConfirmPassword;

    @FXML
    private ComboBox<ApplicationPrivileges> cbPrivileges;

    @FXML
    private Button btnAddUser;

    @FXML
    private TextField tfLastName;

    @FXML
    private Button btnEdit;

    @FXML
    private TextField tfUsername;

    @FXML
    private TextField tfFirstName;

    @FXML
    private PasswordField pfPassword;

    @FXML
    private TextField tfSearch;

    @FXML
    private ListView<UserWrapper> listViewUsers;

    @FXML
    private CheckBox checkAccountActive;

    @FXML
    private Button btnExit;

    @FXML
    private Button btnCleanFields;

    /**
     *
     * Moje varijable
     */
    private LoggedUser user;
    private ObservableList<UserWrapper> users;
    private ObservableList<ApplicationPrivileges> privileges;

    @FXML
    void btnAddUserPressed(ActionEvent event) {
        if (checkFields() == true) {
            addUser();
        }
    }

    @FXML
    void btnEditUserPressed(ActionEvent event) {
        if (checkFields() == true) {
            editUser();
        }
    }

    @FXML
    void btnExitPressed(ActionEvent event) {
        Stage stage = (Stage) btnExit.getScene().getWindow();
        stage.close();
    }

    @FXML
    void btnCleanFieldsPressed(ActionEvent event) {
        tfUsername.setText("");
        pfPassword.setText("");
        pfConfirmPassword.setText("");
        cbPrivileges.setValue(getItemWithPrivileges(0));
        checkAccountActive.setSelected(true);
        tfFirstName.setText("");
        tfLastName.setText("");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        btnAddUser.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/add_user.png"))));

        btnEdit.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/edit_user.png"))));

        btnCleanFields.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/clean.png"))));

        btnExit.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/close.png"))));
        
        listViewUsers.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<UserWrapper>() {
            @Override
            public void changed(ObservableValue<? extends UserWrapper> observable, UserWrapper oldValue, UserWrapper newValue) {
                if (newValue != null) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            changeFields(newValue);
                        }
                    });
                }
            }
        });
        tfSearch.textProperty().addListener(new ChangeListener() {
            public void changed(ObservableValue observable, Object oldVal,
                    Object newVal) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        searchUsers((String) oldVal, (String) newVal);
                    }
                });
            }
        });

        cbPrivileges.setCellFactory(new Callback<ListView<ApplicationPrivileges>, ListCell<ApplicationPrivileges>>() {
            @Override
            public ListCell<ApplicationPrivileges> call(ListView<ApplicationPrivileges> p) {
                return new ListCell<ApplicationPrivileges>() {
                    @Override
                    protected void updateItem(ApplicationPrivileges item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item == null || empty) {
                            setText(null);
                        } else {
                            setText(item.getName());
                        }
                    }
                };
            }
        });

        privileges = FXCollections.observableArrayList();
        privileges.add(new ApplicationPrivileges("Обичан корисник", Privileges.NO_PRIVILEGES.ordinal()));
        //privileges.add(new ApplicationPrivileges("Екстерни корисник", Privileges.EXTERN.ordinal()));
        // privileges.add(new ApplicationPrivileges("Надзор", Privileges.INSIGHT.ordinal()));
        //privileges.add(new ApplicationPrivileges("Члан", Privileges.MEMBER.ordinal()));
        privileges.add(new ApplicationPrivileges("Шеф пројекта", Privileges.CHEF.ordinal()));
        privileges.add(new ApplicationPrivileges("Администратор", Privileges.ADMIN.ordinal()));

        cbPrivileges.setItems(privileges);
    }

    public void setUser(LoggedUser user) {
        this.user = user;
        loadUsers();
    }

    private void loadUsers() {
        users = FXCollections.observableArrayList(
                user.getUserHandler().getAllUsers()
                .stream()
                .map((member) -> new UserWrapper(member))
                .collect(Collectors.toList()));
        listViewUsers.setItems(users);
    }

    private void changeFields(UserWrapper userWrapper) {

        User user = userWrapper.getUser();

        tfUsername.setText(user.getUsername());
        pfPassword.setText(user.getPassword());
        pfConfirmPassword.setText(user.getPassword());
        checkAccountActive.setSelected(user.getActivated());<commit_msg>Form za rad sa korisnickim nalozima kombo box<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package projectguru.controllers;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Callback;
import projectguru.entities.Privileges;

import projectguru.entities.User;
import projectguru.handlers.LoggedUser;
import projectguru.handlers.exceptions.EntityDoesNotExistException;
import projectguru.handlers.exceptions.StoringException;
import projectguru.utils.FormLoader;

/**
 * FXML Controller class
 *
 * @author ZM
 */
public class FormUserAccountsController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private PasswordField pfConfirmPassword;

    @FXML
    private ComboBox<ApplicationPrivileges> cbPrivileges;

    @FXML
    private Button btnAddUser;

    @FXML
    private TextField tfLastName;

    @FXML
    private Button btnEdit;

    @FXML
    private TextField tfUsername;

    @FXML
    private TextField tfFirstName;

    @FXML
    private PasswordField pfPassword;

    @FXML
    private TextField tfSearch;

    @FXML
    private ListView<UserWrapper> listViewUsers;

    @FXML
    private CheckBox checkAccountActive;

    @FXML
    private Button btnExit;

    @FXML
    private Button btnCleanFields;

    /**
     *
     * Moje varijable
     */
    private LoggedUser user;
    private ObservableList<UserWrapper> users;
    private ObservableList<ApplicationPrivileges> privileges;

    @FXML
    void btnAddUserPressed(ActionEvent event) {
        if (checkFields() == true) {
            addUser();
        }
    }

    @FXML
    void btnEditUserPressed(ActionEvent event) {
        if (checkFields() == true) {
            editUser();
        }
    }

    @FXML
    void btnExitPressed(ActionEvent event) {
        Stage stage = (Stage) btnExit.getScene().getWindow();
        stage.close();
    }

    @FXML
    void btnCleanFieldsPressed(ActionEvent event) {
        tfUsername.setText("");
        pfPassword.setText("");
        pfConfirmPassword.setText("");
        cbPrivileges.setValue(getItemWithPrivileges(0));
        checkAccountActive.setSelected(true);
        tfFirstName.setText("");
        tfLastName.setText("");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        btnAddUser.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/add_user.png"))));

        btnEdit.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/edit_user.png"))));

        btnCleanFields.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/clean.png"))));

        btnExit.setGraphic(new ImageView(new Image(FormUserAccountsController.class
                .getResourceAsStream("/projectguru/images/close.png"))));
        
        listViewUsers.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<UserWrapper>() {
            @Override
            public void changed(ObservableValue<? extends UserWrapper> observable, UserWrapper oldValue, UserWrapper newValue) {
                if (newValue != null) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            changeFields(newValue);
                        }
                    });
                }
            }
        });
        tfSearch.textProperty().addListener(new ChangeListener() {
            public void changed(ObservableValue observable, Object oldVal,
                    Object newVal) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        searchUsers((String) oldVal, (String) newVal);
                    }
                });
            }
        });

        cbPrivileges.setCellFactory(new Callback<ListView<ApplicationPrivileges>, ListCell<ApplicationPrivileges>>() {
            @Override
            public ListCell<ApplicationPrivileges> call(ListView<ApplicationPrivileges> p) {
                return new ListCell<ApplicationPrivileges>() {
                    @Override
                    protected void updateItem(ApplicationPrivileges item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item == null || empty) {
                            setText(null);
                        } else {
                            setText(item.getName());
                        }
                    }
                };
            }
        });

        privileges = FXCollections.observableArrayList();
        privileges.add(new ApplicationPrivileges("Обичан корисник", Privileges.NO_PRIVILEGES.ordinal()));
        privileges.add(new ApplicationPrivileges("Екстерни корисник", Privileges.EXTERN.ordinal()));
        privileges.add(new ApplicationPrivileges("Надзор", Privileges.INSIGHT.ordinal()));
        privileges.add(new ApplicationPrivileges("Члан", Privileges.MEMBER.ordinal()));
        privileges.add(new ApplicationPrivileges("Шеф пројекта", Privileges.CHEF.ordinal()));
        privileges.add(new ApplicationPrivileges("Администратор", Privileges.ADMIN.ordinal()));

        cbPrivileges.setItems(privileges);
    }

    public void setUser(LoggedUser user) {
        this.user = user;
        loadUsers();
    }

    private void loadUsers() {
        users = FXCollections.observableArrayList(
                user.getUserHandler().getAllUsers()
                .stream()
                .map((member) -> new UserWrapper(member))
                .collect(Collectors.toList()));
        listViewUsers.setItems(users);
    }

    private void changeFields(UserWrapper userWrapper) {

        User user = userWrapper.getUser();

        tfUsername.setText(user.getUsername());
        pfPassword.setText(user.getPassword());
        pfConfirmPassword.setText(user.getPassword());
        checkAccountActive.setSelected(user.getActivated());
{'fraction_non_alphanumeric': 0.09614975537119762, 'fraction_numerical': 0.00014181379848259235, 'mean_word_length': 2.5110779188449093, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '372113', 'n_tokens_mistral': 3854, 'n_tokens_neox': 3727, 'n_words': 704}