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.warehouse.server.command; 018 019import com.echothree.control.user.warehouse.common.edit.LocationCapacityEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditLocationCapacityForm; 022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 023import com.echothree.control.user.warehouse.common.spec.LocationCapacitySpec; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.uom.server.control.UomControl; 028import com.echothree.model.control.warehouse.server.control.WarehouseControl; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditLocationCapacityCommand 047 extends BaseEditCommand<LocationCapacitySpec, LocationCapacityEdit> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.LocationCapacity.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null) 066 )); 067 068 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("Capacity", FieldType.UNSIGNED_LONG, true, null, null) 070 )); 071 } 072 073 /** Creates a new instance of EditLocationCapacityCommand */ 074 public EditLocationCapacityCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var warehouseControl = Session.getModelController(WarehouseControl.class); 081 var result = WarehouseResultFactory.getEditLocationCapacityResult(); 082 var warehouseName = spec.getWarehouseName(); 083 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 084 085 if(warehouse != null) { 086 var locationName = spec.getLocationName(); 087 var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName); 088 089 if(location != null) { 090 var uomControl = Session.getModelController(UomControl.class); 091 var unitOfMeasureKindName = spec.getUnitOfMeasureKindName(); 092 var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName); 093 094 if(unitOfMeasureKind != null) { 095 var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName(); 096 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 097 098 if(unitOfMeasureType != null) { 099 if(editMode.equals(EditMode.LOCK)) { 100 var locationCapacity = warehouseControl.getLocationCapacity(location, unitOfMeasureType); 101 102 if(locationCapacity != null) { 103 result.setLocationCapacity(warehouseControl.getLocationCapacityTransfer(getUserVisit(), locationCapacity)); 104 105 if(lockEntity(location)) { 106 var edit = WarehouseEditFactory.getLocationCapacityEdit(); 107 108 result.setEdit(edit); 109 edit.setCapacity(locationCapacity.getCapacity().toString()); 110 } else { 111 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 112 } 113 114 result.setEntityLock(getEntityLockTransfer(location)); 115 } else { 116 addExecutionError(ExecutionErrors.UnknownLocationCapacity.name()); 117 } 118 } else if(editMode.equals(EditMode.UPDATE)) { 119 var locationCapacityValue = warehouseControl.getLocationCapacityValueForUpdate(location, unitOfMeasureType); 120 121 if(locationCapacityValue != null) { 122 if(lockEntityForUpdate(location)) { 123 try { 124 locationCapacityValue.setCapacity(Long.valueOf(edit.getCapacity())); 125 126 warehouseControl.updateLocationCapacityFromValue(locationCapacityValue, getPartyPK()); 127 } finally { 128 unlockEntity(location); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.EntityLockStale.name()); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownLocationCapacity.name()); 135 } 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 148 } 149 150 return result; 151 } 152 153}