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.ItemHarmonizedTariffScheduleCodeEdit;
021import com.echothree.control.user.item.common.form.EditItemHarmonizedTariffScheduleCodeForm;
022import com.echothree.control.user.item.common.result.EditItemHarmonizedTariffScheduleCodeResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.control.user.item.common.spec.ItemHarmonizedTariffScheduleCodeSpec;
025import com.echothree.model.control.geo.server.control.GeoControl;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.geo.server.entity.GeoCode;
031import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCode;
032import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCodeUseType;
033import com.echothree.model.data.item.server.entity.ItemHarmonizedTariffScheduleCode;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.server.control.BaseAbstractEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.List;
045import javax.enterprise.context.Dependent;
046
047@Dependent
048public class EditItemHarmonizedTariffScheduleCodeCommand
049        extends BaseAbstractEditCommand<ItemHarmonizedTariffScheduleCodeSpec, ItemHarmonizedTariffScheduleCodeEdit, EditItemHarmonizedTariffScheduleCodeResult, ItemHarmonizedTariffScheduleCode, ItemHarmonizedTariffScheduleCode> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
059                        new SecurityRoleDefinition(SecurityRoleGroups.ItemHarmonizedTariffScheduleCode.name(), SecurityRoles.Edit.name())
060                        ))
061                ));
062
063        SPEC_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("HarmonizedTariffScheduleCodeUseTypeName", FieldType.ENTITY_NAME, true, null, null)
067                );
068
069        EDIT_FIELD_DEFINITIONS = List.of(
070                new FieldDefinition("HarmonizedTariffScheduleCodeName", FieldType.ENTITY_NAME, true, null, null)
071                );
072    }
073
074    /** Creates a new instance of EditItemHarmonizedTariffScheduleCodeCommand */
075    public EditItemHarmonizedTariffScheduleCodeCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078
079    @Override
080    public EditItemHarmonizedTariffScheduleCodeResult getResult() {
081        return ItemResultFactory.getEditItemHarmonizedTariffScheduleCodeResult();
082    }
083
084    @Override
085    public ItemHarmonizedTariffScheduleCodeEdit getEdit() {
086        return ItemEditFactory.getItemHarmonizedTariffScheduleCodeEdit();
087    }
088
089    GeoCode countryGeoCode;
090    HarmonizedTariffScheduleCodeUseType harmonizedTariffScheduleCodeUseType;
091
092    @Override
093    public ItemHarmonizedTariffScheduleCode getEntity(EditItemHarmonizedTariffScheduleCodeResult result) {
094        var itemControl = Session.getModelController(ItemControl.class);
095        ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode = null;
096        var itemName = spec.getItemName();
097        var item = itemControl.getItemByName(itemName);
098        
099        if(item != null) {
100            var geoControl = Session.getModelController(GeoControl.class);
101            var countryName = spec.getCountryName();
102            
103            countryGeoCode = geoControl.getCountryByAlias(countryName);
104            
105            if(countryGeoCode != null) {
106                var harmonizedTariffScheduleCodeUseTypeName = spec.getHarmonizedTariffScheduleCodeUseTypeName();
107                
108                harmonizedTariffScheduleCodeUseType = itemControl.getHarmonizedTariffScheduleCodeUseTypeByName(harmonizedTariffScheduleCodeUseTypeName);
109
110                if(harmonizedTariffScheduleCodeUseType != null) {
111                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                        itemHarmonizedTariffScheduleCode = itemControl.getItemHarmonizedTariffScheduleCode(item,
113                                countryGeoCode, harmonizedTariffScheduleCodeUseType);
114                    } else { // EditMode.UPDATE
115                        itemHarmonizedTariffScheduleCode = itemControl.getItemHarmonizedTariffScheduleCodeForUpdate(item, countryGeoCode,
116                                harmonizedTariffScheduleCodeUseType);
117                    }
118
119                    if(itemHarmonizedTariffScheduleCode == null) {
120                        addExecutionError(ExecutionErrors.UnknownItemHarmonizedTariffScheduleCode.name(), itemName, countryName, harmonizedTariffScheduleCodeUseTypeName);
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeUseTypeName.name(), harmonizedTariffScheduleCodeUseTypeName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
130        }
131
132        return itemHarmonizedTariffScheduleCode;
133    }
134
135    @Override
136    public ItemHarmonizedTariffScheduleCode getLockEntity(ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode) {
137        return itemHarmonizedTariffScheduleCode;
138    }
139
140    @Override
141    public void fillInResult(EditItemHarmonizedTariffScheduleCodeResult result, ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode) {
142        var itemControl = Session.getModelController(ItemControl.class);
143
144        result.setItemHarmonizedTariffScheduleCode(itemControl.getItemHarmonizedTariffScheduleCodeTransfer(getUserVisit(), itemHarmonizedTariffScheduleCode));
145    }
146
147    @Override
148    public void doLock(ItemHarmonizedTariffScheduleCodeEdit edit, ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode) {
149        var itemHarmonizedTariffScheduleCodeDetail = itemHarmonizedTariffScheduleCode.getLastDetail();
150        
151        edit.setHarmonizedTariffScheduleCodeName(itemHarmonizedTariffScheduleCodeDetail.getHarmonizedTariffScheduleCode().getLastDetail().getHarmonizedTariffScheduleCodeName());
152    }
153
154    HarmonizedTariffScheduleCode harmonizedTariffScheduleCode;
155    
156    @Override
157    public void canUpdate(ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode) {
158        var itemControl = Session.getModelController(ItemControl.class);
159        var harmonizedTariffScheduleCodeName = edit.getHarmonizedTariffScheduleCodeName();
160        
161        harmonizedTariffScheduleCode = itemControl.getHarmonizedTariffScheduleCodeByName(countryGeoCode, harmonizedTariffScheduleCodeName);
162
163        if(harmonizedTariffScheduleCode != null) {
164            var harmonizedTariffScheduleCodeUse = itemControl.getHarmonizedTariffScheduleCodeUse(harmonizedTariffScheduleCode,
165                    harmonizedTariffScheduleCodeUseType);
166
167            if(harmonizedTariffScheduleCodeUse == null) {
168                addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeUse.name(), spec.getCountryName(), harmonizedTariffScheduleCodeName,
169                        spec.getHarmonizedTariffScheduleCodeUseTypeName());
170            }
171        } else {
172            addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeName.name(), spec.getCountryName(), harmonizedTariffScheduleCodeName);
173        }
174    }
175
176    @Override
177    public void doUpdate(ItemHarmonizedTariffScheduleCode itemHarmonizedTariffScheduleCode) {
178        var itemControl = Session.getModelController(ItemControl.class);
179        var itemHarmonizedTariffScheduleCodeDetailValue = itemControl.getItemHarmonizedTariffScheduleCodeDetailValueForUpdate(itemHarmonizedTariffScheduleCode);
180        
181        itemHarmonizedTariffScheduleCodeDetailValue.setHarmonizedTariffScheduleCodePK(harmonizedTariffScheduleCode.getPrimaryKey());
182        
183        itemControl.updateItemHarmonizedTariffScheduleCodeFromValue(itemHarmonizedTariffScheduleCodeDetailValue, getPartyPK());
184    }
185
186}