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.CreateContentCategoryItemForm;
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 CreateContentCategoryItemCommand
044        extends BaseSimpleCommand<CreateContentCategoryItemForm> {
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.Create.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                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
067                );
068    }
069    
070    /** Creates a new instance of CreateContentCategoryItemCommand */
071    public CreateContentCategoryItemCommand() {
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 isDefault = Boolean.valueOf(form.getIsDefault());
113                                    var sortOrder = Integer.valueOf(form.getSortOrder());
114
115                                    ContentLogic.getInstance().createContentCategoryItem(this, contentCategory, item, inventoryCondition, unitOfMeasureType,
116                                            currency, isDefault, sortOrder, getPartyPK());
117                                } else {
118                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
119                                }
120                            } else {
121                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(),
122                                        unitOfMeasureTypeName);
123                            }
124                        } else {
125                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
126                        }
127                    } else {
128                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
129                    }
130                } else {
131                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCollectionName, contentCatalogName, contentCategoryName);
132                }
133            } else {
134                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollectionName, contentCatalogName);
135            }
136        } else {
137            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
138        }
139        
140        return null;
141    }
142    
143}