001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.content.server.command;
018
019import com.echothree.control.user.content.common.form.DeleteContentCategoryItemForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.content.server.control.ContentControl;
022import com.echothree.model.control.content.server.logic.ContentLogic;
023import com.echothree.model.control.inventory.server.control.InventoryControl;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.uom.server.control.UomControl;
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.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class DeleteContentCategoryItemCommand
044        extends BaseSimpleCommand<DeleteContentCategoryItemForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Delete.name())
054                        ))
055                ));
056        
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null)
065                );
066    }
067    
068    /** Creates a new instance of DeleteContentCategoryItemCommand */
069    public DeleteContentCategoryItemCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
071    }
072    
073    @Override
074    protected BaseResult execute() {
075        var contentControl = Session.getModelController(ContentControl.class);
076        var contentCollectionName = form.getContentCollectionName();
077        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
078        
079        if(contentCollection != null) {
080            var contentCatalogName = form.getContentCatalogName();
081            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
082            
083            if(contentCatalog != null) {
084                var contentCategoryName = form.getContentCategoryName();
085                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
086                
087                if(contentCategory != null) {
088                    var itemControl = Session.getModelController(ItemControl.class);
089                    var itemName = form.getItemName();
090                    var item = itemControl.getItemByName(itemName);
091                    
092                    if(item != null) {
093                        var inventoryControl = Session.getModelController(InventoryControl.class);
094                        var inventoryConditionName = form.getInventoryConditionName();
095                        var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
096                        
097                        if(inventoryCondition != null) {
098                            var uomControl = Session.getModelController(UomControl.class);
099                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
100                            var itemDetail = item.getLastDetail();
101                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
102                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
103                            
104                            if(unitOfMeasureType != null) {
105                                var accountingControl = Session.getModelController(AccountingControl.class);
106                                var currencyIsoName = form.getCurrencyIsoName();
107                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
108                                
109                                if(currency != null) {
110                                    var contentCatalogItem = contentControl.getContentCatalogItemForUpdate(contentCatalog,
111                                            item, inventoryCondition, unitOfMeasureType, currency);
112                                    
113                                    if(contentCatalogItem != null) {
114                                        var contentCategoryItem = contentControl.getContentCategoryItemForUpdate(contentCategory,
115                                                contentCatalogItem);
116                                        
117                                        if(contentCategoryItem != null) {
118                                            ContentLogic.getInstance().deleteContentCategoryItem(contentCategoryItem, getPartyPK());
119                                        } else {
120                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(),
121                                                    contentCollectionName, contentCatalogName, contentCategoryName, itemName,
122                                                    inventoryConditionName, unitOfMeasureTypeName, currencyIsoName);
123                                        }
124                                    } else {
125                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollectionName,
126                                                contentCatalogName, itemName, inventoryConditionName, unitOfMeasureTypeName,
127                                                currencyIsoName);
128                                    }
129                                } else {
130                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
131                                }
132                            } else {
133                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
134                            }
135                        } else {
136                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
137                        }
138                    } else {
139                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
140                    }
141                } else {
142                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
143                }
144            } else {
145                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
146            }
147        } else {
148            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
149        }
150        
151        return null;
152    }
153    
154}