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