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.tax.server.command; 018 019import com.echothree.control.user.tax.common.edit.ItemTaxClassificationEdit; 020import com.echothree.control.user.tax.common.edit.TaxEditFactory; 021import com.echothree.control.user.tax.common.form.EditItemTaxClassificationForm; 022import com.echothree.control.user.tax.common.result.EditItemTaxClassificationResult; 023import com.echothree.control.user.tax.common.result.TaxResultFactory; 024import com.echothree.control.user.tax.common.spec.ItemTaxClassificationSpec; 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.control.tax.server.control.TaxControl; 031import com.echothree.model.data.geo.server.entity.GeoCode; 032import com.echothree.model.data.tax.server.entity.ItemTaxClassification; 033import com.echothree.model.data.tax.server.entity.TaxClassification; 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 EditItemTaxClassificationCommand 049 extends BaseAbstractEditCommand<ItemTaxClassificationSpec, ItemTaxClassificationEdit, EditItemTaxClassificationResult, ItemTaxClassification, ItemTaxClassification> { 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.ItemTaxClassification.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 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("TaxClassificationName", FieldType.ENTITY_NAME, true, null, null) 070 ); 071 } 072 073 /** Creates a new instance of EditItemTaxClassificationCommand */ 074 public EditItemTaxClassificationCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditItemTaxClassificationResult getResult() { 080 return TaxResultFactory.getEditItemTaxClassificationResult(); 081 } 082 083 @Override 084 public ItemTaxClassificationEdit getEdit() { 085 return TaxEditFactory.getItemTaxClassificationEdit(); 086 } 087 088 GeoCode countryGeoCode; 089 090 @Override 091 public ItemTaxClassification getEntity(EditItemTaxClassificationResult result) { 092 var itemControl = Session.getModelController(ItemControl.class); 093 ItemTaxClassification itemTaxClassification = null; 094 var itemName = spec.getItemName(); 095 var item = itemControl.getItemByName(itemName); 096 097 if(item != null) { 098 var geoControl = Session.getModelController(GeoControl.class); 099 var countryName = spec.getCountryName(); 100 101 countryGeoCode = geoControl.getCountryByAlias(countryName); 102 103 if(countryGeoCode != null) { 104 var taxControl = Session.getModelController(TaxControl.class); 105 106 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 107 itemTaxClassification = taxControl.getItemTaxClassification(item, countryGeoCode); 108 } else { // EditMode.UPDATE 109 itemTaxClassification = taxControl.getItemTaxClassificationForUpdate(item, countryGeoCode); 110 } 111 112 if(itemTaxClassification == null) { 113 addExecutionError(ExecutionErrors.UnknownItemTaxClassification.name(), itemName, countryName); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 120 } 121 122 return itemTaxClassification; 123 } 124 125 @Override 126 public ItemTaxClassification getLockEntity(ItemTaxClassification itemTaxClassification) { 127 return itemTaxClassification; 128 } 129 130 @Override 131 public void fillInResult(EditItemTaxClassificationResult result, ItemTaxClassification itemTaxClassification) { 132 var taxControl = Session.getModelController(TaxControl.class); 133 134 result.setItemTaxClassification(taxControl.getItemTaxClassificationTransfer(getUserVisit(), itemTaxClassification)); 135 } 136 137 @Override 138 public void doLock(ItemTaxClassificationEdit edit, ItemTaxClassification itemTaxClassification) { 139 var itemTaxClassificationDetail = itemTaxClassification.getLastDetail(); 140 141 edit.setTaxClassificationName(itemTaxClassificationDetail.getTaxClassification().getLastDetail().getTaxClassificationName()); 142 } 143 144 TaxClassification harmonizedTariffScheduleCode; 145 146 @Override 147 public void canUpdate(ItemTaxClassification itemTaxClassification) { 148 var taxControl = Session.getModelController(TaxControl.class); 149 var harmonizedTariffScheduleCodeName = edit.getTaxClassificationName(); 150 151 harmonizedTariffScheduleCode = taxControl.getTaxClassificationByName(countryGeoCode, harmonizedTariffScheduleCodeName); 152 153 if(harmonizedTariffScheduleCode == null) { 154 addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), spec.getCountryName(), harmonizedTariffScheduleCodeName); 155 } 156 } 157 158 @Override 159 public void doUpdate(ItemTaxClassification itemTaxClassification) { 160 var taxControl = Session.getModelController(TaxControl.class); 161 var itemTaxClassificationDetailValue = taxControl.getItemTaxClassificationDetailValueForUpdate(itemTaxClassification); 162 163 itemTaxClassificationDetailValue.setTaxClassificationPK(harmonizedTariffScheduleCode.getPrimaryKey()); 164 165 taxControl.updateItemTaxClassificationFromValue(itemTaxClassificationDetailValue, getPartyPK()); 166 } 167 168}