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