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