001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GetItemCategoryResult;
021import com.echothree.control.user.item.common.result.ItemResultFactory;
022import com.echothree.model.control.core.common.ComponentVendors;
023import com.echothree.model.control.core.common.EntityTypes;
024import com.echothree.model.control.core.common.EventTypes;
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.core.server.entity.EntityInstance;
032import com.echothree.model.data.item.server.entity.ItemCategory;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.server.control.BaseSingleEntityCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.Arrays;
044import java.util.Collections;
045import java.util.List;
046
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("Key", FieldType.KEY, false, null, null),
065                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
066                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
067                ));
068    }
069    
070    /** Creates a new instance of GetItemCategoryCommand */
071    public GetItemCategoryCommand(UserVisitPK userVisitPK, GetItemCategoryForm form) {
072        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
073    }
074    
075    @Override
076    protected ItemCategory getEntity() {
077        var itemControl = Session.getModelController(ItemControl.class);
078        ItemCategory itemCategory = null;
079        String itemCategoryName = form.getItemCategoryName();
080        var parameterCount = (itemCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
081
082        if(parameterCount == 1) {
083            if(itemCategoryName == null) {
084                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form,
085                        ComponentVendors.ECHO_THREE.name(), EntityTypes.ItemCategory.name());
086
087                if(!hasExecutionErrors()) {
088                    itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance);
089                }
090            } else {
091                itemCategory = ItemCategoryLogic.getInstance().getItemCategoryByName(this, itemCategoryName);
092            }
093
094            if(itemCategory != null) {
095                sendEvent(itemCategory.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
096            }
097        } else {
098            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
099        }
100        
101        return itemCategory;
102    }
103    
104    @Override
105    protected BaseResult getResult(ItemCategory itemCategory) {
106        var itemControl = Session.getModelController(ItemControl.class);
107        GetItemCategoryResult result = ItemResultFactory.getGetItemCategoryResult();
108
109        if(itemCategory != null) {
110            result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory));
111        }
112
113        return result;
114    }
115    
116}