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