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