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.inventory.server.command; 018 019import com.echothree.control.user.inventory.common.edit.InventoryCostingMethodEdit; 020import com.echothree.control.user.inventory.common.edit.InventoryEditFactory; 021import com.echothree.control.user.inventory.common.result.EditInventoryCostingMethodResult; 022import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 023import com.echothree.control.user.inventory.common.spec.InventoryCostingMethodUniversalSpec; 024import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl; 025import com.echothree.model.control.inventory.server.logic.InventoryCostingMethodLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod; 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.server.control.BaseAbstractEditCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.List; 038import javax.inject.Inject; 039import javax.enterprise.context.Dependent; 040 041@Dependent 042public class EditInventoryCostingMethodCommand 043 extends BaseAbstractEditCommand<InventoryCostingMethodUniversalSpec, InventoryCostingMethodEdit, EditInventoryCostingMethodResult, InventoryCostingMethod, InventoryCostingMethod> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 053 new SecurityRoleDefinition(SecurityRoleGroups.InventoryCostingMethod.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("InventoryCostingMethodName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 060 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("InventoryCostingMethodName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 InventoryCostingMethodControl inventoryCostingMethodControl; 073 074 @Inject 075 InventoryCostingMethodLogic inventoryCostingMethodLogic; 076 077 /** Creates a new instance of EditInventoryCostingMethodCommand */ 078 public EditInventoryCostingMethodCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditInventoryCostingMethodResult getResult() { 084 return InventoryResultFactory.getEditInventoryCostingMethodResult(); 085 } 086 087 @Override 088 public InventoryCostingMethodEdit getEdit() { 089 return InventoryEditFactory.getInventoryCostingMethodEdit(); 090 } 091 092 @Override 093 public InventoryCostingMethod getEntity(EditInventoryCostingMethodResult result) { 094 var inventoryCostingMethodName = spec.getInventoryCostingMethodName(); 095 var inventoryCostingMethod = inventoryCostingMethodLogic.getInventoryCostingMethodByUniversalSpec(this, spec, false, 096 editModeToEntityPermission(editMode)); 097 098 if(inventoryCostingMethod != null) { 099 result.setInventoryCostingMethod(inventoryCostingMethodControl.getInventoryCostingMethodTransfer(getUserVisit(), inventoryCostingMethod)); 100 } else { 101 addExecutionError(ExecutionErrors.UnknownInventoryCostingMethodName.name(), inventoryCostingMethodName); 102 } 103 104 return inventoryCostingMethod; 105 } 106 107 @Override 108 public InventoryCostingMethod getLockEntity(InventoryCostingMethod inventoryCostingMethod) { 109 return inventoryCostingMethod; 110 } 111 112 @Override 113 public void fillInResult(EditInventoryCostingMethodResult result, InventoryCostingMethod inventoryCostingMethod) { 114 result.setInventoryCostingMethod(inventoryCostingMethodControl.getInventoryCostingMethodTransfer(getUserVisit(), inventoryCostingMethod)); 115 } 116 117 @Override 118 public void doLock(InventoryCostingMethodEdit edit, InventoryCostingMethod inventoryCostingMethod) { 119 var inventoryCostingMethodDescription = inventoryCostingMethodControl.getInventoryCostingMethodDescription(inventoryCostingMethod, getPreferredLanguage()); 120 var inventoryCostingMethodDetail = inventoryCostingMethod.getLastDetail(); 121 122 edit.setInventoryCostingMethodName(inventoryCostingMethodDetail.getInventoryCostingMethodName()); 123 edit.setIsDefault(inventoryCostingMethodDetail.getIsDefault().toString()); 124 edit.setSortOrder(inventoryCostingMethodDetail.getSortOrder().toString()); 125 126 if(inventoryCostingMethodDescription != null) { 127 edit.setDescription(inventoryCostingMethodDescription.getDescription()); 128 } 129 } 130 131 @Override 132 public void canUpdate(InventoryCostingMethod inventoryCostingMethod) { 133 var inventoryCostingMethodName = edit.getInventoryCostingMethodName(); 134 var duplicateInventoryCostingMethod = inventoryCostingMethodControl.getInventoryCostingMethodByName(inventoryCostingMethodName); 135 136 if(duplicateInventoryCostingMethod != null && !inventoryCostingMethod.equals(duplicateInventoryCostingMethod)) { 137 addExecutionError(ExecutionErrors.DuplicateInventoryCostingMethodName.name(), inventoryCostingMethodName); 138 } 139 } 140 141 @Override 142 public void doUpdate(InventoryCostingMethod inventoryCostingMethod) { 143 var partyPK = getPartyPK(); 144 var inventoryCostingMethodDetailValue = inventoryCostingMethodControl.getInventoryCostingMethodDetailValueForUpdate(inventoryCostingMethod); 145 var inventoryCostingMethodDescription = inventoryCostingMethodControl.getInventoryCostingMethodDescriptionForUpdate(inventoryCostingMethod, getPreferredLanguage()); 146 var description = edit.getDescription(); 147 148 inventoryCostingMethodDetailValue.setInventoryCostingMethodName(edit.getInventoryCostingMethodName()); 149 inventoryCostingMethodDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 150 inventoryCostingMethodDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 151 152 inventoryCostingMethodLogic.updateInventoryCostingMethodFromValue(inventoryCostingMethodDetailValue, partyPK); 153 154 if(inventoryCostingMethodDescription == null && description != null) { 155 inventoryCostingMethodControl.createInventoryCostingMethodDescription(inventoryCostingMethod, getPreferredLanguage(), description, partyPK); 156 } else { 157 if(inventoryCostingMethodDescription != null && description == null) { 158 inventoryCostingMethodControl.deleteInventoryCostingMethodDescription(inventoryCostingMethodDescription, partyPK); 159 } else { 160 if(inventoryCostingMethodDescription != null && description != null) { 161 var inventoryCostingMethodDescriptionValue = inventoryCostingMethodControl.getInventoryCostingMethodDescriptionValue(inventoryCostingMethodDescription); 162 163 inventoryCostingMethodDescriptionValue.setDescription(description); 164 inventoryCostingMethodControl.updateInventoryCostingMethodDescriptionFromValue(inventoryCostingMethodDescriptionValue, partyPK); 165 } 166 } 167 } 168 } 169 170}