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