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.persistence.Session;
036import com.echothree.util.server.string.DateUtils;
037import java.util.List;
038import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditItemShippingTimeCommand */
060    public EditItemShippingTimeCommand() {
061        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
062    }
063    
064    @Override
065    public EditItemShippingTimeResult getResult() {
066        return ItemResultFactory.getEditItemShippingTimeResult();
067    }
068
069    @Override
070    public ItemShippingTimeEdit getEdit() {
071        return ItemEditFactory.getItemShippingTimeEdit();
072    }
073
074    @Override
075    public ItemShippingTime getEntity(EditItemShippingTimeResult result) {
076        var itemControl = Session.getModelController(ItemControl.class);
077        ItemShippingTime itemShippingTime = null;
078        var itemName = spec.getItemName();
079        var item = itemControl.getItemByName(itemName);
080
081        if(item != null) {
082            var customerControl = Session.getModelController(CustomerControl.class);
083            var customerTypeName = spec.getCustomerTypeName();
084            var customerType = customerControl.getCustomerTypeByName(customerTypeName);
085
086            if(customerType != null) {
087                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
088                    itemShippingTime = itemControl.getItemShippingTime(item, customerType);
089                } else { // EditMode.UPDATE
090                    itemShippingTime = itemControl.getItemShippingTimeForUpdate(item, customerType);
091                }
092
093                if(itemShippingTime == null) {
094                    addExecutionError(ExecutionErrors.UnknownItemShippingTime.name(), itemName, customerTypeName);
095                }
096            } else {
097                addExecutionError(ExecutionErrors.UnknownCustomerTypeName.name(), customerTypeName);
098            }
099        } else {
100            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
101        }
102
103        return itemShippingTime;
104    }
105
106    @Override
107    public Item getLockEntity(ItemShippingTime itemShippingTime) {
108        return itemShippingTime.getItem();
109    }
110
111    @Override
112    public void fillInResult(EditItemShippingTimeResult result, ItemShippingTime itemShippingTime) {
113        var itemControl = Session.getModelController(ItemControl.class);
114
115        result.setItemShippingTime(itemControl.getItemShippingTimeTransfer(getUserVisit(), itemShippingTime));
116    }
117
118    @Override
119    public void doLock(ItemShippingTimeEdit edit, ItemShippingTime itemShippingTime) {
120        var dateUtils = DateUtils.getInstance();
121        var shippingEndTime = itemShippingTime.getShippingEndTime();
122        var preferredDateTimeFormat = getPreferredDateTimeFormat();
123        var userVisit = getUserVisit();
124
125        edit.setShippingStartTime(dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, itemShippingTime.getShippingStartTime()));
126        edit.setShippingEndTime(shippingEndTime == null? null: dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, shippingEndTime));
127    }
128
129    @Override
130    public void doUpdate(ItemShippingTime itemShippingTime) {
131        var itemControl = Session.getModelController(ItemControl.class);
132        var itemShippingTimeValue = itemControl.getItemShippingTimeValue(itemShippingTime);
133        var strShippingEndTime = edit.getShippingEndTime();
134        var shippingEndTime = strShippingEndTime == null? null: Long.valueOf(strShippingEndTime);
135
136        itemShippingTimeValue.setShippingStartTime(Long.valueOf(edit.getShippingStartTime()));
137        itemShippingTimeValue.setShippingEndTime(shippingEndTime);
138
139        itemControl.updateItemShippingTimeFromValue(itemShippingTimeValue, getPartyPK());
140    }
141    
142}