Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program developed by Hassan Althaf.
- * Copyright © 2015, Hassan Althaf.
- * Website: http://hassanalthaf.com
- */
- package com.HassanAlthaf.StockControlSystem.View;
- import com.HassanAlthaf.StockControlSystem.Stocks.StockController;
- import com.HassanAlthaf.StockControlSystem.Stocks.StockItem;
- import com.HassanAlthaf.StockControlSystem.Users.User;
- import com.HassanAlthaf.StockControlSystem.Users.UserController;
- import java.io.IOException;
- import java.net.URL;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.ResourceBundle;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.fxml.Initializable;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.Menu;
- import javafx.scene.control.MenuItem;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.TextField;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- /**
- *
- * @author hassan
- */
- public class MainView implements Initializable {
- @FXML
- private Parent mainWindow;
- @FXML
- private AnchorPane main;
- @FXML
- private AnchorPane about;
- @FXML
- private AnchorPane addInventoryItem;
- @FXML
- private AnchorPane listInventory;
- @FXML
- private AnchorPane listUsers;
- private AnchorPane activePage;
- @FXML
- private TextField productNameField;
- @FXML
- private TextField availableQuantityField;
- @FXML
- private TextField unitPriceField;
- @FXML
- private TextField reorderLevelField;
- private final StockController stockController;
- @FXML
- private Text addItemErrors;
- @FXML
- private TableView<StockItem> stockList;
- @FXML
- private TableColumn productNameColumn;
- @FXML
- private TableColumn unitPriceColumn;
- @FXML
- private TableColumn availableQuantityColumn;
- @FXML
- private TableColumn reorderLevelColumn;
- @FXML
- private TableColumn totalValueColumn;
- @FXML
- private TableView<User> usersList;
- @FXML
- private TableColumn usernameColumn;
- @FXML
- private TableColumn rankColumn;
- @FXML
- private Label totalStockValueLabel;
- private ObservableList<StockItem> stockItemData;
- private ObservableList<User> userListData;
- private UserController userController;
- @FXML
- private Menu userMenu;
- @FXML
- private MenuItem addStockMenuItem;
- @FXML
- private Button deleteStockItemButton;
- public MainView() {
- this.stockController = new StockController();
- }
- public void setUserController(UserController userController) {
- this.userController = userController;
- }
- public void show(Parent loginWindow) throws IOException, SQLException {
- Scene scene = new Scene(this.mainWindow);
- Stage stage = new Stage();
- stage.setScene(scene);
- stage.setTitle("Stock Control System");
- stage.setResizable(false);
- stage.show();
- this.updateViewForUser();
- this.initializeStats();
- Stage loginStage = (Stage) loginWindow.getScene().getWindow();
- loginStage.close();
- this.activePage = this.main;
- }
- public void menuClick(ActionEvent event) throws SQLException, IOException {
- Object source = event.getSource();
- String id = null;
- if (source instanceof MenuItem) {
- MenuItem clickedItem = (MenuItem) source;
- id = clickedItem.getId();
- }
- this.authenticateAccess();
- this.updateViewForUser();
- if (id.equals("aboutMenuItem")) {
- this.activePage.setOpacity(0);
- this.about.toFront();
- this.about.setOpacity(1);
- this.activePage = this.about;
- } else if (id.equals("homeMenuItem")) {
- this.activePage.setOpacity(0);
- this.main.toFront();
- this.main.setOpacity(1);
- this.activePage = this.main;
- } else if (id.equals("addStockMenuItem")) {
- this.authenticateAdminRights();
- this.activePage.setOpacity(0);
- this.addInventoryItem.toFront();
- this.addInventoryItem.setOpacity(1);
- this.activePage = this.addInventoryItem;
- } else if (id.equals("listStocksMenuItem")) {
- this.activePage.setOpacity(0);
- this.listInventory.toFront();
- this.populateStocksList();
- this.listInventory.setOpacity(1);
- this.activePage = this.listInventory;
- } else if (id.equals("loadUsersList")) {
- this.authenticateAdminRights();
- this.activePage.setOpacity(0);
- this.listUsers.toFront();
- this.populateUsersList();
- this.listUsers.setOpacity(1);
- this.activePage = this.listUsers;
- }
- }
- public void authenticateAccess() throws IOException, SQLException {
- this.userController.updateLoggedInUser();
- if(this.userController.userIsDisabled()) {
- this.logUserOut();
- }
- }
- public void authenticateAdminRights() throws IOException, SQLException {
- this.userController.updateLoggedInUser();
- if(!this.userController.isAdmin()) {
- this.logUserOut();
- }
- }
- public void addStockItem(ActionEvent event) throws SQLException, IOException {
- this.authenticateAdminRights();
- boolean response = this.stockController.addStockItem(
- this.productNameField.getText(),
- this.availableQuantityField.getText(),
- this.unitPriceField.getText(),
- this.reorderLevelField.getText()
- );
- if (response == false) {
- ArrayList<String> errors = this.stockController.getReturnMessages();
- String text = "";
- for (String line : errors) {
- text = text + line + "\n";
- }
- text = text.substring(0, text.length() - 1);
- this.addItemErrors.setText(text);
- } else {
- this.initializeStats();
- this.addItemErrors.setText("Successfully added!");
- }
- }
- public void populateStocksList() throws IOException, SQLException {
- ArrayList<StockItem> stockItems = null;
- this.authenticateAccess();
- try {
- stockItems = this.stockController.fetchAllStockItems();
- } catch(SQLException exception) {
- System.out.println("Failed");
- }
- this.stockItemData = FXCollections.observableArrayList(stockItems);
- this.productNameColumn.setCellValueFactory(
- new PropertyValueFactory<>("productName")
- );
- this.availableQuantityColumn.setCellValueFactory(
- new PropertyValueFactory<>("availableQuantity")
- );
- this.unitPriceColumn.setCellValueFactory(
- new PropertyValueFactory<>("unitPrice")
- );
- this.reorderLevelColumn.setCellValueFactory(
- new PropertyValueFactory<>("reorderLevel")
- );
- this.totalValueColumn.setCellValueFactory(
- new PropertyValueFactory<>("totalValue")
- );
- this.stockList.setItems(this.stockItemData);
- }
- public void populateUsersList() throws IOException, SQLException {
- this.authenticateAdminRights();
- ArrayList<User> usersList = null;
- try {
- usersList = this.userController.fetchAllUsers();
- } catch(SQLException exception) {
- System.out.println("Failed");
- }
- this.userListData = FXCollections.observableArrayList(usersList);
- this.usernameColumn.setCellValueFactory(
- new PropertyValueFactory<>("username")
- );
- this.rankColumn.setCellValueFactory(
- new PropertyValueFactory<>("rank")
- );
- this.usersList.setItems(this.userListData);
- }
- public void initializeStats() throws IOException, SQLException {
- this.authenticateAccess();
- this.setTotalStockValueLabel();
- }
- public void setTotalStockValueLabel() {
- ArrayList<StockItem> stockItems = null;
- try {
- stockItems = this.stockController.fetchAllStockItems();
- } catch(Exception exception) {
- System.out.println(exception);
- }
- Double totalStockValue = 0.0;
- for(StockItem stockItem : stockItems) {
- totalStockValue = totalStockValue + Double.parseDouble(stockItem.getTotalValue());
- }
- this.totalStockValueLabel.setText(String.format("%.2f", totalStockValue));
- }
- public void editStockItem(ActionEvent event) throws IOException, SQLException {
- this.authenticateAccess();
- if(this.stockList.getSelectionModel().getSelectedItem() != null) {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("EditStockItemDialog.fxml"));
- Parent editStockItemDialog = loader.load();
- EditStockItemDialogView editStockItemDialogView = loader.getController();
- editStockItemDialogView.setStockItemObject(this.stockList.getSelectionModel().getSelectedItem());
- editStockItemDialogView.setStockController(this.stockController);
- editStockItemDialogView.setMainView(this);
- editStockItemDialogView.show();
- }
- }
- public void deleteStockItem(ActionEvent event) throws SQLException, IOException {
- this.authenticateAdminRights();
- if(this.stockList.getSelectionModel().getSelectedItem() != null) {
- this.stockController.removeStockItem(this.stockList.getSelectionModel().getSelectedItem().getID());
- }
- this.initializeStats();
- this.populateStocksList();
- }
- public void logoutUser(ActionEvent event) throws IOException {
- this.logUserOut();
- }
- public void logUserOut() throws IOException {
- this.userController.logout();
- FXMLLoader loader = new FXMLLoader(getClass().getResource("LoginWindow.fxml"));
- Parent loginWindow = loader.load();
- LoginWindowView loginWindowView = loader.getController();
- loginWindowView.show(this.mainWindow);
- }
- public void addUser(ActionEvent event) throws IOException, SQLException {
- this.authenticateAdminRights();
- FXMLLoader loader = new FXMLLoader(getClass().getResource("AddUserDialog.fxml"));
- Parent addUserDialog = loader.load();
- AddUserDialogView addUserDialogView = loader.getController();
- addUserDialogView.setMainView(this);
- addUserDialogView.setUserController(this.userController);
- addUserDialogView.show();
- }
- public void editUser(ActionEvent event) throws IOException, SQLException {
- this.authenticateAdminRights();
- if(this.usersList.getSelectionModel().getSelectedItem() != null) {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("EditUserDialog.fxml"));
- Parent editUserDialog = loader.load();
- EditUserDialogView editUserDialogView = loader.getController();
- editUserDialogView.setMainView(this);
- editUserDialogView.setUserController(this.userController);
- editUserDialogView.setUserEntity(this.usersList.getSelectionModel().getSelectedItem());
- editUserDialogView.show();
- }
- }
- public void deleteUser(ActionEvent event) throws SQLException, IOException {
- this.authenticateAdminRights();
- if(this.usersList.getSelectionModel().getSelectedItem() != null) {
- this.userController.deleteUser(this.usersList.getSelectionModel().getSelectedItem().getID());
- }
- this.populateUsersList();
- }
- public void updateViewForUser() throws IOException, SQLException {
- this.userController.updateLoggedInUser();
- int rank = userController.getLoggedInUser().getRank();
- if(rank == UserController.RANK_DISABLED_USER) {
- this.logUserOut();
- } else if(rank == UserController.RANK_REGULAR_USER) {
- this.userMenu.setDisable(true);
- this.addStockMenuItem.setDisable(true);
- this.deleteStockItemButton.setDisable(true);
- } else if(rank == UserController.RANK_ADMINISTRATOR) {
- this.userMenu.setDisable(false);
- this.addStockMenuItem.setDisable(false);
- this.deleteStockItemButton.setDisable(false);
- }
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement