001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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 javax.enterprise.context.ApplicationScoped; 027import javax.inject.Inject; 028 029@ApplicationScoped 030public class ShipmentTimeLogic 031 extends BaseLogic { 032 033 @Inject 034 ShipmentControl shipmentControl; 035 036 protected ShipmentTimeLogic() { 037 super(); 038 } 039 040 private String getShipmentTypeName(ShipmentType shipmentType) { 041 return shipmentType.getLastDetail().getShipmentTypeName(); 042 } 043 044 public void createOrUpdateShipmentTimeIfNotNull(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time, 045 final BasePK partyPK) { 046 if(time != null) { 047 createOrUpdateShipmentTime(ema, shipment, shipmentTimeTypeName, time, partyPK); 048 } 049 } 050 051 public void createOrUpdateShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final Long time, 052 final BasePK partyPK) { 053 var shipmentDetail = shipment.getLastDetail(); 054 var shipmentType = shipmentDetail.getShipmentType(); 055 var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName); 056 057 if(shipmentTimeType == null) { 058 if(ema != null) { 059 ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName); 060 } 061 } else { 062 var shipmentTimeValue = shipmentControl.getShipmentTimeValueForUpdate(shipment, shipmentTimeType); 063 064 if(shipmentTimeValue == null) { 065 shipmentControl.createShipmentTime(shipment, shipmentTimeType, time, partyPK); 066 } else { 067 shipmentTimeValue.setTime(time); 068 shipmentControl.updateShipmentTimeFromValue(shipmentTimeValue, partyPK); 069 } 070 } 071 } 072 073 public Long getShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName) { 074 var shipmentDetail = shipment.getLastDetail(); 075 var shipmentType = shipmentDetail.getShipmentType(); 076 var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName); 077 Long result = null; 078 079 if(shipmentTimeType == null) { 080 if(ema != null) { 081 ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName); 082 } 083 } else { 084 var shipmentTime = shipmentControl.getShipmentTime(shipment, shipmentTimeType); 085 086 if(shipmentTime == null) { 087 if(ema != null) { 088 ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName); 089 } 090 } else { 091 result = shipmentTime.getTime(); 092 } 093 } 094 095 return result; 096 } 097 098 public void deleteShipmentTime(final ExecutionErrorAccumulator ema, final Shipment shipment, final String shipmentTimeTypeName, final BasePK deletedBy) { 099 var shipmentDetail = shipment.getLastDetail(); 100 var shipmentType = shipmentDetail.getShipmentType(); 101 var shipmentTimeType = shipmentControl.getShipmentTimeTypeByName(shipmentType, shipmentTimeTypeName); 102 103 if(shipmentTimeType == null) { 104 if(ema != null) { 105 ema.addExecutionError(ExecutionErrors.UnknownShipmentTimeTypeName.name(), getShipmentTypeName(shipmentType), shipmentTimeTypeName); 106 } 107 } else { 108 var shipmentTime = shipmentControl.getShipmentTimeForUpdate(shipment, shipmentTimeType); 109 110 if(shipmentTime == null) { 111 if(ema != null) { 112 ema.addExecutionError(ExecutionErrors.UnknownShipmentTime.name(), getShipmentTypeName(shipmentType), shipmentDetail.getShipmentName(), shipmentTimeTypeName); 113 } 114 } else { 115 shipmentControl.deleteShipmentTime(shipmentTime, deletedBy); 116 } 117 } 118 } 119 120}