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.SetDefaultContentCategoryItemForm;
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 SetDefaultContentCategoryItemCommand
044        extends BaseSimpleCommand<SetDefaultContentCategoryItemForm> {
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.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Edit.name())
053                ))
054        ));
055        
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null)
064        );
065    }
066
067    @Inject
068    AccountingControl accountingControl;
069
070    @Inject
071    ContentControl contentControl;
072
073    @Inject
074    InventoryConditionControl inventoryConditionControl;
075
076    @Inject
077    ItemControl itemControl;
078
079    @Inject
080    UomControl uomControl;
081
082    @Inject
083    ContentLogic contentLogic;
084
085    
086    /** Creates a new instance of SetDefaultContentCategoryItemCommand */
087    public SetDefaultContentCategoryItemCommand() {
088        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
089    }
090    
091    @Override
092    protected BaseResult execute() {
093        var contentCollectionName = form.getContentCollectionName();
094        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
095        
096        if(contentCollection != null) {
097            var contentCatalogName = form.getContentCatalogName();
098            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
099            
100            if(contentCatalog != null) {
101                var contentCategoryName = form.getContentCategoryName();
102                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
103                
104                if(contentCategory != null) {
105                    var itemName = form.getItemName();
106                    var item = itemControl.getItemByName(itemName);
107                    
108                    if(item != null) {
109                        var inventoryConditionName = form.getInventoryConditionName();
110                        var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
111                        
112                        if(inventoryCondition != null) {
113                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
114                            var itemDetail = item.getLastDetail();
115                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
116                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
117                            
118                            if(unitOfMeasureType != null) {
119                                var currencyIsoName = form.getCurrencyIsoName();
120                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
121                                
122                                if(currency != null) {
123                                    var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog,
124                                            item, inventoryCondition, unitOfMeasureType, currency);
125                                    
126                                    if(contentCatalogItem != null) {
127                                        var contentCategoryItemValue = contentControl.getContentCategoryItemValueForUpdate(contentCategory,
128                                                contentCatalogItem);
129                                        
130                                        if(contentCategoryItemValue != null) {
131                                            contentCategoryItemValue.setIsDefault(true);
132                                            
133                                            contentLogic.updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK());
134                                        } else {
135                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(),
136                                                    contentCollection.getLastDetail().getContentCollectionName(),
137                                                    contentCatalog.getLastDetail().getContentCatalogName(),
138                                                    contentCategory.getLastDetail().getContentCategoryName(),
139                                                    item.getLastDetail().getItemName(),
140                                                    inventoryCondition.getLastDetail().getInventoryConditionName(),
141                                                    unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
142                                                    currency.getCurrencyIsoName());
143                                        }
144                                    } else {
145                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(),
146                                                contentCollection.getLastDetail().getContentCollectionName(),
147                                                contentCatalog.getLastDetail().getContentCatalogName(),
148                                                item.getLastDetail().getItemName(),
149                                                inventoryCondition.getLastDetail().getInventoryConditionName(),
150                                                unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
151                                                currency.getCurrencyIsoName());
152                                    }
153                                } else {
154                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
155                                }
156                            } else {
157                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
158                            }
159                        } else {
160                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
161                        }
162                    } else {
163                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
164                    }
165                } else {
166                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
167                            contentCollection.getLastDetail().getContentCollectionName(),
168                            contentCatalog.getLastDetail().getContentCatalogName(), contentCategoryName);
169                }
170            } else {
171                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
172                        contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
173            }
174        } else {
175            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
176        }
177        
178        return null;
179    }
180    
181}