001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.ShipmentControl;
020import com.echothree.model.data.shipment.server.entity.Shipment;
021import com.echothree.model.data.shipment.server.entity.ShipmentDetail;
022import com.echothree.model.data.shipment.server.entity.ShipmentTime;
023import com.echothree.model.data.shipment.server.entity.ShipmentTimeType;
024import com.echothree.model.data.shipment.server.entity.ShipmentType;
025import com.echothree.model.data.shipment.server.value.ShipmentTimeValue;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.persistence.BasePK;
028import com.echothree.util.server.control.BaseLogic;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.Session;
031
032public class ShipmentTimeLogic
033        extends BaseLogic {
034
035    private ShipmentTimeLogic() {
036        super();
037    }
038
039    private static class ShipmentTimeLogicHolder {
040        static ShipmentTimeLogic instance = new ShipmentTimeLogic();
041    }
042
043    public static ShipmentTimeLogic getInstance() {
044        return ShipmentTimeLogicHolder.instance;
045    }
046
047    private String getShipmentTypeName(ShipmentType shipmentType) {
048        return shipmentType.getLastDetail().getShipmentTypeName();
049    }
050
051    public void createOrUpdateShipmentTimeIfNotNull(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time,
052            final BasePK partyPK) {
053        if(time != null) {
054            createOrUpdateShipmentTime(ema, shipment, shipmentTimeTypeName, time, partyPK);
055        }
056    }
057
058    public void createOrUpdateShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time,
059            final BasePK partyPK) {
060        var shipmentControl = Session.getModelController(ShipmentControl.class);
061        ShipmentDetail shipmentDetail = shipment.getLastDetail();
062        ShipmentType shipmentType = shipmentDetail.getShipmentType();
063        ShipmentTimeType shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
064
065        if(shipmentTimeType == null) {
066            if(ema != null) {
067                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
068            }
069        } else {
070            ShipmentTimeValue shipmentTimeValue = shipmentControl.getShipmentTimeValueForUpdate(shipment, shipmentTimeType);
071
072            if(shipmentTimeValue == null) {
073                shipmentControl.createShipmentTime(shipment, shipmentTimeType, time, partyPK);
074            } else {
075                shipmentTimeValue.setTime(time);
076                shipmentControl.updateShipmentTimeFromValue(shipmentTimeValue, partyPK);
077            }
078        }
079    }
080
081    public Long getShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName) {
082        var shipmentControl = Session.getModelController(ShipmentControl.class);
083        ShipmentDetail shipmentDetail = shipment.getLastDetail();
084        ShipmentType shipmentType = shipmentDetail.getShipmentType();
085        ShipmentTimeType shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
086        Long result = null;
087
088        if(shipmentTimeType == null) {
089            if(ema != null) {
090                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
091            }
092        } else {
093            ShipmentTime shipmentTime = shipmentControl.getShipmentTime(shipment, shipmentTimeType);
094
095            if(shipmentTime == null) {
096                if(ema != null) {
097                    ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName);
098                }
099            } else {
100                result = shipmentTime.getTime();
101            }
102        }
103
104        return result;
105    }
106
107    public void deleteShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final BasePK deletedBy) {
108        var shipmentControl = Session.getModelController(ShipmentControl.class);
109        ShipmentDetail shipmentDetail = shipment.getLastDetail();
110        ShipmentType shipmentType = shipmentDetail.getShipmentType();
111        ShipmentTimeType shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName);
112
113        if(shipmentTimeType == null) {
114            if(ema != null) {
115                ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName);
116            }
117        } else {
118            ShipmentTime shipmentTime = shipmentControl.getShipmentTimeForUpdate(shipment, shipmentTimeType);
119
120            if(shipmentTime == null) {
121                if(ema != null) {
122                    ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName);
123                }
124            } else {
125                shipmentControl.deleteShipmentTime(shipmentTime, deletedBy);
126            }
127        }
128    }
129
130}