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