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