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