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