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.order.server.logic; 018 019import com.echothree.model.control.order.common.exception.UnknownOrderLineTimeException; 020import com.echothree.model.control.order.common.exception.UnknownOrderTimeTypeNameException; 021import com.echothree.model.control.order.common.transfer.OrderLineTimeTransfer; 022import com.echothree.model.control.order.server.control.OrderTimeControl; 023import com.echothree.model.data.order.server.entity.OrderLine; 024import com.echothree.model.data.order.server.entity.OrderLineTime; 025import com.echothree.model.data.order.server.entity.OrderTimeType; 026import com.echothree.model.data.order.server.entity.OrderType; 027import com.echothree.model.data.user.server.entity.UserVisit; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.persistence.BasePK; 030import com.echothree.util.server.control.BaseLogic; 031import com.echothree.util.server.message.ExecutionErrorAccumulator; 032import java.util.List; 033import javax.enterprise.context.ApplicationScoped; 034import javax.inject.Inject; 035 036@ApplicationScoped 037public class OrderLineTimeLogic 038 extends BaseLogic { 039 040 @Inject 041 OrderTimeControl orderTimeControl; 042 043 protected OrderLineTimeLogic() { 044 super(); 045 } 046 047 private String getOrderTypeName(OrderType orderType) { 048 return orderType.getLastDetail().getOrderTypeName(); 049 } 050 051 public OrderTimeType getOrderTimeTypeByName(final ExecutionErrorAccumulator eea, final OrderType orderType, final String orderTimeTypeName) { 052 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 053 054 if(orderTimeType == null) { 055 var orderTypeName = orderType.getLastDetail().getOrderTypeName(); 056 057 handleExecutionError(UnknownOrderTimeTypeNameException.class, eea, ExecutionErrors.UnknownOrderTimeTypeName.name(), orderTypeName, orderTimeTypeName); 058 } 059 060 return orderTimeType; 061 } 062 063 public void createOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, final BasePK partyPK) { 064 var orderLineDetail = orderLine.getLastDetail(); 065 var orderDetail = orderLineDetail.getOrder().getLastDetail(); 066 var orderType = orderDetail.getOrderType(); 067 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 068 069 if(eea == null || !eea.hasExecutionErrors()) { 070 if(orderTimeControl.orderLineTimeExists(orderLine, orderTimeType)) { 071 handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.DuplicateOrderLineTime.name(), getOrderTypeName(orderType), 072 orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName); 073 } else { 074 orderTimeControl.createOrderLineTime(orderLine, orderTimeType, time, partyPK); 075 } 076 } 077 } 078 079 public void updateOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, final BasePK partyPK) { 080 var orderLineDetail = orderLine.getLastDetail(); 081 var orderDetail = orderLineDetail.getOrder().getLastDetail(); 082 var orderType = orderDetail.getOrderType(); 083 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 084 085 if(eea == null || !eea.hasExecutionErrors()) { 086 var orderLineTimeValue = orderTimeControl.getOrderLineTimeValueForUpdate(orderLine, orderTimeType); 087 088 if(orderLineTimeValue == null) { 089 handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType), 090 orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName); 091 } else { 092 orderLineTimeValue.setTime(time); 093 orderTimeControl.updateOrderLineTimeFromValue(orderLineTimeValue, partyPK); 094 } 095 } 096 } 097 098 public void createOrUpdateOrderLineTimeIfNotNull(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, 099 final BasePK partyPK) { 100 if(time != null) { 101 createOrUpdateOrderLineTime(eea, orderLine, orderTimeTypeName, time, partyPK); 102 } 103 } 104 105 public void createOrUpdateOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, 106 final BasePK partyPK) { 107 var orderLineDetail = orderLine.getLastDetail(); 108 var orderDetail = orderLineDetail.getOrder().getLastDetail(); 109 var orderType = orderDetail.getOrderType(); 110 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 111 112 if(eea == null || !eea.hasExecutionErrors()) { 113 var orderLineTimeValue = orderTimeControl.getOrderLineTimeValueForUpdate(orderLine, orderTimeType); 114 115 if(orderLineTimeValue == null) { 116 orderTimeControl.createOrderLineTime(orderLine, orderTimeType, time, partyPK); 117 } else { 118 orderLineTimeValue.setTime(time); 119 orderTimeControl.updateOrderLineTimeFromValue(orderLineTimeValue, partyPK); 120 } 121 } 122 } 123 124 public OrderLineTime getOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName) { 125 var orderLineDetail = orderLine.getLastDetail(); 126 var orderDetail = orderLineDetail.getOrder().getLastDetail(); 127 var orderType = orderDetail.getOrderType(); 128 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 129 OrderLineTime result = null; 130 131 if(eea == null || !eea.hasExecutionErrors()) { 132 result = orderTimeControl.getOrderLineTimeForUpdate(orderLine, orderTimeType); 133 134 if(result == null) { 135 handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType), 136 orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName); 137 } 138 } 139 140 return result; 141 } 142 143 public OrderLineTimeTransfer getOrderLineTimeTransfer(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final OrderLine orderLine, 144 final String orderTimeTypeName) { 145 var orderLineTime = getOrderLineTime(eea, orderLine, orderTimeTypeName); 146 147 return orderLineTime == null ? null : orderTimeControl.getOrderLineTimeTransfer(userVisit, orderLineTime); 148 } 149 150 public List<OrderLineTimeTransfer> getOrderLineTimeTransfersByOrder(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final OrderLine orderLine) { 151 return orderTimeControl.getOrderLineTimeTransfersByOrderLine(userVisit, orderLine); 152 } 153 154 public void deleteOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final BasePK deletedBy) { 155 var orderLineDetail = orderLine.getLastDetail(); 156 var orderDetail = orderLineDetail.getOrder().getLastDetail(); 157 var orderType = orderDetail.getOrderType(); 158 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 159 160 if(eea == null || !eea.hasExecutionErrors()) { 161 var orderLineTime = orderTimeControl.getOrderLineTimeForUpdate(orderLine, orderTimeType); 162 163 if(orderLineTime == null) { 164 handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType), 165 orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName); 166 } else { 167 orderTimeControl.deleteOrderLineTime(orderLineTime, deletedBy); 168 } 169 } 170 } 171 172}