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.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 com.echothree.util.server.persistence.Session; 037import java.util.List; 038import javax.enterprise.context.RequestScoped; 039 040@RequestScoped 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 /** Creates a new instance of CreateItemWeightCommand */ 065 public CreateItemWeightCommand() { 066 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 067 } 068 069 @Override 070 protected BaseResult execute() { 071 var itemControl = Session.getModelController(ItemControl.class); 072 var itemName = form.getItemName(); 073 var item = itemControl.getItemByName(itemName); 074 075 if(item != null) { 076 var uomControl = Session.getModelController(UomControl.class); 077 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 078 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), 079 unitOfMeasureTypeName); 080 081 if(unitOfMeasureType != null) { 082 var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType); 083 084 if(itemUnitOfMeasureType != null) { 085 var itemWeightType = ItemWeightTypeLogic.getInstance().getItemWeightTypeByName(this, form.getItemWeightTypeName()); 086 087 if(!hasExecutionErrors()) { 088 var itemWeight = itemControl.getItemWeight(item, unitOfMeasureType, itemWeightType); 089 090 if(itemWeight == null) { 091 var weightUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_WEIGHT); 092 var weightUnitOfMeasureTypeName = form.getWeightUnitOfMeasureTypeName(); 093 var weightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(weightUnitOfMeasureKind, 094 weightUnitOfMeasureTypeName); 095 096 if(weightUnitOfMeasureType != null) { 097 var weight = Long.valueOf(form.getWeight()); 098 099 if(weight > 0) { 100 var conversion = new Conversion(uomControl, weightUnitOfMeasureType, weight); 101 102 conversion.convertToLowestUnitOfMeasureType(); 103 itemControl.createItemWeight(item, unitOfMeasureType, itemWeightType, conversion.getQuantity(), getPartyPK()); 104 } else { 105 addExecutionError(ExecutionErrors.InvalidWeight.name(), weight); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureTypeName.name(), weightUnitOfMeasureTypeName); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.DuplicateItemWeight.name(), item.getLastDetail().getItemName(), 112 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 113 itemWeightType.getLastDetail().getItemWeightTypeName()); 114 } 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 124 } 125 126 return null; 127 } 128 129}