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.control.user.sales.server.command;
018
019import com.echothree.control.user.sales.common.edit.SalesEditFactory;
020import com.echothree.control.user.sales.common.edit.SalesOrderTimeEdit;
021import com.echothree.control.user.sales.common.form.EditSalesOrderTimeForm;
022import com.echothree.control.user.sales.common.result.EditSalesOrderTimeResult;
023import com.echothree.control.user.sales.common.result.SalesResultFactory;
024import com.echothree.control.user.sales.common.spec.SalesOrderTimeSpec;
025import com.echothree.model.control.order.server.control.OrderTimeControl;
026import com.echothree.model.control.sales.server.logic.SalesOrderLogic;
027import com.echothree.model.data.order.server.entity.Order;
028import com.echothree.model.data.order.server.entity.OrderTime;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.string.DateUtils;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class EditSalesOrderTimeCommand
042        extends BaseAbstractEditCommand<SalesOrderTimeSpec, SalesOrderTimeEdit, EditSalesOrderTimeResult, OrderTime, Order> {
043
044    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
045    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
046
047    static {
048        SPEC_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("OrderTimeTypeName", FieldType.ENTITY_NAME, true, null, null)
051        );
052
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("Time", FieldType.DATE_TIME, true, null, null)
055        );
056    }
057
058    @Inject
059    OrderTimeControl orderTimeControl;
060
061    @Inject
062    SalesOrderLogic salesOrderLogic;
063
064    /** Creates a new instance of EditSalesOrderTimeCommand */
065    public EditSalesOrderTimeCommand() {
066        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
067    }
068
069    @Override
070    public EditSalesOrderTimeResult getResult() {
071        return SalesResultFactory.getEditSalesOrderTimeResult();
072    }
073
074    @Override
075    public SalesOrderTimeEdit getEdit() {
076        return SalesEditFactory.getSalesOrderTimeEdit();
077    }
078
079    @Override
080    public OrderTime getEntity(EditSalesOrderTimeResult result) {
081        var orderName = spec.getOrderName();
082        var order = salesOrderLogic.getOrderByName(this, orderName);
083        OrderTime orderTime = null;
084        
085        if(!hasExecutionErrors()) {
086            var orderType = order.getLastDetail().getOrderType();
087            var orderTimeTypeName = spec.getOrderTimeTypeName();
088            var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName);
089
090            if(orderTimeType != null) {
091                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
092                    orderTime = orderTimeControl.getOrderTime(order, orderTimeType);
093                } else { // EditMode.UPDATE
094                    orderTime = orderTimeControl.getOrderTimeForUpdate(order, orderTimeType);
095                }
096
097                if(orderTime == null) {
098                    addExecutionError(ExecutionErrors.UnknownOrderTime.name(), orderType.getLastDetail().getOrderTypeName(), orderName, orderTimeTypeName);
099                }
100            }
101        }
102        
103        return orderTime;
104    }
105
106    @Override
107    public Order getLockEntity(OrderTime orderTime) {
108        return orderTime.getOrder();
109    }
110
111    @Override
112    public void fillInResult(EditSalesOrderTimeResult result, OrderTime orderTime) {
113        result.setOrderTime(orderTimeControl.getOrderTimeTransfer(getUserVisit(), orderTime));
114    }
115
116    @Override
117    public void doLock(SalesOrderTimeEdit edit, OrderTime orderTime) {
118        edit.setTime(DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), orderTime.getTime()));
119    }
120
121    @Override
122    public void doUpdate(OrderTime orderTime) {
123        var orderTimeValue = orderTimeControl.getOrderTimeValue(orderTime);
124        var time = Long.valueOf(edit.getTime());
125        
126        orderTimeValue.setTime(time);
127
128        orderTimeControl.updateOrderTimeFromValue(orderTimeValue, getPartyPK());
129    }
130
131}