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.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 com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class GetItemCategoryCommand
048        extends BaseSingleEntityCommand<ItemCategory, GetItemCategoryForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.ItemCategory.name(), SecurityRoles.Review.name())
058                        )))
059                )));
060        
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
064                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
065                ));
066    }
067    
068    /** Creates a new instance of GetItemCategoryCommand */
069    public GetItemCategoryCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
071    }
072    
073    @Override
074    protected ItemCategory getEntity() {
075        var itemControl = Session.getModelController(ItemControl.class);
076        ItemCategory itemCategory = null;
077        var itemCategoryName = form.getItemCategoryName();
078        var parameterCount = (itemCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
079
080        if(parameterCount == 1) {
081            if(itemCategoryName == null) {
082                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form,
083                        ComponentVendors.ECHO_THREE.name(), EntityTypes.ItemCategory.name());
084
085                if(!hasExecutionErrors()) {
086                    itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance);
087                }
088            } else {
089                itemCategory = ItemCategoryLogic.getInstance().getItemCategoryByName(this, itemCategoryName);
090            }
091
092            if(itemCategory != null) {
093                sendEvent(itemCategory.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
094            }
095        } else {
096            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
097        }
098        
099        return itemCategory;
100    }
101    
102    @Override
103    protected BaseResult getResult(ItemCategory itemCategory) {
104        var itemControl = Session.getModelController(ItemControl.class);
105        var result = ItemResultFactory.getGetItemCategoryResult();
106
107        if(itemCategory != null) {
108            result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory));
109        }
110
111        return result;
112    }
113    
114}