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