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.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.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class CreateContentCategoryItemCommand
046        extends BaseSimpleCommand<CreateContentCategoryItemForm> {
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.Create.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                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
069                ));
070    }
071    
072    /** Creates a new instance of CreateContentCategoryItemCommand */
073    public CreateContentCategoryItemCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var contentControl = Session.getModelController(ContentControl.class);
080        var contentCollectionName = form.getContentCollectionName();
081        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
082        
083        if(contentCollection != null) {
084            var contentCatalogName = form.getContentCatalogName();
085            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
086            
087            if(contentCatalog != null) {
088                var contentCategoryName = form.getContentCategoryName();
089                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
090                
091                if(contentCategory != null) {
092                    var itemControl = Session.getModelController(ItemControl.class);
093                    var itemName = form.getItemName();
094                    var item = itemControl.getItemByName(itemName);
095                    
096                    if(item != null) {
097                        var inventoryControl = Session.getModelController(InventoryControl.class);
098                        var inventoryConditionName = form.getInventoryConditionName();
099                        var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
100                        
101                        if(inventoryCondition != null) {
102                            var uomControl = Session.getModelController(UomControl.class);
103                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
104                            var itemDetail = item.getLastDetail();
105                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
106                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
107                            
108                            if(unitOfMeasureType != null) {
109                                var accountingControl = Session.getModelController(AccountingControl.class);
110                                var currencyIsoName = form.getCurrencyIsoName();
111                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
112                                
113                                if(currency != null) {
114                                    var isDefault = Boolean.valueOf(form.getIsDefault());
115                                    var sortOrder = Integer.valueOf(form.getSortOrder());
116
117                                    ContentLogic.getInstance().createContentCategoryItem(this, contentCategory, item, inventoryCondition, unitOfMeasureType,
118                                            currency, isDefault, sortOrder, getPartyPK());
119                                } else {
120                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
121                                }
122                            } else {
123                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(),
124                                        unitOfMeasureTypeName);
125                            }
126                        } else {
127                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
128                        }
129                    } else {
130                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
131                    }
132                } else {
133                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCollectionName, contentCatalogName, contentCategoryName);
134                }
135            } else {
136                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollectionName, contentCatalogName);
137            }
138        } else {
139            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
140        }
141        
142        return null;
143    }
144    
145}