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.InventoryConditionControl;
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 java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
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    @Inject
069    AccountingControl accountingControl;
070
071    @Inject
072    ContentControl contentControl;
073
074    @Inject
075    InventoryConditionControl inventoryConditionControl;
076
077    @Inject
078    ItemControl itemControl;
079
080    @Inject
081    UomControl uomControl;
082
083    @Inject
084    ContentLogic contentLogic;
085
086    
087    /** Creates a new instance of DeleteContentCategoryItemCommand */
088    public DeleteContentCategoryItemCommand() {
089        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
090    }
091    
092    @Override
093    protected BaseResult execute() {
094        var contentCollectionName = form.getContentCollectionName();
095        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
096        
097        if(contentCollection != null) {
098            var contentCatalogName = form.getContentCatalogName();
099            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
100            
101            if(contentCatalog != null) {
102                var contentCategoryName = form.getContentCategoryName();
103                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
104                
105                if(contentCategory != null) {
106                    var itemName = form.getItemName();
107                    var item = itemControl.getItemByName(itemName);
108                    
109                    if(item != null) {
110                        var inventoryConditionName = form.getInventoryConditionName();
111                        var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
112                        
113                        if(inventoryCondition != null) {
114                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
115                            var itemDetail = item.getLastDetail();
116                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
117                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
118                            
119                            if(unitOfMeasureType != null) {
120                                var currencyIsoName = form.getCurrencyIsoName();
121                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
122                                
123                                if(currency != null) {
124                                    var contentCatalogItem = contentControl.getContentCatalogItemForUpdate(contentCatalog,
125                                            item, inventoryCondition, unitOfMeasureType, currency);
126                                    
127                                    if(contentCatalogItem != null) {
128                                        var contentCategoryItem = contentControl.getContentCategoryItemForUpdate(contentCategory,
129                                                contentCatalogItem);
130                                        
131                                        if(contentCategoryItem != null) {
132                                            contentLogic.deleteContentCategoryItem(contentCategoryItem, getPartyPK());
133                                        } else {
134                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(),
135                                                    contentCollectionName, contentCatalogName, contentCategoryName, itemName,
136                                                    inventoryConditionName, unitOfMeasureTypeName, currencyIsoName);
137                                        }
138                                    } else {
139                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollectionName,
140                                                contentCatalogName, itemName, inventoryConditionName, unitOfMeasureTypeName,
141                                                currencyIsoName);
142                                    }
143                                } else {
144                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
145                                }
146                            } else {
147                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
148                            }
149                        } else {
150                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
151                        }
152                    } else {
153                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
154                    }
155                } else {
156                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
157                }
158            } else {
159                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
160            }
161        } else {
162            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
163        }
164        
165        return null;
166    }
167    
168}