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.item.server.command;
018
019import com.echothree.control.user.item.common.edit.ItemEditFactory;
020import com.echothree.control.user.item.common.edit.ItemShippingTimeEdit;
021import com.echothree.control.user.item.common.form.EditItemShippingTimeForm;
022import com.echothree.control.user.item.common.result.EditItemShippingTimeResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.control.user.item.common.spec.ItemShippingTimeSpec;
025import com.echothree.model.control.customer.server.control.CustomerControl;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.data.item.server.entity.Item;
028import com.echothree.model.data.item.server.entity.ItemShippingTime;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.EditMode;
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 EditItemShippingTimeCommand
042        extends BaseAbstractEditCommand<ItemShippingTimeSpec, ItemShippingTimeEdit, EditItemShippingTimeResult, ItemShippingTime, Item> {
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("ItemName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("CustomerTypeName", FieldType.ENTITY_NAME, true, null, null)
051        );
052        
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("ShippingStartTime", FieldType.DATE_TIME, true, null, null),
055                new FieldDefinition("ShippingEndTime", FieldType.DATE_TIME, false, null, null)
056        );
057    }
058
059    @Inject
060    CustomerControl customerControl;
061
062    @Inject
063    ItemControl itemControl;
064
065    
066    /** Creates a new instance of EditItemShippingTimeCommand */
067    public EditItemShippingTimeCommand() {
068        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
069    }
070    
071    @Override
072    public EditItemShippingTimeResult getResult() {
073        return ItemResultFactory.getEditItemShippingTimeResult();
074    }
075
076    @Override
077    public ItemShippingTimeEdit getEdit() {
078        return ItemEditFactory.getItemShippingTimeEdit();
079    }
080
081    @Override
082    public ItemShippingTime getEntity(EditItemShippingTimeResult result) {
083        ItemShippingTime itemShippingTime = null;
084        var itemName = spec.getItemName();
085        var item = itemControl.getItemByName(itemName);
086
087        if(item != null) {
088            var customerTypeName = spec.getCustomerTypeName();
089            var customerType = customerControl.getCustomerTypeByName(customerTypeName);
090
091            if(customerType != null) {
092                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
093                    itemShippingTime = itemControl.getItemShippingTime(item, customerType);
094                } else { // EditMode.UPDATE
095                    itemShippingTime = itemControl.getItemShippingTimeForUpdate(item, customerType);
096                }
097
098                if(itemShippingTime == null) {
099                    addExecutionError(ExecutionErrors.UnknownItemShippingTime.name(), itemName, customerTypeName);
100                }
101            } else {
102                addExecutionError(ExecutionErrors.UnknownCustomerTypeName.name(), customerTypeName);
103            }
104        } else {
105            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
106        }
107
108        return itemShippingTime;
109    }
110
111    @Override
112    public Item getLockEntity(ItemShippingTime itemShippingTime) {
113        return itemShippingTime.getItem();
114    }
115
116    @Override
117    public void fillInResult(EditItemShippingTimeResult result, ItemShippingTime itemShippingTime) {
118        result.setItemShippingTime(itemControl.getItemShippingTimeTransfer(getUserVisit(), itemShippingTime));
119    }
120
121    @Override
122    public void doLock(ItemShippingTimeEdit edit, ItemShippingTime itemShippingTime) {
123        var dateUtils = DateUtils.getInstance();
124        var shippingEndTime = itemShippingTime.getShippingEndTime();
125        var preferredDateTimeFormat = getPreferredDateTimeFormat();
126        var userVisit = getUserVisit();
127
128        edit.setShippingStartTime(dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, itemShippingTime.getShippingStartTime()));
129        edit.setShippingEndTime(shippingEndTime == null? null: dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, shippingEndTime));
130    }
131
132    @Override
133    public void doUpdate(ItemShippingTime itemShippingTime) {
134        var itemShippingTimeValue = itemControl.getItemShippingTimeValue(itemShippingTime);
135        var strShippingEndTime = edit.getShippingEndTime();
136        var shippingEndTime = strShippingEndTime == null? null: Long.valueOf(strShippingEndTime);
137
138        itemShippingTimeValue.setShippingStartTime(Long.valueOf(edit.getShippingStartTime()));
139        itemShippingTimeValue.setShippingEndTime(shippingEndTime);
140
141        itemControl.updateItemShippingTimeFromValue(itemShippingTimeValue, getPartyPK());
142    }
143    
144}