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