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.edit.ContentCategoryItemEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCategoryItemForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCategoryItemResult;
024import com.echothree.control.user.content.common.spec.ContentCategoryItemSpec;
025import com.echothree.model.control.accounting.server.control.AccountingControl;
026import com.echothree.model.control.content.server.control.ContentControl;
027import com.echothree.model.control.content.server.logic.ContentLogic;
028import com.echothree.model.control.inventory.server.control.InventoryControl;
029import com.echothree.model.control.item.server.control.ItemControl;
030import com.echothree.model.control.party.common.PartyTypes;
031import com.echothree.model.control.security.common.SecurityRoleGroups;
032import com.echothree.model.control.security.common.SecurityRoles;
033import com.echothree.model.control.uom.server.control.UomControl;
034import com.echothree.model.data.content.server.entity.ContentCategory;
035import com.echothree.model.data.content.server.entity.ContentCategoryItem;
036import com.echothree.model.data.user.common.pk.UserVisitPK;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
040import com.echothree.util.common.command.EditMode;
041import com.echothree.util.server.control.BaseAbstractEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import com.echothree.util.server.persistence.Session;
046import java.util.List;
047import javax.enterprise.context.Dependent;
048
049@Dependent
050public class EditContentCategoryItemCommand
051        extends BaseAbstractEditCommand<ContentCategoryItemSpec, ContentCategoryItemEdit, EditContentCategoryItemResult, ContentCategoryItem, ContentCategory> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
061                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Edit.name())
062                        ))
063                ));
064        
065        SPEC_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null)
073                );
074        
075        EDIT_FIELD_DEFINITIONS = List.of(
076                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
077                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
078                );
079    }
080    
081    /** Creates a new instance of EditContentCategoryItemCommand */
082    public EditContentCategoryItemCommand() {
083        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    public EditContentCategoryItemResult getResult() {
088        return ContentResultFactory.getEditContentCategoryItemResult();
089    }
090    
091    @Override
092    public ContentCategoryItemEdit getEdit() {
093        return ContentEditFactory.getContentCategoryItemEdit();
094    }
095    
096    @Override
097    public ContentCategoryItem getEntity(EditContentCategoryItemResult result) {
098        var contentControl = Session.getModelController(ContentControl.class);
099        ContentCategoryItem contentCategoryItem = null;
100        var contentCollectionName = spec.getContentCollectionName();
101        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
102        
103        if(contentCollection != null) {
104            var contentCatalogName = spec.getContentCatalogName();
105            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
106            
107            if(contentCatalog != null) {
108                var contentCategoryName = spec.getContentCategoryName();
109                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
110                
111                if(contentCategory != null) {
112                    var itemControl = Session.getModelController(ItemControl.class);
113                    var itemName = spec.getItemName();
114                    var item = itemControl.getItemByName(itemName);
115                    
116                    if(item != null) {
117                        var inventoryControl = Session.getModelController(InventoryControl.class);
118                        var inventoryConditionName = spec.getInventoryConditionName();
119                        var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
120
121                        if(inventoryCondition != null) {
122                            var uomControl = Session.getModelController(UomControl.class);
123                            var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
124                            var itemDetail = item.getLastDetail();
125                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
126                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
127
128                            if(unitOfMeasureType != null) {
129                                var accountingControl = Session.getModelController(AccountingControl.class);
130                                var currencyIsoName = spec.getCurrencyIsoName();
131                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
132
133                                if(currency != null) {
134                                    var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog,
135                                            item, inventoryCondition, unitOfMeasureType, currency);
136
137                                    if(contentCatalogItem != null) {
138                                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
139                                            contentCategoryItem = contentControl.getContentCategoryItem(contentCategory, contentCatalogItem);
140                                        } else { // EditMode.UPDATE
141                                            contentCategoryItem = contentControl.getContentCategoryItemForUpdate(contentCategory, contentCatalogItem);
142                                        }
143                                        
144                                        if(contentCategoryItem == null) {
145                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name());
146                                        }
147                                    } else {
148                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollectionName, contentCatalogName, itemName, inventoryConditionName,
149                                                unitOfMeasureTypeName, currencyIsoName);
150                                    }
151                                } else {
152                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
153                                }
154                            } else {
155                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
156                            }
157                        } else {
158                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
159                        }
160                    } else {
161                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
162                    }
163                } else {
164                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
165                }
166            } else {
167                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
168            }
169        } else {
170            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
171        }
172
173        return contentCategoryItem;
174    }
175    
176    @Override
177    public ContentCategory getLockEntity(ContentCategoryItem contentCategoryItem) {
178        return contentCategoryItem.getContentCategory();
179    }
180    
181    @Override
182    public void fillInResult(EditContentCategoryItemResult result, ContentCategoryItem contentCategoryItem) {
183        var contentControl = Session.getModelController(ContentControl.class);
184        
185        result.setContentCategoryItem(contentControl.getContentCategoryItemTransfer(getUserVisit(), contentCategoryItem));
186    }
187    
188    @Override
189    public void doLock(ContentCategoryItemEdit edit, ContentCategoryItem contentCategoryItem) {
190        edit.setIsDefault(contentCategoryItem.getIsDefault().toString());
191        edit.setSortOrder(contentCategoryItem.getSortOrder().toString());
192    }
193    
194    @Override
195    public void doUpdate(ContentCategoryItem contentCategoryItem) {
196        var contentControl = Session.getModelController(ContentControl.class);
197        var contentCategoryItemValue = contentControl.getContentCategoryItemValue(contentCategoryItem);
198
199        contentCategoryItemValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
200        contentCategoryItemValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
201
202        ContentLogic.getInstance().updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK());
203    }
204    
205}