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