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.GetItemCategoriesForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.item.server.entity.ItemCategory;
026import com.echothree.model.data.item.server.factory.ItemCategoryFactory;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import com.echothree.util.server.control.SecurityRoleDefinition;
035import com.echothree.util.server.persistence.Session;
036import java.util.Collection;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class GetItemCategoriesCommand
042        extends BasePaginatedMultipleEntitiesCommand<ItemCategory, GetItemCategoriesForm> {
043    
044    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
049                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
050                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
051                        new SecurityRoleDefinition(SecurityRoleGroups.ItemCategory.name(), SecurityRoles.List.name())
052                ))
053        ));
054        
055        FORM_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("ParentItemCategoryName", FieldType.ENTITY_NAME, false, null, null)
057        );
058    }
059    
060    /** Creates a new instance of GetItemCategoriesCommand */
061    public GetItemCategoriesCommand() {
062        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    private ItemCategory parentItemCategory;
066    
067    @Override
068    protected void handleForm() {
069        var itemControl = Session.getModelController(ItemControl.class);
070        var parentItemCategoryName = form.getParentItemCategoryName();
071
072        parentItemCategory = parentItemCategoryName == null ? null : itemControl.getItemCategoryByName(parentItemCategoryName);
073
074        if(parentItemCategoryName != null && parentItemCategory == null) {
075            addExecutionError(ExecutionErrors.UnknownParentItemCategoryName.name(), parentItemCategoryName);
076        }
077    }
078
079    @Override
080    protected Long getTotalEntities() {
081        if(hasExecutionErrors())
082            return null;
083
084        var itemControl = Session.getModelController(ItemControl.class);
085        
086        return parentItemCategory == null
087                ? itemControl.countItemCategories()
088                : itemControl.countItemCategoriesByParentItemCategory(parentItemCategory);
089    }
090    
091    @Override
092    protected Collection<ItemCategory> getEntities() {
093        Collection<ItemCategory> itemCategories = null;
094        
095        if(!hasExecutionErrors()) {
096            var itemControl = Session.getModelController(ItemControl.class);
097            
098            itemCategories = parentItemCategory == null ? itemControl.getItemCategories()
099                    : itemControl.getItemCategoriesByParentItemCategory(parentItemCategory);
100        }
101        
102        return itemCategories;
103    }
104    
105    @Override
106    protected BaseResult getResult(Collection<ItemCategory> entities) {
107        var result = ItemResultFactory.getGetItemCategoriesResult();
108
109        if(entities != null) {
110            var itemControl = Session.getModelController(ItemControl.class);
111            var userVisit = getUserVisit();
112
113            if(session.hasLimit(ItemCategoryFactory.class)) {
114                result.setItemCategoryCount(getTotalEntities());
115            }
116            
117            result.setParentItemCategory(parentItemCategory == null ? null : itemControl.getItemCategoryTransfer(userVisit, parentItemCategory));
118            result.setItemCategories(itemControl.getItemCategoryTransfers(userVisit, entities));
119        }
120        
121        return result;
122    }
123    
124}