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