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.ItemCountryOfOriginEdit;
020import com.echothree.control.user.item.common.edit.ItemEditFactory;
021import com.echothree.control.user.item.common.form.EditItemCountryOfOriginForm;
022import com.echothree.control.user.item.common.result.EditItemCountryOfOriginResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.control.user.item.common.spec.ItemCountryOfOriginSpec;
025import com.echothree.model.control.geo.server.control.GeoControl;
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.ItemCountryOfOrigin;
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.PercentUtils;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class EditItemCountryOfOriginCommand
042        extends BaseAbstractEditCommand<ItemCountryOfOriginSpec, ItemCountryOfOriginEdit, EditItemCountryOfOriginResult, ItemCountryOfOrigin, 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("CountryName", FieldType.ENTITY_NAME, true, null, null)
051        );
052        
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("Percent", FieldType.FRACTIONAL_PERCENT, true, null, null)
055        );
056    }
057
058    @Inject
059    GeoControl geoControl;
060
061    @Inject
062    ItemControl itemControl;
063
064    
065    /** Creates a new instance of EditItemCountryOfOriginCommand */
066    public EditItemCountryOfOriginCommand() {
067        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
068    }
069    
070    @Override
071    public EditItemCountryOfOriginResult getResult() {
072        return ItemResultFactory.getEditItemCountryOfOriginResult();
073    }
074
075    @Override
076    public ItemCountryOfOriginEdit getEdit() {
077        return ItemEditFactory.getItemCountryOfOriginEdit();
078    }
079
080    @Override
081    public ItemCountryOfOrigin getEntity(EditItemCountryOfOriginResult result) {
082        ItemCountryOfOrigin itemCountryOfOrigin = null;
083        var itemName = spec.getItemName();
084        var item = itemControl.getItemByName(itemName);
085
086        if(item != null) {
087            var countryName = spec.getCountryName();
088            var countryGeoCode = geoControl.getCountryByAlias(countryName);
089            
090            if(countryGeoCode != null) {
091                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
092                    itemCountryOfOrigin = itemControl.getItemCountryOfOrigin(item, countryGeoCode);
093                } else { // EditMode.UPDATE
094                    itemCountryOfOrigin = itemControl.getItemCountryOfOriginForUpdate(item, countryGeoCode);
095                }
096
097                if(itemCountryOfOrigin == null) {
098                    addExecutionError(ExecutionErrors.UnknownItemCountryOfOrigin.name(), itemName, countryName);
099                }
100            } else {
101                addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
102            }
103        } else {
104            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
105        }
106
107        return itemCountryOfOrigin;
108    }
109
110    @Override
111    public Item getLockEntity(ItemCountryOfOrigin itemCountryOfOrigin) {
112        return itemCountryOfOrigin.getItem();
113    }
114
115    @Override
116    public void fillInResult(EditItemCountryOfOriginResult result, ItemCountryOfOrigin itemCountryOfOrigin) {
117        result.setItemCountryOfOrigin(itemControl.getItemCountryOfOriginTransfer(getUserVisit(), itemCountryOfOrigin));
118    }
119
120    @Override
121    public void doLock(ItemCountryOfOriginEdit edit, ItemCountryOfOrigin itemCountryOfOrigin) {
122        edit.setPercent(PercentUtils.getInstance().formatFractionalPercent(itemCountryOfOrigin.getPercent()));
123    }
124
125    @Override
126    public void doUpdate(ItemCountryOfOrigin itemCountryOfOrigin) {
127        var itemCountryOfOriginValue = itemControl.getItemCountryOfOriginValue(itemCountryOfOrigin);
128
129        itemCountryOfOriginValue.setPercent(Integer.valueOf(edit.getPercent()));
130
131        itemControl.updateItemCountryOfOriginFromValue(itemCountryOfOriginValue, getPartyPK());
132    }
133    
134}