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