Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

DT-480 [5/?]: Support CBR and commodity being unresolvable #149

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,11 @@ public void resolvedConfirmedBooking(@NotNull ConfirmedBooking confirmedBooking)
}
this.confirmedBooking = confirmedBooking;
}

public void resolvedCommodity(@NotNull Commodity commodity) {
if (!this.getCommoditySubreference().equals(commodity.getCommoditySubreference())) {
throw new IllegalArgumentException("Commodity had the wrong commodity subreference");
}
this.commodity = commodity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ protected RuntimeException errorForTargetStateNotListedAsSuccessor(
e);
}

public void assignConsignmentItems(List<ConsignmentItem> consignmentItems) {
public void assignConsignmentItems(@NotNull List<ConsignmentItem> consignmentItems) {
this.consignmentItems = consignmentItems;
for (var ci : consignmentItems) {
ci.setShippingInstruction(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,43 @@ public boolean isValid(ConsignmentItem value, ConstraintValidatorContext context
context.disableDefaultConstraintViolation();

var state = ValidationState.of(value, context);
validateConfirmedBookings(state);
validateBookingAndCommodity(state);
validateCustomsReferences(state,state.getValue().getCustomsReferences());
return state.isValid();
}

private void validateConfirmedBookings(ValidationState<ConsignmentItem> state) {
var confirmedBooking = state.getValue().getConfirmedBooking();
private void validateBookingAndCommodity(ValidationState<ConsignmentItem> state) {
var consignmentItem = state.getValue();
var confirmedBooking = consignmentItem.getConfirmedBooking();
if (confirmedBooking == null) {
state.getContext().buildConstraintViolationWithTemplate(
"Could not resolve booking with reference "
+ consignmentItem.getCarrierBookingReference()
+ ": It is not a known carrier booking reference")
// Match the TO path
.addPropertyNode("carrierBookingReference")
.addConstraintViolation();
state.invalidate();
return;
}
if (!confirmedBooking.getBooking().getBookingStatus().equals(BookingStatus.CONFIRMED)) {
state.getContext().buildConstraintViolationWithTemplate("The booking " + confirmedBooking.getCarrierBookingReference() + " is not in state CONFIRMED")
// Match the TO path
.addPropertyNode("carrierBookingReference")
.addConstraintViolation();
state.invalidate();
}
if (consignmentItem.getCommodity() == null) {
state.getContext().buildConstraintViolationWithTemplate(
"Could not resolve the commodity subreference "
+ consignmentItem.getCommoditySubreference()
+ " on the booking with carrier booking reference "
+ consignmentItem.getCarrierBookingReference()
+ ": It is not a known commodity subreference on said booking")
// Match the TO path
.addPropertyNode("carrierBookingReference")
.addConstraintViolation();
state.invalidate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,32 @@ private void validateShipper(ValidationState<ShippingInstruction> state) {
}

private void validateManifestFilings(ValidationState<ShippingInstruction> state) {
var si = state.getValue();

List<AdvanceManifestFilingEBL> advanceManifestFilingSIs = state.getValue().getAdvanceManifestFilings();
List<ConsignmentItem> consignmentItems = state.getValue().getConsignmentItems();
List<AdvanceManifestFilingEBL> advanceManifestFilingSIs = si.getAdvanceManifestFilings();
List<ConsignmentItem> consignmentItems = si.getConsignmentItems();

ConsignmentItem firstconsignmentItem = state.getValue().getConsignmentItems().get(0);
List<AdvanceManifestFiling> advanceManifestFilingBase = firstconsignmentItem.getConfirmedBooking().getAdvanceManifestFilings();
var firstBooking = si.getConsignmentItems().stream()
.map(ConsignmentItem::getConfirmedBooking)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);

if (firstBooking == null) {
// ConsignmentItemValidator flags shipment being null, so we are silent here.
return;
}

List<AdvanceManifestFiling> advanceManifestFilingBase = firstBooking.getAdvanceManifestFilings();
Map<String,List<AdvanceManifestFiling>> misMatchedConsignmentManifestFilings = new HashMap<>();

for(ConsignmentItem ci: consignmentItems) {
List<AdvanceManifestFiling> advanceManifestFilings = ci.getConfirmedBooking().getAdvanceManifestFilings();
for (ConsignmentItem ci : consignmentItems) {
var confirmedBooking = ci.getConfirmedBooking();
if (confirmedBooking == null) {
// ConsignmentItemValidator flags shipment being null, so we are silent here.
continue;
}
List<AdvanceManifestFiling> advanceManifestFilings = confirmedBooking.getAdvanceManifestFilings();
List<AdvanceManifestFiling> misMatchedManifestFilings = advanceManifestFilingBase.stream()
.filter(two -> advanceManifestFilings.stream()
.noneMatch(one -> one.getManifestTypeCode().equals(two.getManifestTypeCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ CREATE TABLE consignment_item (
carrier_booking_reference varchar(35) NOT NULL,
commodity_subreference varchar(100) NOT NULL,
shipping_instruction_id uuid NOT NULL REFERENCES shipping_instruction (id),
confirmed_booking_id uuid NOT NULL REFERENCES confirmed_booking (id), -- TODO; should be `NULL` instead of `NOT NULL`.
confirmed_booking_id uuid NULL REFERENCES confirmed_booking (id), -- TODO; should be `NULL` instead of `NOT NULL`.
nt-gt marked this conversation as resolved.
Show resolved Hide resolved
commodity_id uuid NULL REFERENCES commodity (id),
si_entry_order int NOT NULL default 0
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.dcsa.edocumentation.service;

import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotNull;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.dcsa.edocumentation.domain.persistence.entity.*;
import org.dcsa.edocumentation.domain.persistence.entity.CargoItem;
import org.dcsa.edocumentation.domain.persistence.entity.ConfirmedBooking;
import org.dcsa.edocumentation.domain.persistence.entity.ShippingInstruction;
Expand Down Expand Up @@ -62,7 +65,15 @@ public void resolveStuffing(
Map<String, UtilizedTransportEquipment> savedTransportEquipments) {

for (var ci : shippingInstruction.getConsignmentItems()) {
ci.resolvedConfirmedBooking(resolveConfirmedBooking(ci.getCarrierBookingReference()));
var confirmedBooking = resolveConfirmedBooking(ci.getCarrierBookingReference());
Commodity commodity = null;
if (confirmedBooking != null) {
ci.resolvedConfirmedBooking(confirmedBooking);
commodity = resolveCommodity(confirmedBooking, ci.getCommoditySubreference());
}
if (commodity != null) {
ci.resolvedCommodity(commodity);
}
for (var c : ci.getCargoItems()) {
c.assignEquipment(findSavedUtilizedTransportEquipmentViaCargoItem(savedTransportEquipments, c));
}
Expand All @@ -74,12 +85,18 @@ public void resolveStuffing(
private ConfirmedBooking resolveConfirmedBooking(String carrierBookingReference) {
return confirmedBookingRepository
.findByCarrierBookingReference(carrierBookingReference)
.orElseThrow(
() ->
// TODO: This should result in a PENDING UPDATE rather than a HTTP 400 by the time DT-578 is finished
ConcreteRequestErrorMessageException.invalidInput(
"No shipment has been found for this carrierBookingReference: "
+ carrierBookingReference));
.orElse(null);
}

private Commodity resolveCommodity(@NotNull ConfirmedBooking shipment, @NotNull String commoditySubreference) {
for (var reg : shipment.getBooking().getRequestedEquipments()) {
for (var commodity : reg.getCommodities()) {
if (Objects.equals(commoditySubreference, commodity.getCommoditySubreference())) {
return commodity;
}
}
}
return null;
}

private UtilizedTransportEquipment findSavedUtilizedTransportEquipmentViaCargoItem(
Expand Down
Loading
Loading