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.CreateItemVolumeForm; 020import com.echothree.model.control.item.server.control.ItemControl; 021import com.echothree.model.control.item.server.logic.ItemVolumeTypeLogic; 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.Arrays; 038import java.util.Collections; 039import java.util.List; 040import javax.enterprise.context.RequestScoped; 041 042@RequestScoped 043public class CreateItemVolumeCommand 044 extends BaseSimpleCommand<CreateItemVolumeForm> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> FORM_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.ItemVolume.name(), SecurityRoles.Create.name()) 054 )) 055 )); 056 057 FORM_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null), 062 new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null), 064 new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null) 066 ); 067 } 068 069 /** Creates a new instance of CreateItemVolumeCommand */ 070 public CreateItemVolumeCommand() { 071 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 072 } 073 074 @Override 075 protected BaseResult execute() { 076 var itemControl = Session.getModelController(ItemControl.class); 077 var itemName = form.getItemName(); 078 var item = itemControl.getItemByName(itemName); 079 080 if(item != null) { 081 var uomControl = Session.getModelController(UomControl.class); 082 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 083 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), 084 unitOfMeasureTypeName); 085 086 if(unitOfMeasureType != null) { 087 var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType); 088 089 if(itemUnitOfMeasureType != null) { 090 var itemVolumeType = ItemVolumeTypeLogic.getInstance().getItemVolumeTypeByName(this, form.getItemVolumeTypeName()); 091 092 if(!hasExecutionErrors()) { 093 var itemVolume = itemControl.getItemVolume(item, unitOfMeasureType, itemVolumeType); 094 095 if(itemVolume == null) { 096 var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME); 097 098 if(volumeUnitOfMeasureKind != null) { 099 var heightUnitOfMeasureTypeName = form.getHeightUnitOfMeasureTypeName(); 100 var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, 101 heightUnitOfMeasureTypeName); 102 103 if(heightUnitOfMeasureType != null) { 104 var height = Long.valueOf(form.getHeight()); 105 106 if(height > 0) { 107 var widthUnitOfMeasureTypeName = form.getWidthUnitOfMeasureTypeName(); 108 var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, 109 widthUnitOfMeasureTypeName); 110 111 if(widthUnitOfMeasureType != null) { 112 var width = Long.valueOf(form.getWidth()); 113 114 if(width > 0) { 115 var depthUnitOfMeasureTypeName = form.getDepthUnitOfMeasureTypeName(); 116 var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, 117 depthUnitOfMeasureTypeName); 118 119 if(depthUnitOfMeasureType != null) { 120 var depth = Long.valueOf(form.getDepth()); 121 122 if(depth > 0) { 123 var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType(); 124 var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType(); 125 var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType(); 126 127 itemControl.createItemVolume(item, unitOfMeasureType, itemVolumeType, 128 heightConversion.getQuantity(), widthConversion.getQuantity(), 129 depthConversion.getQuantity(), getPartyPK()); 130 } else { 131 addExecutionError(ExecutionErrors.InvalidDepth.name(), depth); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.InvalidWidth.name(), width); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.InvalidHeight.name(), height); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name()); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.DuplicateItemVolume.name(), item.getLastDetail().getItemName(), 153 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 154 itemVolumeType.getLastDetail().getItemVolumeTypeName()); 155 } 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 165 } 166 167 return null; 168 } 169 170}