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