ConsignmentSearchResultHelper.java

package com.tradecloud.dto.consignment;

import com.tradecloud.domain.container.ContainerTypeCode;
import com.tradecloud.domain.container.ShipmentContainer;
import com.tradecloud.domain.event.ShipmentEvent;
import com.tradecloud.domain.event.ShipmentEventType;
import com.tradecloud.domain.model.ordermanagement.Consignment;
import com.tradecloud.domain.model.shipment.ShippingMode;
import com.tradecloud.domain.shipment.SeaShipment;
import com.tradecloud.domain.shipment.Shipment;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;

import java.util.List;

/**
 *
 */
public class ConsignmentSearchResultHelper {

    public static <T> T getNonProxyObject(T potentialProxy) {
        if (potentialProxy instanceof HibernateProxy) {
            HibernateProxy hibernateProxy = (HibernateProxy) potentialProxy;
            LazyInitializer hibernateLazyInitializer = hibernateProxy.getHibernateLazyInitializer();

            if (!hibernateLazyInitializer.isUninitialized()) {
                return (T) hibernateLazyInitializer.getImplementation();
            }
        }
        return potentialProxy;
    }

    public static void populateConsignmentStatusSearchResult(ConsignmentStatusSearchResult result, Consignment consignment) {

        result.setReference(consignment.getReference() != null ? consignment.getReference() : null);
        result.setNumber(consignment.getNumber() != null ? consignment.getNumber() : null);
        result.setState(consignment.getState() != null ? consignment.getState() : null);
        result.setPlaceOfLoading(consignment.getShippingInfo().getPlaceOfLoading() != null
                ? consignment.getShippingInfo().getPlaceOfLoading().getName() : null);
        result.setPlaceOfDischarge(consignment.getShippingInfo().getPlaceOfDischarge() != null
                ? consignment.getShippingInfo().getPlaceOfDischarge().getName() : null);
        result.setFreightForwarder(consignment.getShippingInfo().getFreightForwarder() != null
                ? consignment.getShippingInfo().getFreightForwarder().getName() : "");
        result.setId(consignment.getId() != null ? consignment.getId() : null);
        result.setLatestShipmentDate(consignment.getLatestShipmentDate());
        result.setShippingMode(consignment.getShippingMode() != null ? consignment.getShippingMode().name() : null);
    }

    public static void populateCarrierUtilisationSearchResult(CarrierUtilisationSearchResult result, Consignment consignment) {

        result.setReference(consignment.getReference() != null ? consignment.getReference() : null);
        result.setPlaceOfLoading(consignment.getShippingInfo().getPlaceOfLoading() != null
                ? consignment.getShippingInfo().getPlaceOfLoading().getName() : null);
        result.setId(consignment.getId() != null ? consignment.getId() : null);

        if (consignment.getShippingMode() == ShippingMode.SEA) {
        }
        result.setActualDepartureDate(null);

        if (consignment.getShipment() != null) {
            result.setShipmentId(consignment.getShipment().getId());
            result.setShipmentReference(consignment.getShipment().getReference());
            result.setActualDepartureDate(consignment.getShipment().getActualDepartureDate());
            if (getNonProxyObject(consignment.getShipment()) instanceof SeaShipment) {
                if (((SeaShipment) getNonProxyObject(consignment.getShipment())).getCarrier() != null) {
                    result.setActualCarrier(((SeaShipment) getNonProxyObject(consignment.getShipment()))
                            .getCarrier().getName());
                }
            }

            BigDecimal teu = BigDecimal.ZERO.setScale(4);
            for (ShipmentContainer container : consignment.getShipment().getContainers()) {
                if (container.getContainerType().getCode().equals(ContainerTypeCode.LCL.name())) {
                    teu = teu.add(BigDecimal.ONE.divide(container.getContainerType().getTeu(), 4, RoundingMode.HALF_UP));
                } else {
                    teu = teu.add(container.getContainerType().getTeu());
                }
            }

            result.setTwentyFootEquivalentUnits("" + teu);
        }
        if (consignment.getShippingInfo() != null && consignment.getShippingInfo().getCarrier() != null) {
            result.setAllocatedCarrier(consignment.getShippingInfo().getCarrier().getName());
        }
        if (consignment.getShipment().getShippingInfo() != null
                && consignment.getShipment().getEstimatedArrivalDateAtPlaceOfDischarge() != null) {
            result.setEstimateArrivalDatesOfPlaceOfDischarge(consignment.getShipment().getEstimatedArrivalDateAtPlaceOfDischarge());
        }
        result.setConsignmentReference(consignment.getReference());

    }

    public static ShipmentStatusSearchResult populateShipmentStatusSearchResult(Shipment shipment) {
        ShipmentStatusSearchResult result = new ShipmentStatusSearchResult();

        result.setShipmentId(shipment.getId());
        result.setShipmentNumber(shipment.getNumber());
        result.setShipmentReference(shipment.getReference());
        result.setShippingMode(shipment.getShippingMode().name());
        result.setFreightForwarder(shipment.getShippingInfo().getFreightForwarder().getName());
        result.setPlaceOfLoading(shipment.getShippingInfo().getPlaceOfLoading().getName());
        result.setShipmentCreationDate(shipment.getShippingInfo().getCreated());
        result.setShipmentState(shipment.getState());

        String unsignoffReason = "";
        List<ShipmentEvent> events = shipment.getEvents();
        for (ShipmentEvent event : events) {
            if (event.getEventType().equals(ShipmentEventType.UNSIGNED_OFF)) {
                unsignoffReason = event.getReason();
            }
        }

        result.setUnsignOffReason(unsignoffReason);

        return result;
    }
}