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