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.form.CreateItemWeightForm; 020import com.echothree.model.control.item.server.control.ItemControl; 021import com.echothree.model.control.item.server.logic.ItemWeightTypeLogic; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.control.uom.common.UomConstants; 026import com.echothree.model.control.uom.server.control.UomControl; 027import com.echothree.model.control.uom.server.util.Conversion; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class CreateItemWeightCommand 042 extends BaseSimpleCommand<CreateItemWeightForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 051 new SecurityRoleDefinition(SecurityRoleGroups.ItemWeight.name(), SecurityRoles.Create.name()) 052 )) 053 )); 054 055 FORM_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 057 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("ItemWeightTypeName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("WeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("Weight", FieldType.UNSIGNED_LONG, true, null, null) 061 ); 062 } 063 064 @Inject 065 ItemControl itemControl; 066 067 @Inject 068 UomControl uomControl; 069 070 @Inject 071 ItemWeightTypeLogic itemWeightTypeLogic; 072 073 074 /** Creates a new instance of CreateItemWeightCommand */ 075 public CreateItemWeightCommand() { 076 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 077 } 078 079 @Override 080 protected BaseResult execute() { 081 var itemName = form.getItemName(); 082 var item = itemControl.getItemByName(itemName); 083 084 if(item != null) { 085 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 086 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), 087 unitOfMeasureTypeName); 088 089 if(unitOfMeasureType != null) { 090 var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType); 091 092 if(itemUnitOfMeasureType != null) { 093 var itemWeightType = itemWeightTypeLogic.getItemWeightTypeByName(this, form.getItemWeightTypeName()); 094 095 if(!hasExecutionErrors()) { 096 var itemWeight = itemControl.getItemWeight(item, unitOfMeasureType, itemWeightType); 097 098 if(itemWeight == null) { 099 var weightUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_WEIGHT); 100 var weightUnitOfMeasureTypeName = form.getWeightUnitOfMeasureTypeName(); 101 var weightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(weightUnitOfMeasureKind, 102 weightUnitOfMeasureTypeName); 103 104 if(weightUnitOfMeasureType != null) { 105 var weight = Long.valueOf(form.getWeight()); 106 107 if(weight > 0) { 108 var conversion = new Conversion(uomControl, weightUnitOfMeasureType, weight); 109 110 conversion.convertToLowestUnitOfMeasureType(); 111 itemControl.createItemWeight(item, unitOfMeasureType, itemWeightType, conversion.getQuantity(), getPartyPK()); 112 } else { 113 addExecutionError(ExecutionErrors.InvalidWeight.name(), weight); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureTypeName.name(), weightUnitOfMeasureTypeName); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.DuplicateItemWeight.name(), item.getLastDetail().getItemName(), 120 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 121 itemWeightType.getLastDetail().getItemWeightTypeName()); 122 } 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 132 } 133 134 return null; 135 } 136 137}