001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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 com.echothree.util.server.persistence.Session; 034import java.util.List; 035import javax.enterprise.context.ApplicationScoped; 036import javax.enterprise.inject.spi.CDI; 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 private String getOrderTypeName(OrderType orderType) { 051 return orderType.getLastDetail().getOrderTypeName(); 052 } 053 054 public OrderTimeType getOrderTimeTypeByName(final ExecutionErrorAccumulator eea, final OrderType orderType, final String orderTimeTypeName) { 055 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 056 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 057 058 if(orderTimeType == null) { 059 var orderTypeName = orderType.getLastDetail().getOrderTypeName(); 060 061 handleExecutionError(UnknownOrderTimeTypeNameException.class, eea, ExecutionErrors.UnknownOrderTimeTypeName.name(), orderTypeName, orderTimeTypeName); 062 } 063 064 return orderTimeType; 065 } 066 067 public void createOrderTime(final ExecutionErrorAccumulator eea, final Order order, final String orderTimeTypeName, final Long time, final BasePK partyPK) { 068 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 069 var orderDetail = order.getLastDetail().getOrder().getLastDetail(); 070 var orderType = orderDetail.getOrderType(); 071 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 072 073 if(eea == null || !eea.hasExecutionErrors()) { 074 if(orderTimeControl.orderTimeExists(order, orderTimeType)) { 075 handleExecutionError(DuplicateOrderTimeException.class, eea, ExecutionErrors.DuplicateOrderTime.name(), getOrderTypeName(orderType), 076 orderDetail.getOrderName(), orderTimeTypeName); 077 } else { 078 orderTimeControl.createOrderTime(order, orderTimeType, time, partyPK); 079 } 080 } 081 } 082 083 public void updateOrderTime(final ExecutionErrorAccumulator eea, final Order order, final String orderTimeTypeName, final Long time, final BasePK partyPK) { 084 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 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 orderTimeControl = Session.getModelController(OrderTimeControl.class); 112 var orderDetail = order.getLastDetail(); 113 var orderType = orderDetail.getOrderType(); 114 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 115 116 if(eea == null || !eea.hasExecutionErrors()) { 117 var orderTimeValue = orderTimeControl.getOrderTimeValueForUpdate(order, orderTimeType); 118 119 if(orderTimeValue == null) { 120 orderTimeControl.createOrderTime(order, orderTimeType, time, partyPK); 121 } else { 122 orderTimeValue.setTime(time); 123 orderTimeControl.updateOrderTimeFromValue(orderTimeValue, partyPK); 124 } 125 } 126 } 127 128 private OrderTime getOrderTimeEntity(final ExecutionErrorAccumulator eea, final Order order, final String orderTimeTypeName) { 129 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 130 var orderDetail = order.getLastDetail(); 131 var orderType = orderDetail.getOrderType(); 132 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 133 OrderTime result = null; 134 135 if(eea == null || !eea.hasExecutionErrors()) { 136 result = orderTimeControl.getOrderTimeForUpdate(order, orderTimeType); 137 138 if(result == null) { 139 handleExecutionError(UnknownOrderTimeException.class, eea, ExecutionErrors.UnknownOrderTime.name(), getOrderTypeName(orderType), orderDetail.getOrderName(), 140 orderTimeTypeName); 141 } 142 } 143 144 return result; 145 } 146 147 public Long getOrderTime(final ExecutionErrorAccumulator eea, final Order order, final String orderTimeTypeName) { 148 var orderTime = getOrderTimeEntity(eea, order, orderTimeTypeName); 149 150 return orderTime == null ? null : orderTime.getTime(); 151 } 152 153 public OrderTimeTransfer getOrderTimeTransfer(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final Order order, final String orderTimeTypeName) { 154 var orderTime = getOrderTimeEntity(eea, order, orderTimeTypeName); 155 156 return orderTime == null ? null : ((OrderTimeControl)Session.getModelController(OrderTimeControl.class)).getOrderTimeTransfer(userVisit, orderTime); 157 } 158 159 public List<OrderTimeTransfer> getOrderTimeTransfersByOrder(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final Order order) { 160 return ((OrderTimeControl)Session.getModelController(OrderTimeControl.class)).getOrderTimeTransfersByOrder(userVisit, order); 161 } 162 163 public void deleteOrderTime(final ExecutionErrorAccumulator eea, final Order order, final String orderTimeTypeName, final BasePK deletedBy) { 164 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 165 var orderDetail = order.getLastDetail(); 166 var orderType = orderDetail.getOrderType(); 167 var orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName); 168 169 if(eea == null || !eea.hasExecutionErrors()) { 170 var orderTime = orderTimeControl.getOrderTimeForUpdate(order, orderTimeType); 171 172 if(orderTime == null) { 173 handleExecutionError(UnknownOrderTimeException.class, eea, ExecutionErrors.UnknownOrderTime.name(), getOrderTypeName(orderType), orderDetail.getOrderName(), orderTimeTypeName); 174 } else { 175 orderTimeControl.deleteOrderTime(orderTime, deletedBy); 176 } 177 } 178 } 179 180}