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.form.GetItemCategoryForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.core.common.ComponentVendors;
022import com.echothree.model.control.core.common.EntityTypes;
023import com.echothree.model.control.core.common.EventTypes;
024import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
025import com.echothree.model.control.item.server.control.ItemControl;
026import com.echothree.model.control.item.server.logic.ItemCategoryLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.item.server.entity.ItemCategory;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.server.control.BaseSingleEntityCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetItemCategoryCommand
046        extends BaseSingleEntityCommand<ItemCategory, GetItemCategoryForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.ItemCategory.name(), SecurityRoles.Review.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
063        );
064    }
065
066    @Inject
067    ItemControl itemControl;
068
069    @Inject
070    EntityInstanceLogic entityInstanceLogic;
071
072    @Inject
073    ItemCategoryLogic itemCategoryLogic;
074
075    
076    /** Creates a new instance of GetItemCategoryCommand */
077    public GetItemCategoryCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
079    }
080    
081    @Override
082    protected ItemCategory getEntity() {
083        ItemCategory itemCategory = null;
084        var itemCategoryName = form.getItemCategoryName();
085        var parameterCount = (itemCategoryName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
086
087        if(parameterCount == 1) {
088            if(itemCategoryName == null) {
089                var entityInstance = entityInstanceLogic.getEntityInstance(this, form,
090                        ComponentVendors.ECHO_THREE.name(), EntityTypes.ItemCategory.name());
091
092                if(!hasExecutionErrors()) {
093                    itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance);
094                }
095            } else {
096                itemCategory = itemCategoryLogic.getItemCategoryByName(this, itemCategoryName);
097            }
098
099            if(itemCategory != null) {
100                sendEvent(itemCategory.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
101            }
102        } else {
103            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
104        }
105        
106        return itemCategory;
107    }
108    
109    @Override
110    protected BaseResult getResult(ItemCategory itemCategory) {
111        var result = ItemResultFactory.getGetItemCategoryResult();
112
113        if(itemCategory != null) {
114            result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory));
115        }
116
117        return result;
118    }
119    
120}