diff --git a/src/main/java/org/chainoptim/desktop/features/warehouse/controller/WarehouseStorageController.java b/src/main/java/org/chainoptim/desktop/features/warehouse/controller/WarehouseStorageController.java index af7fa60..e8711c7 100644 --- a/src/main/java/org/chainoptim/desktop/features/warehouse/controller/WarehouseStorageController.java +++ b/src/main/java/org/chainoptim/desktop/features/warehouse/controller/WarehouseStorageController.java @@ -1,9 +1,7 @@ package org.chainoptim.desktop.features.warehouse.controller; import org.chainoptim.desktop.core.context.TenantContext; -import org.chainoptim.desktop.features.warehouse.model.Compartment; -import org.chainoptim.desktop.features.warehouse.model.Crate; -import org.chainoptim.desktop.features.warehouse.model.Warehouse; +import org.chainoptim.desktop.features.warehouse.model.*; import org.chainoptim.desktop.features.warehouse.service.CompartmentService; import org.chainoptim.desktop.features.warehouse.service.CrateService; import org.chainoptim.desktop.shared.fallback.FallbackManager; @@ -12,7 +10,10 @@ import com.google.inject.Inject; import javafx.application.Platform; import javafx.fxml.FXML; +import javafx.geometry.Pos; import javafx.scene.control.Label; +import javafx.scene.control.ProgressBar; +import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import java.util.List; @@ -26,6 +27,9 @@ public class WarehouseStorageController implements DataReceiver { // State private final FallbackManager fallbackManager; + private Warehouse warehouse; + private List crates; + // FXML @FXML private VBox compartmentsVBox; @@ -40,7 +44,7 @@ public WarehouseStorageController(CompartmentService compartmentService, } public void setData(Warehouse warehouse) { - loadCompartments(warehouse.getId()); + this.warehouse = warehouse; if (TenantContext.getCurrentUser() == null) { fallbackManager.setErrorMessage("User not logged in"); @@ -50,22 +54,13 @@ public void setData(Warehouse warehouse) { loadCrates(organizationId); } - private void loadCompartments(Integer warehouseId) { - fallbackManager.reset(); - fallbackManager.setLoading(true); - - compartmentService.getCompartmentsByWarehouseId(warehouseId) - .thenApply(this::handleCompartmentsResponse) - .exceptionally(this::handleCompartmentsError); - } - private void loadCrates(Integer organizationId) { crateService.getCratesByOrganizationId(organizationId) - .thenApply(this::handleCratesResponse) - .exceptionally(this::handleCratesError); + .thenApply(this::handleCratesResponse) + .exceptionally(this::handleCratesError); } - private Result> handleCompartmentsResponse(Result> result) { + private Result> handleCratesResponse(Result> result) { Platform.runLater(() -> { if (result.getError() != null) { fallbackManager.setErrorMessage(result.getError().getMessage()); @@ -73,33 +68,31 @@ private Result> handleCompartmentsResponse(Result> handleCompartmentsError(Throwable throwable) { - fallbackManager.setErrorMessage("Failed to load compartments"); + private Result> handleCratesError(Throwable throwable) { + fallbackManager.setErrorMessage("Failed to load crates"); return new Result<>(); } - private void renderCompartments(List compartments) { - compartmentsVBox.getChildren().clear(); - - for (Compartment compartment : compartments) { - Label compartmentName = new Label(compartment.getName()); - compartmentName.getStyleClass().add("entity-name-label"); - compartmentsVBox.getChildren().add(compartmentName); - } + private void loadCompartments(Integer warehouseId) { + fallbackManager.reset(); + fallbackManager.setLoading(true); - fallbackManager.setNoResults(false); + compartmentService.getCompartmentsByWarehouseId(warehouseId) + .thenApply(this::handleCompartmentsResponse) + .exceptionally(this::handleCompartmentsError); } - private Result> handleCratesResponse(Result> result) { + private Result> handleCompartmentsResponse(Result> result) { Platform.runLater(() -> { if (result.getError() != null) { fallbackManager.setErrorMessage(result.getError().getMessage()); @@ -107,16 +100,75 @@ private Result> handleCratesResponse(Result> result) { } fallbackManager.setLoading(false); - System.out.println("Crates loaded successfully"); -// renderCrates(result.getData()); + + renderCompartments(result.getData()); }); return result; } - private Result> handleCratesError(Throwable throwable) { - fallbackManager.setErrorMessage("Failed to load crates"); + private Result> handleCompartmentsError(Throwable throwable) { + fallbackManager.setErrorMessage("Failed to load compartments"); return new Result<>(); } + + private void renderCompartments(List compartments) { + compartmentsVBox.getChildren().clear(); + compartmentsVBox.setSpacing(10); + + for (Compartment compartment : compartments) { + HBox compartmentHBox = new HBox(10); + compartmentHBox.setAlignment(Pos.CENTER_LEFT); + compartmentsVBox.getChildren().add(compartmentHBox); + + Label compartmentName = new Label(compartment.getName()); + compartmentName.getStyleClass().add("entity-name-label"); + compartmentHBox.getChildren().add(compartmentName); + + List crateSpecs = compartment.getData().getCrateSpecs(); + if (crateSpecs == null || crateSpecs.isEmpty()) { + continue; + } + + for (CrateSpec crateSpec : crateSpecs) { + Integer crateId = crateSpec.getCrateId(); + if (crateId == null) { + continue; + } + +// Crate crate = crates.stream() +// .filter(c -> c.getId().equals(crateId)) +// .findFirst() +// .orElse(null); +// if (crate == null) { +// continue; +// } +// +// Label crateName = new Label(crate.getName()); +// crateName.getStyleClass().add("entity-name-label"); +// compartmentHBox.getChildren().add(crateName); + + CrateData crateData = compartment.getData().getCurrentCrates().stream() + .filter(cd -> cd.getCrateId().equals(crateId)) + .findFirst() + .orElse(null); + if (crateData == null) { + continue; + } + + String crateQuantityText = crateData.getNumberOfCrates() + " / " + crateSpec.getMaxCrates(); + Label crateQuantity = new Label(crateQuantityText); + compartmentHBox.getChildren().add(crateQuantity); + + ProgressBar progressBar = new ProgressBar(); + progressBar.setProgress((double) crateData.getNumberOfCrates() / crateSpec.getMaxCrates()); + compartmentHBox.getChildren().add(progressBar); + + float occupiedRatio = crateData.getNumberOfCrates() / crateSpec.getMaxCrates(); + Label occupiedRatioLabel = new Label(String.format("%.2f", occupiedRatio * 100) + "%"); + compartmentHBox.getChildren().add(occupiedRatioLabel); + } + } + } }