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