Complete InventoryDialogController migration
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package org.example.petshopdesktop.controllers.dialogcontrollers;
|
package org.example.petshopdesktop.controllers.dialogcontrollers;
|
||||||
|
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
@@ -11,14 +13,19 @@ import javafx.scene.control.TextField;
|
|||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.input.MouseEvent;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import javafx.util.StringConverter;
|
import javafx.util.StringConverter;
|
||||||
|
import org.example.petshopdesktop.DTOs.InventoryDTO;
|
||||||
import org.example.petshopdesktop.Validator;
|
import org.example.petshopdesktop.Validator;
|
||||||
import org.example.petshopdesktop.database.InventoryDB;
|
import org.example.petshopdesktop.api.dto.inventory.InventoryRequest;
|
||||||
import org.example.petshopdesktop.database.ProductDB;
|
import org.example.petshopdesktop.api.dto.inventory.InventoryResponse;
|
||||||
import org.example.petshopdesktop.models.Inventory;
|
import org.example.petshopdesktop.api.dto.product.ProductResponse;
|
||||||
|
import org.example.petshopdesktop.api.endpoints.InventoryApi;
|
||||||
|
import org.example.petshopdesktop.api.endpoints.ProductApi;
|
||||||
import org.example.petshopdesktop.models.Product;
|
import org.example.petshopdesktop.models.Product;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class InventoryDialogController {
|
public class InventoryDialogController {
|
||||||
|
|
||||||
@@ -60,12 +67,21 @@ public class InventoryDialogController {
|
|||||||
public Product fromString(String string) { return null; }
|
public Product fromString(String string) { return null; }
|
||||||
});
|
});
|
||||||
|
|
||||||
//Load product list from DB into combobox
|
//Load product list from API into combobox
|
||||||
try {
|
try {
|
||||||
cbProduct.setItems(ProductDB.getProducts());
|
List<ProductResponse> productResponses = ProductApi.getInstance().listProducts(null);
|
||||||
}
|
ObservableList<Product> products = FXCollections.observableArrayList();
|
||||||
|
for (ProductResponse pr : productResponses) {
|
||||||
catch (SQLException e) {
|
products.add(new Product(
|
||||||
|
pr.getId().intValue(),
|
||||||
|
pr.getProductName(),
|
||||||
|
pr.getPrice().doubleValue(),
|
||||||
|
0,
|
||||||
|
pr.getDescription()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
cbProduct.setItems(products);
|
||||||
|
} catch (Exception e) {
|
||||||
ActivityLogger.getInstance().logException(
|
ActivityLogger.getInstance().logException(
|
||||||
"InventoryDialogController.initialize",
|
"InventoryDialogController.initialize",
|
||||||
e,
|
e,
|
||||||
@@ -106,75 +122,33 @@ public class InventoryDialogController {
|
|||||||
|
|
||||||
//Operation only occurs if there are no errors
|
//Operation only occurs if there are no errors
|
||||||
if (errorMsg.isEmpty()) {
|
if (errorMsg.isEmpty()) {
|
||||||
|
try {
|
||||||
//Ensures duplicate entries aren't possible
|
InventoryRequest request = new InventoryRequest();
|
||||||
if (mode.equals("Add")) {
|
|
||||||
Product selectedProduct = cbProduct.getSelectionModel().getSelectedItem();
|
Product selectedProduct = cbProduct.getSelectionModel().getSelectedItem();
|
||||||
|
request.setProductId((long) selectedProduct.getProdId());
|
||||||
|
request.setQuantity(Integer.parseInt(txtQuantity.getText()));
|
||||||
|
|
||||||
try {
|
if (mode.equals("Add")) {
|
||||||
|
InventoryApi.getInstance().createInventory(request);
|
||||||
if (InventoryDB.productExistsInInventory(selectedProduct.getProdId())) {
|
} else {
|
||||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
Long inventoryId = Long.parseLong(lblInventoryId.getText().split(": ")[1]);
|
||||||
alert.setHeaderText("Duplicate Entry");
|
InventoryApi.getInstance().updateInventory(inventoryId, request);
|
||||||
alert.setContentText("An inventory record for \"" + selectedProduct.getProdName() + "\" already exists.");
|
|
||||||
alert.showAndWait();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (SQLException e) {
|
|
||||||
ActivityLogger.getInstance().logException(
|
|
||||||
"InventoryDialogController.buttonSaveClicked",
|
|
||||||
e,
|
|
||||||
"Checking if product exists in inventory");
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Inventory inventory = collectInventory();
|
|
||||||
|
|
||||||
//Adding inventory
|
|
||||||
if (mode.equals("Add")) {
|
|
||||||
try {
|
|
||||||
numRow = InventoryDB.insertInventory(inventory);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
ActivityLogger.getInstance().logException(
|
|
||||||
"InventoryDialogController.buttonSaveClicked",
|
|
||||||
e,
|
|
||||||
"Inserting new inventory record");
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Updating inventory
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
numRow = InventoryDB.updateInventory(inventory.getInventoryId(), inventory);
|
|
||||||
}
|
|
||||||
|
|
||||||
catch (SQLException e) {
|
|
||||||
ActivityLogger.getInstance().logException(
|
|
||||||
"InventoryDialogController.buttonSaveClicked",
|
|
||||||
e,
|
|
||||||
"Updating inventory with ID: " + inventory.getInventoryId());
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Display database operation result
|
|
||||||
if (numRow == 0) {
|
|
||||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
||||||
alert.setHeaderText("Database Operation Error");
|
|
||||||
alert.setContentText(mode + " failed");
|
|
||||||
alert.showAndWait();
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||||
alert.setHeaderText("Saved");
|
alert.setHeaderText("Saved");
|
||||||
alert.setContentText(mode + " succeeded");
|
alert.setContentText(mode + " succeeded");
|
||||||
alert.showAndWait();
|
alert.showAndWait();
|
||||||
closeStage(mouseEvent);
|
closeStage(mouseEvent);
|
||||||
|
} catch (Exception e) {
|
||||||
|
ActivityLogger.getInstance().logException(
|
||||||
|
"InventoryDialogController.buttonSaveClicked",
|
||||||
|
e,
|
||||||
|
mode + " inventory");
|
||||||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
alert.setHeaderText("Database Operation Error");
|
||||||
|
alert.setContentText(e.getMessage());
|
||||||
|
alert.showAndWait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,27 +161,6 @@ public class InventoryDialogController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create Inventory object using values entered by user
|
|
||||||
private Inventory collectInventory() {
|
|
||||||
int inventoryId = 0;
|
|
||||||
|
|
||||||
//Grab inventory ID when editing pre-existing record
|
|
||||||
if (lblInventoryId.isVisible()) {
|
|
||||||
inventoryId = Integer.parseInt(lblInventoryId.getText().split(": ")[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get selected product
|
|
||||||
Product selectedProduct = cbProduct.getSelectionModel().getSelectedItem();
|
|
||||||
|
|
||||||
//Build and returns Inventory object
|
|
||||||
return new Inventory(
|
|
||||||
inventoryId,
|
|
||||||
selectedProduct.getProdId(),
|
|
||||||
selectedProduct.getProdName(),
|
|
||||||
Integer.parseInt(txtQuantity.getText())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Close dialog view
|
//Close dialog view
|
||||||
private void closeStage(MouseEvent mouseEvent) {
|
private void closeStage(MouseEvent mouseEvent) {
|
||||||
Node node = (Node) mouseEvent.getSource();
|
Node node = (Node) mouseEvent.getSource();
|
||||||
@@ -217,7 +170,7 @@ public class InventoryDialogController {
|
|||||||
|
|
||||||
//Editing
|
//Editing
|
||||||
//Displays fields with existing inventory data
|
//Displays fields with existing inventory data
|
||||||
public void displayInventoryDetails(Inventory inventory) {
|
public void displayInventoryDetails(InventoryDTO inventory) {
|
||||||
if (inventory != null) {
|
if (inventory != null) {
|
||||||
|
|
||||||
//Displays inventory ID
|
//Displays inventory ID
|
||||||
|
|||||||
Reference in New Issue
Block a user