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.form.DeletePartyInventoryLevelForm;
020import com.echothree.model.control.inventory.server.control.InventoryControl;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.common.validation.FieldDefinition;
025import com.echothree.util.common.validation.FieldType;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.server.persistence.Session;
028import java.util.Arrays;
029import java.util.Collections;
030import java.util.List;
031import javax.enterprise.context.RequestScoped;
032
033@RequestScoped
034public class DeletePartyInventoryLevelCommand
035        extends BasePartyInventoryLevelCommand<DeletePartyInventoryLevelForm> {
036    
037    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
038    
039    static {
040        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
041                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
042                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
043                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null)
046                ));
047    }
048    
049    /** Creates a new instance of DeletePartyInventoryLevelCommand */
050    public DeletePartyInventoryLevelCommand() {
051        super(FORM_FIELD_DEFINITIONS);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var inventoryControl = Session.getModelController(InventoryControl.class);
057        var party = getParty(form);
058        
059        if(party != null) {
060            var itemControl = Session.getModelController(ItemControl.class);
061            var itemName = form.getItemName();
062            var item = itemControl.getItemByName(itemName);
063            
064            if(item != null) {
065                var inventoryConditionName = form.getInventoryConditionName();
066                var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
067                
068                if(inventoryCondition != null) {
069                    var partyInventoryLevel = inventoryControl.getPartyInventoryLevelForUpdate(party, item, inventoryCondition);
070                    
071                    if(partyInventoryLevel != null) {
072                        inventoryControl.deletePartyInventoryLevel(partyInventoryLevel, getPartyPK());
073                    } else {
074                        addExecutionError(ExecutionErrors.UnknownPartyInventoryLevel.name());
075                    }
076                } else {
077                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
078                }
079            } else {
080                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
081            }
082        }
083        
084        return null;
085    }
086    
087}