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.InventoryLocationGroupDescriptionEdit; 021import com.echothree.control.user.inventory.common.form.EditInventoryLocationGroupDescriptionForm; 022import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 023import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupDescriptionSpec; 024import com.echothree.model.control.inventory.server.control.InventoryControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.warehouse.server.control.WarehouseControl; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.ArrayList; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044 045@Dependent 046public class EditInventoryLocationGroupDescriptionCommand 047 extends BaseEditCommand<InventoryLocationGroupDescriptionSpec, InventoryLocationGroupDescriptionEdit> { 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.InventoryLocationGroup.name(), SecurityRoles.Description.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 065 ); 066 067 EDIT_FIELD_DEFINITIONS = List.of( 068 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 069 ); 070 } 071 072 /** Creates a new instance of EditInventoryLocationGroupDescriptionCommand */ 073 public EditInventoryLocationGroupDescriptionCommand() { 074 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var warehouseControl = Session.getModelController(WarehouseControl.class); 080 var result = InventoryResultFactory.getEditInventoryLocationGroupDescriptionResult(); 081 var warehouseName = spec.getWarehouseName(); 082 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 083 084 if(warehouse != null) { 085 var inventoryControl = Session.getModelController(InventoryControl.class); 086 var warehouseParty = warehouse.getParty(); 087 var inventoryLocationGroupName = spec.getInventoryLocationGroupName(); 088 var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 089 090 if(inventoryLocationGroup != null) { 091 var partyControl = Session.getModelController(PartyControl.class); 092 var languageIsoName = spec.getLanguageIsoName(); 093 var language = partyControl.getLanguageByIsoName(languageIsoName); 094 095 if(language != null) { 096 if(editMode.equals(EditMode.LOCK)) { 097 var inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescription(inventoryLocationGroup, language); 098 099 if(inventoryLocationGroupDescription != null) { 100 result.setInventoryLocationGroupDescription(inventoryControl.getInventoryLocationGroupDescriptionTransfer(getUserVisit(), inventoryLocationGroupDescription)); 101 102 if(lockEntity(inventoryLocationGroup)) { 103 var edit = InventoryEditFactory.getInventoryLocationGroupDescriptionEdit(); 104 105 result.setEdit(edit); 106 edit.setDescription(inventoryLocationGroupDescription.getDescription()); 107 } else { 108 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 109 } 110 111 result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup)); 112 } else { 113 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupDescription.name()); 114 } 115 } else if(editMode.equals(EditMode.UPDATE)) { 116 var inventoryLocationGroupDescriptionValue = inventoryControl.getInventoryLocationGroupDescriptionValueForUpdate(inventoryLocationGroup, language); 117 118 if(inventoryLocationGroupDescriptionValue != null) { 119 if(lockEntityForUpdate(inventoryLocationGroup)) { 120 try { 121 var description = edit.getDescription(); 122 123 inventoryLocationGroupDescriptionValue.setDescription(description); 124 125 inventoryControl.updateInventoryLocationGroupDescriptionFromValue(inventoryLocationGroupDescriptionValue, getPartyPK()); 126 } finally { 127 unlockEntity(inventoryLocationGroup); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.EntityLockStale.name()); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupDescription.name()); 134 } 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 144 } 145 146 return result; 147 } 148 149}