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