001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.shipment.server.logic;
018
019import com.echothree.model.control.shipment.server.control.ShipmentControl;
020import com.echothree.model.data.shipment.server.entity.Shipment;
021import com.echothree.model.data.shipment.server.entity.ShipmentType;
022import com.echothree.util.common.message.ExecutionErrors;
023import com.echothree.util.common.persistence.BasePK;
024import com.echothree.util.server.control.BaseLogic;
025import com.echothree.util.server.message.ExecutionErrorAccumulator;
026import com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.ApplicationScoped;
028import javax.enterprise.inject.spi.CDI;
029
030@ApplicationScoped
031public class ShipmentTimeLogic
032        extends BaseLogic {
033
034    protected ShipmentTimeLogic() {
035        super();
036    }
037
038    public static ShipmentTimeLogic getInstance() {
039        return CDI.current().select(ShipmentTimeLogic.class).get();
040    }
041
042    private String getShipmentTypeName(ShipmentType shipmentType) {
043        return shipmentType.getLastDetail().getShipmentTypeName();
044    }
045
046    public void createOrUpdateShipmentTimeIfNotNull(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time,
047            final BasePK partyPK) {
048        if(time != null) {
049            createOrUpdateShipmentTime(ema, shipment, shipmentTimeTypeName, time, partyPK);
050        }
051    }
052
053    public void createOrUpdateShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time,
054            final BasePK partyPK) {
055        var shipmentControl = Session.getModelController(ShipmentControl.class);
056        var shipmentDetail = shipment.getLastDetail();
057        var shipmentType = shipmentDetail.getShipmentType();
058        var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
059
060        if(shipmentTimeType == null) {
061            if(ema != null) {
062                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
063            }
064        } else {
065            var shipmentTimeValue = shipmentControl.getShipmentTimeValueForUpdate(shipment, shipmentTimeType);
066
067            if(shipmentTimeValue == null) {
068                shipmentControl.createShipmentTime(shipment, shipmentTimeType, time, partyPK);
069            } else {
070                shipmentTimeValue.setTime(time);
071                shipmentControl.updateShipmentTimeFromValue(shipmentTimeValue, partyPK);
072            }
073        }
074    }
075
076    public Long getShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName) {
077        var shipmentControl = Session.getModelController(ShipmentControl.class);
078        var shipmentDetail = shipment.getLastDetail();
079        var shipmentType = shipmentDetail.getShipmentType();
080        var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
081        Long result = null;
082
083        if(shipmentTimeType == null) {
084            if(ema != null) {
085                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
086            }
087        } else {
088            var shipmentTime = shipmentControl.getShipmentTime(shipment, shipmentTimeType);
089
090            if(shipmentTime == null) {
091                if(ema != null) {
092                    ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName);
093                }
094            } else {
095                result = shipmentTime.getTime();
096            }
097        }
098
099        return result;
100    }
101
102    public void deleteShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final BasePK deletedBy) {
103        var shipmentControl = Session.getModelController(ShipmentControl.class);
104        var shipmentDetail = shipment.getLastDetail();
105        var shipmentType = shipmentDetail.getShipmentType();
106        var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
107
108        if(shipmentTimeType == null) {
109            if(ema != null) {
110                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
111            }
112        } else {
113            var shipmentTime = shipmentControl.getShipmentTimeForUpdate(shipment, shipmentTimeType);
114
115            if(shipmentTime == null) {
116                if(ema != null) {
117                    ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName);
118                }
119            } else {
120                shipmentControl.deleteShipmentTime(shipmentTime, deletedBy);
121            }
122        }
123    }
124
125}