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.item.server.command;
018
019import com.echothree.control.user.item.common.edit.ItemCategoryEdit;
020import com.echothree.control.user.item.common.edit.ItemEditFactory;
021import com.echothree.control.user.item.common.form.EditItemCategoryForm;
022import com.echothree.control.user.item.common.result.EditItemCategoryResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.model.control.core.common.EntityTypes;
025import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.control.item.server.logic.ItemCategoryLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.item.server.entity.ItemCategory;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import com.echothree.control.user.item.common.spec.ItemCategoryUniversalSpec;
043import com.echothree.model.control.core.common.ComponentVendors;
044import javax.enterprise.context.Dependent;
045
046@Dependent
047public class EditItemCategoryCommand
048        extends BaseAbstractEditCommand<ItemCategoryUniversalSpec, ItemCategoryEdit, EditItemCategoryResult, ItemCategory, ItemCategory> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ItemCategory.name(), SecurityRoles.Edit.name())
059                        ))
060                ));
061        
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
065                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
066                );
067        
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("ParentItemCategoryName", FieldType.ENTITY_NAME, false, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                );
075    }
076    
077    /** Creates a new instance of EditItemCategoryCommand */
078    public EditItemCategoryCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditItemCategoryResult getResult() {
084        return ItemResultFactory.getEditItemCategoryResult();
085    }
086    
087    @Override
088    public ItemCategoryEdit getEdit() {
089        return ItemEditFactory.getItemCategoryEdit();
090    }
091    
092    @Override
093    public ItemCategory getEntity(EditItemCategoryResult result) {
094        var itemControl = Session.getModelController(ItemControl.class);
095        ItemCategory itemCategory = null;
096        var itemCategoryName = spec.getItemCategoryName();
097        var parameterCount = (itemCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(spec);
098
099        if(parameterCount == 1) {
100            if(itemCategoryName == null) {
101                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec, ComponentVendors.ECHO_THREE.name(),
102                        EntityTypes.ItemCategory.name());
103
104                if(!hasExecutionErrors()) {
105                    itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance, editModeToEntityPermission(editMode));
106                }
107            } else {
108                itemCategory = ItemCategoryLogic.getInstance().getItemCategoryByName(this, itemCategoryName, editModeToEntityPermission(editMode));
109            }
110
111            if(itemCategory != null) {
112                result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory));
113            }
114        } else {
115            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
116        }
117
118        return itemCategory;
119    }
120    
121    @Override
122    public ItemCategory getLockEntity(ItemCategory itemCategory) {
123        return itemCategory;
124    }
125    
126    @Override
127    public void fillInResult(EditItemCategoryResult result, ItemCategory itemCategory) {
128        var itemControl = Session.getModelController(ItemControl.class);
129        
130        result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory));
131    }
132    
133    ItemCategory parentItemCategory = null;
134    
135    @Override
136    public void doLock(ItemCategoryEdit edit, ItemCategory itemCategory) {
137        var itemControl = Session.getModelController(ItemControl.class);
138        var itemCategoryDescription = itemControl.getItemCategoryDescription(itemCategory, getPreferredLanguage());
139        var itemCategoryDetail = itemCategory.getLastDetail();
140        
141        parentItemCategory = itemCategoryDetail.getParentItemCategory();
142        
143        edit.setItemCategoryName(itemCategoryDetail.getItemCategoryName());
144        edit.setParentItemCategoryName(parentItemCategory == null? null: parentItemCategory.getLastDetail().getItemCategoryName());
145        edit.setIsDefault(itemCategoryDetail.getIsDefault().toString());
146        edit.setSortOrder(itemCategoryDetail.getSortOrder().toString());
147
148        if(itemCategoryDescription != null) {
149            edit.setDescription(itemCategoryDescription.getDescription());
150        }
151    }
152        
153    @Override
154    public void canUpdate(ItemCategory itemCategory) {
155        var itemControl = Session.getModelController(ItemControl.class);
156        var itemCategoryName = edit.getItemCategoryName();
157        var duplicateItemCategory = itemControl.getItemCategoryByName(itemCategoryName);
158
159        if(duplicateItemCategory == null || itemCategory.equals(duplicateItemCategory)) {
160            var parentItemCategoryName = edit.getParentItemCategoryName();
161            
162            parentItemCategory = parentItemCategoryName == null? null: itemControl.getItemCategoryByName(parentItemCategoryName);
163
164            if(parentItemCategoryName == null || parentItemCategory != null) {
165                if(parentItemCategory != null) {
166                    if(!itemControl.isParentItemCategorySafe(itemCategory, parentItemCategory)) {
167                        addExecutionError(ExecutionErrors.InvalidParentItemCategory.name());
168                    }
169                }
170            } else {
171                addExecutionError(ExecutionErrors.UnknownParentItemCategoryName.name(), parentItemCategoryName);
172            }
173        } else {
174            addExecutionError(ExecutionErrors.DuplicateItemCategoryName.name(), itemCategoryName);
175        }
176    }
177    
178    @Override
179    public void doUpdate(ItemCategory itemCategory) {
180        var itemControl = Session.getModelController(ItemControl.class);
181        var partyPK = getPartyPK();
182        var itemCategoryDetailValue = itemControl.getItemCategoryDetailValueForUpdate(itemCategory);
183        var itemCategoryDescription = itemControl.getItemCategoryDescriptionForUpdate(itemCategory, getPreferredLanguage());
184        var description = edit.getDescription();
185
186        itemCategoryDetailValue.setItemCategoryName(edit.getItemCategoryName());
187        itemCategoryDetailValue.setParentItemCategoryPK(parentItemCategory == null? null: parentItemCategory.getPrimaryKey());
188        itemCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
189        itemCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
190
191        itemControl.updateItemCategoryFromValue(itemCategoryDetailValue, partyPK);
192
193        if(itemCategoryDescription == null && description != null) {
194            itemControl.createItemCategoryDescription(itemCategory, getPreferredLanguage(), description, partyPK);
195        } else if(itemCategoryDescription != null && description == null) {
196            itemControl.deleteItemCategoryDescription(itemCategoryDescription, partyPK);
197        } else if(itemCategoryDescription != null && description != null) {
198            var itemCategoryDescriptionValue = itemControl.getItemCategoryDescriptionValue(itemCategoryDescription);
199
200            itemCategoryDescriptionValue.setDescription(description);
201            itemControl.updateItemCategoryDescriptionFromValue(itemCategoryDescriptionValue, partyPK);
202        }
203    }
204    
205}