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.item.server.command;
018
019import com.echothree.control.user.item.common.form.DeleteItemKitMemberForm;
020import com.echothree.model.control.inventory.server.control.InventoryControl;
021import com.echothree.model.control.item.common.ItemConstants;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.uom.server.control.UomControl;
024import com.echothree.model.data.inventory.server.entity.InventoryCondition;
025import com.echothree.model.data.item.server.entity.Item;
026import com.echothree.model.data.item.server.entity.ItemDetail;
027import com.echothree.model.data.item.server.entity.ItemKitMember;
028import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039
040public class DeleteItemKitMemberCommand
041        extends BaseSimpleCommand<DeleteItemKitMemberForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
047            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
048            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
049            new FieldDefinition("UnitOfMeasureTypeName", FieldType.PERCENT, true, null, null),
050            new FieldDefinition("MemberItemName", FieldType.ENTITY_NAME, true, null, null),
051            new FieldDefinition("MemberInventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
052            new FieldDefinition("MemberUnitOfMeasureTypeName", FieldType.PERCENT, true, null, null)
053        ));
054    }
055    
056    /** Creates a new instance of DeleteItemKitMemberCommand */
057    public DeleteItemKitMemberCommand(UserVisitPK userVisitPK, DeleteItemKitMemberForm form) {
058        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var itemControl = Session.getModelController(ItemControl.class);
064        String itemName = form.getItemName();
065        Item item = itemControl.getItemByName(itemName);
066        
067        if(item != null) {
068            ItemDetail itemDetail = item.getLastDetail();
069            String itemTypeName = itemDetail.getItemType().getItemTypeName();
070            
071            if(itemTypeName.equals(ItemConstants.ItemType_KIT)) {
072                var inventoryControl = Session.getModelController(InventoryControl.class);
073                String inventoryConditionName = form.getInventoryConditionName();
074                InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
075                
076                if(inventoryCondition != null) {
077                    var uomControl = Session.getModelController(UomControl.class);
078                    String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
079                    UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(itemDetail.getUnitOfMeasureKind(), unitOfMeasureTypeName);
080                    
081                    if(unitOfMeasureType != null) {
082                        String memberItemName = form.getMemberItemName();
083                        Item memberItem = itemControl.getItemByName(memberItemName);
084                        
085                        if(memberItem != null) {
086                            String memberInventoryConditionName = form.getMemberInventoryConditionName();
087                            InventoryCondition memberInventoryCondition = inventoryControl.getInventoryConditionByName(memberInventoryConditionName);
088                            
089                            if(memberInventoryCondition != null) {
090                                String memberUnitOfMeasureTypeName = form.getMemberUnitOfMeasureTypeName();
091                                UnitOfMeasureType memberUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(memberItem.getLastDetail().getUnitOfMeasureKind(), memberUnitOfMeasureTypeName);
092                                
093                                if(memberUnitOfMeasureType != null) {
094                                    ItemKitMember itemKitMember = itemControl.getItemKitMemberForUpdate(item, inventoryCondition,
095                                            unitOfMeasureType, memberItem, memberInventoryCondition, memberUnitOfMeasureType);
096                                    
097                                    if(itemKitMember != null) {
098                                        itemControl.deleteItemKitMember(itemKitMember, getPartyPK());
099                                    } else {
100                                        addExecutionError(ExecutionErrors.UnknownItemKitMember.name());
101                                    }
102                                } else {
103                                    addExecutionError(ExecutionErrors.UnknownMemberUnitOfMeasureTypeName.name(), memberUnitOfMeasureTypeName);
104                                }
105                            } else {
106                                addExecutionError(ExecutionErrors.UnknownMemberInventoryConditionName.name(), memberInventoryConditionName);
107                            }
108                        } else {
109                            addExecutionError(ExecutionErrors.UnknownMemberItemName.name(), memberItemName);
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
113                    }
114                } else {
115                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
116                }
117            } else {
118                addExecutionError(ExecutionErrors.InvalidItemType.name(), itemTypeName);
119            }
120        } else {
121            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
122        }
123        
124        return null;
125    }
126    
127}