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.InventoryConditionControl;
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 java.util.List;
046import javax.enterprise.context.Dependent;
047import javax.inject.Inject;
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    @Inject
082    AccountingControl accountingControl;
083
084    @Inject
085    ContentControl contentControl;
086
087    @Inject
088    InventoryConditionControl inventoryConditionControl;
089
090    @Inject
091    ItemControl itemControl;
092
093    @Inject
094    UomControl uomControl;
095
096    @Inject
097    ContentLogic contentLogic;
098
099    
100    /** Creates a new instance of EditContentCategoryItemCommand */
101    public EditContentCategoryItemCommand() {
102        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
103    }
104    
105    @Override
106    public EditContentCategoryItemResult getResult() {
107        return ContentResultFactory.getEditContentCategoryItemResult();
108    }
109    
110    @Override
111    public ContentCategoryItemEdit getEdit() {
112        return ContentEditFactory.getContentCategoryItemEdit();
113    }
114    
115    @Override
116    public ContentCategoryItem getEntity(EditContentCategoryItemResult result) {
117        ContentCategoryItem contentCategoryItem = null;
118        var contentCollectionName = spec.getContentCollectionName();
119        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
120        
121        if(contentCollection != null) {
122            var contentCatalogName = spec.getContentCatalogName();
123            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
124            
125            if(contentCatalog != null) {
126                var contentCategoryName = spec.getContentCategoryName();
127                var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
128                
129                if(contentCategory != null) {
130                    var itemName = spec.getItemName();
131                    var item = itemControl.getItemByName(itemName);
132                    
133                    if(item != null) {
134                        var inventoryConditionName = spec.getInventoryConditionName();
135                        var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
136
137                        if(inventoryCondition != null) {
138                            var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
139                            var itemDetail = item.getLastDetail();
140                            var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
141                            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
142
143                            if(unitOfMeasureType != null) {
144                                var currencyIsoName = spec.getCurrencyIsoName();
145                                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
146
147                                if(currency != null) {
148                                    var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog,
149                                            item, inventoryCondition, unitOfMeasureType, currency);
150
151                                    if(contentCatalogItem != null) {
152                                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
153                                            contentCategoryItem = contentControl.getContentCategoryItem(contentCategory, contentCatalogItem);
154                                        } else { // EditMode.UPDATE
155                                            contentCategoryItem = contentControl.getContentCategoryItemForUpdate(contentCategory, contentCatalogItem);
156                                        }
157                                        
158                                        if(contentCategoryItem == null) {
159                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name());
160                                        }
161                                    } else {
162                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollectionName, contentCatalogName, itemName, inventoryConditionName,
163                                                unitOfMeasureTypeName, currencyIsoName);
164                                    }
165                                } else {
166                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
167                                }
168                            } else {
169                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
170                            }
171                        } else {
172                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
173                        }
174                    } else {
175                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
176                    }
177                } else {
178                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
179                }
180            } else {
181                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
182            }
183        } else {
184            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
185        }
186
187        return contentCategoryItem;
188    }
189    
190    @Override
191    public ContentCategory getLockEntity(ContentCategoryItem contentCategoryItem) {
192        return contentCategoryItem.getContentCategory();
193    }
194    
195    @Override
196    public void fillInResult(EditContentCategoryItemResult result, ContentCategoryItem contentCategoryItem) {
197        result.setContentCategoryItem(contentControl.getContentCategoryItemTransfer(getUserVisit(), contentCategoryItem));
198    }
199    
200    @Override
201    public void doLock(ContentCategoryItemEdit edit, ContentCategoryItem contentCategoryItem) {
202        edit.setIsDefault(contentCategoryItem.getIsDefault().toString());
203        edit.setSortOrder(contentCategoryItem.getSortOrder().toString());
204    }
205    
206    @Override
207    public void doUpdate(ContentCategoryItem contentCategoryItem) {
208        var contentCategoryItemValue = contentControl.getContentCategoryItemValue(contentCategoryItem);
209
210        contentCategoryItemValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
211        contentCategoryItemValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
212
213        contentLogic.updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK());
214    }
215    
216}