Skip to content

Commit

Permalink
Display Compartments in Warehouse Storage tab
Browse files Browse the repository at this point in the history
  • Loading branch information
TudorOrban committed Jul 27, 2024
1 parent e34eba1 commit 398aebf
Showing 1 changed file with 85 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -26,6 +27,9 @@ public class WarehouseStorageController implements DataReceiver<Warehouse> {
// State
private final FallbackManager fallbackManager;

private Warehouse warehouse;
private List<Crate> crates;

// FXML
@FXML
private VBox compartmentsVBox;
Expand All @@ -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");
Expand All @@ -50,73 +54,121 @@ 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<List<Compartment>> handleCompartmentsResponse(Result<List<Compartment>> result) {
private Result<List<Crate>> handleCratesResponse(Result<List<Crate>> result) {
Platform.runLater(() -> {
if (result.getError() != null) {
fallbackManager.setErrorMessage(result.getError().getMessage());
return;
}

fallbackManager.setLoading(false);
crates = result.getData();

renderCompartments(result.getData());
loadCompartments(this.warehouse.getId());
});

return result;
}

private Result<List<Compartment>> handleCompartmentsError(Throwable throwable) {
fallbackManager.setErrorMessage("Failed to load compartments");
private Result<List<Crate>> handleCratesError(Throwable throwable) {
fallbackManager.setErrorMessage("Failed to load crates");

return new Result<>();
}

private void renderCompartments(List<Compartment> 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<List<Crate>> handleCratesResponse(Result<List<Crate>> result) {
private Result<List<Compartment>> handleCompartmentsResponse(Result<List<Compartment>> result) {
Platform.runLater(() -> {
if (result.getError() != null) {
fallbackManager.setErrorMessage(result.getError().getMessage());
return;
}

fallbackManager.setLoading(false);
System.out.println("Crates loaded successfully");
// renderCrates(result.getData());

renderCompartments(result.getData());
});

return result;
}

private Result<List<Crate>> handleCratesError(Throwable throwable) {
fallbackManager.setErrorMessage("Failed to load crates");
private Result<List<Compartment>> handleCompartmentsError(Throwable throwable) {
fallbackManager.setErrorMessage("Failed to load compartments");

return new Result<>();
}

private void renderCompartments(List<Compartment> 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<CrateSpec> 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);
}
}
}
}

0 comments on commit 398aebf

Please sign in to comment.