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.InventoryEditFactory; 020import com.echothree.control.user.inventory.common.edit.InventoryLocationGroupEdit; 021import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 022import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupSpec; 023import com.echothree.model.control.inventory.server.control.InventoryControl; 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.warehouse.server.control.WarehouseControl; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.command.EditMode; 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.BaseEditCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import com.echothree.util.server.persistence.Session; 038import java.util.List; 039import javax.enterprise.context.Dependent; 040 041@Dependent 042public class EditInventoryLocationGroupCommand 043 extends BaseEditCommand<InventoryLocationGroupSpec, InventoryLocationGroupEdit> { 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.InventoryLocationGroup.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null) 060 ); 061 062 EDIT_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 065 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 067 ); 068 } 069 070 /** Creates a new instance of EditInventoryLocationGroupCommand */ 071 public EditInventoryLocationGroupCommand() { 072 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 073 } 074 075 @Override 076 protected BaseResult execute() { 077 var warehouseControl = Session.getModelController(WarehouseControl.class); 078 var result = InventoryResultFactory.getEditInventoryLocationGroupResult(); 079 var warehouseName = spec.getWarehouseName(); 080 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 081 082 if(warehouse != null) { 083 var inventoryControl = Session.getModelController(InventoryControl.class); 084 var warehouseParty = warehouse.getParty(); 085 086 if(editMode.equals(EditMode.LOCK)) { 087 var inventoryLocationGroupName = spec.getInventoryLocationGroupName(); 088 var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 089 090 if(inventoryLocationGroup != null) { 091 result.setInventoryLocationGroup(inventoryControl.getInventoryLocationGroupTransfer(getUserVisit(), inventoryLocationGroup)); 092 093 if(lockEntity(inventoryLocationGroup)) { 094 var inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage()); 095 var edit = InventoryEditFactory.getInventoryLocationGroupEdit(); 096 var inventoryLocationGroupDetail = inventoryLocationGroup.getLastDetail(); 097 098 result.setEdit(edit); 099 edit.setInventoryLocationGroupName(inventoryLocationGroupDetail.getInventoryLocationGroupName()); 100 edit.setIsDefault(inventoryLocationGroupDetail.getIsDefault().toString()); 101 edit.setSortOrder(inventoryLocationGroupDetail.getSortOrder().toString()); 102 103 if(inventoryLocationGroupDescription != null) { 104 edit.setDescription(inventoryLocationGroupDescription.getDescription()); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 108 } 109 110 result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup)); 111 } else { 112 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 113 } 114 } else if(editMode.equals(EditMode.UPDATE)) { 115 var inventoryLocationGroupName = spec.getInventoryLocationGroupName(); 116 var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByNameForUpdate(warehouseParty, inventoryLocationGroupName); 117 118 if(inventoryLocationGroup != null) { 119 inventoryLocationGroupName = edit.getInventoryLocationGroupName(); 120 var duplicateInventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 121 122 if(duplicateInventoryLocationGroup == null || inventoryLocationGroup.equals(duplicateInventoryLocationGroup)) { 123 if(lockEntityForUpdate(inventoryLocationGroup)) { 124 try { 125 var partyPK = getPartyPK(); 126 var inventoryLocationGroupDetailValue = inventoryControl.getInventoryLocationGroupDetailValueForUpdate(inventoryLocationGroup); 127 var inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescriptionForUpdate(inventoryLocationGroup, getPreferredLanguage()); 128 var description = edit.getDescription(); 129 130 inventoryLocationGroupDetailValue.setInventoryLocationGroupName(edit.getInventoryLocationGroupName()); 131 inventoryLocationGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 132 inventoryLocationGroupDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 133 134 inventoryControl.updateInventoryLocationGroupFromValue(inventoryLocationGroupDetailValue, partyPK); 135 136 if(inventoryLocationGroupDescription == null && description != null) { 137 inventoryControl.createInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage(), description, partyPK); 138 } else if(inventoryLocationGroupDescription != null && description == null) { 139 inventoryControl.deleteInventoryLocationGroupDescription(inventoryLocationGroupDescription, partyPK); 140 } else if(inventoryLocationGroupDescription != null && description != null) { 141 var inventoryLocationGroupDescriptionValue = inventoryControl.getInventoryLocationGroupDescriptionValue(inventoryLocationGroupDescription); 142 143 inventoryLocationGroupDescriptionValue.setDescription(description); 144 inventoryControl.updateInventoryLocationGroupDescriptionFromValue(inventoryLocationGroupDescriptionValue, partyPK); 145 } 146 } finally { 147 unlockEntity(inventoryLocationGroup); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.EntityLockStale.name()); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupName.name(), inventoryLocationGroupName); 154 } 155 } else { 156 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 157 } 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 161 } 162 163 return result; 164 } 165 166}