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.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.OrderDetail;
024import com.echothree.model.data.order.server.entity.OrderLine;
025import com.echothree.model.data.order.server.entity.OrderLineDetail;
026import com.echothree.model.data.order.server.entity.OrderLineTime;
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.OrderLineTimeValue;
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 OrderLineTimeLogic
039        extends BaseLogic {
040
041    private OrderLineTimeLogic() {
042        super();
043    }
044
045    private static class OrderLineTimeLogicHolder {
046        static OrderLineTimeLogic instance = new OrderLineTimeLogic();
047    }
048
049    public static OrderLineTimeLogic getInstance() {
050        return OrderLineTimeLogicHolder.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 createOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, final BasePK partyPK) {
071        var orderTimeControl = Session.getModelController(OrderTimeControl.class);
072        OrderLineDetail orderLineDetail = orderLine.getLastDetail();
073        OrderDetail orderDetail = orderLineDetail.getOrder().getLastDetail();
074        OrderType orderType = orderDetail.getOrderType();
075        OrderTimeType orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName);
076
077        if(eea == null || !eea.hasExecutionErrors()) {
078            if(orderTimeControl.orderLineTimeExists(orderLine, orderTimeType)) {
079                handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.DuplicateOrderLineTime.name(), getOrderTypeName(orderType),
080                        orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName);
081            } else {
082                orderTimeControl.createOrderLineTime(orderLine, orderTimeType, time, partyPK);
083            }
084        }
085    }
086
087    public void updateOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time, final BasePK partyPK) {
088        var orderTimeControl = Session.getModelController(OrderTimeControl.class);
089        OrderLineDetail orderLineDetail = orderLine.getLastDetail();
090        OrderDetail orderDetail = orderLineDetail.getOrder().getLastDetail();
091        OrderType orderType = orderDetail.getOrderType();
092        OrderTimeType orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName);
093
094        if(eea == null || !eea.hasExecutionErrors()) {
095            OrderLineTimeValue orderLineTimeValue = orderTimeControl.getOrderLineTimeValueForUpdate(orderLine, orderTimeType);
096
097            if(orderLineTimeValue == null) {
098                handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType),
099                        orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName);
100            } else {
101                orderLineTimeValue.setTime(time);
102                orderTimeControl.updateOrderLineTimeFromValue(orderLineTimeValue, partyPK);
103            }
104        }
105    }
106
107    public void createOrUpdateOrderLineTimeIfNotNull(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time,
108            final BasePK partyPK) {
109        if(time != null) {
110            createOrUpdateOrderLineTime(eea, orderLine, orderTimeTypeName, time, partyPK);
111        }
112    }
113
114    public void createOrUpdateOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final Long time,
115            final BasePK partyPK) {
116        var orderTimeControl = Session.getModelController(OrderTimeControl.class);
117        OrderLineDetail orderLineDetail = orderLine.getLastDetail();
118        OrderDetail orderDetail = orderLineDetail.getOrder().getLastDetail();
119        OrderType orderType = orderDetail.getOrderType();
120        OrderTimeType orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName);
121
122        if(eea == null || !eea.hasExecutionErrors()) {
123            OrderLineTimeValue orderLineTimeValue = orderTimeControl.getOrderLineTimeValueForUpdate(orderLine, orderTimeType);
124
125            if(orderLineTimeValue == null) {
126                orderTimeControl.createOrderLineTime(orderLine, orderTimeType, time, partyPK);
127            } else {
128                orderLineTimeValue.setTime(time);
129                orderTimeControl.updateOrderLineTimeFromValue(orderLineTimeValue, partyPK);
130            }
131        }
132    }
133
134    private OrderLineTime getOrderLineTimeEntity(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName) {
135        var orderTimeControl = Session.getModelController(OrderTimeControl.class);
136        OrderLineDetail orderLineDetail = orderLine.getLastDetail();
137        OrderDetail orderDetail = orderLineDetail.getOrder().getLastDetail();
138        OrderType orderType = orderDetail.getOrderType();
139        OrderTimeType orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName);
140        OrderLineTime result = null;
141
142        if(eea == null || !eea.hasExecutionErrors()) {
143            result = orderTimeControl.getOrderLineTimeForUpdate(orderLine, orderTimeType);
144
145            if(result == null) {
146                handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType),
147                        orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName);
148            }
149        }
150
151        return result;
152    }
153
154    public Long getOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName) {
155        OrderLineTime orderLineTime = getOrderLineTimeEntity(eea, orderLine, orderTimeTypeName);
156        
157        return orderLineTime == null ? null : orderLineTime.getTime();
158    }
159
160    public OrderLineTimeTransfer getOrderLineTimeTransfer(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final OrderLine orderLine,
161            final String orderTimeTypeName) {
162        OrderLineTime orderLineTime = getOrderLineTimeEntity(eea, orderLine, orderTimeTypeName);
163        
164        return orderLineTime == null ? null : ((OrderTimeControl)Session.getModelController(OrderTimeControl.class)).getOrderLineTimeTransfer(userVisit, orderLineTime);
165    }
166
167    public List<OrderLineTimeTransfer> getOrderLineTimeTransfersByOrder(final ExecutionErrorAccumulator eea, final UserVisit userVisit, final OrderLine orderLine) {
168        return ((OrderTimeControl)Session.getModelController(OrderTimeControl.class)).getOrderLineTimeTransfersByOrderLine(userVisit, orderLine);
169    }
170
171    public void deleteOrderLineTime(final ExecutionErrorAccumulator eea, final OrderLine orderLine, final String orderTimeTypeName, final BasePK deletedBy) {
172        var orderTimeControl = Session.getModelController(OrderTimeControl.class);
173        OrderLineDetail orderLineDetail = orderLine.getLastDetail();
174        OrderDetail orderDetail = orderLineDetail.getOrder().getLastDetail();
175        OrderType orderType = orderDetail.getOrderType();
176        OrderTimeType orderTimeType = getOrderTimeTypeByName(eea, orderType, orderTimeTypeName);
177
178        if(eea == null || !eea.hasExecutionErrors()) {
179            OrderLineTime orderLineTime = orderTimeControl.getOrderLineTimeForUpdate(orderLine, orderTimeType);
180
181            if(orderLineTime == null) {
182                handleExecutionError(UnknownOrderLineTimeException.class, eea, ExecutionErrors.UnknownOrderLineTime.name(), getOrderTypeName(orderType),
183                        orderDetail.getOrderName(), orderLineDetail.getOrderLineSequence().toString(), orderTimeTypeName);
184            } else {
185                orderTimeControl.deleteOrderLineTime(orderLineTime, deletedBy);
186            }
187        }
188    }
189
190}