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 com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 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 /** Creates a new instance of GetItemCategoryCommand */ 067 public GetItemCategoryCommand() { 068 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 069 } 070 071 @Override 072 protected ItemCategory getEntity() { 073 var itemControl = Session.getModelController(ItemControl.class); 074 ItemCategory itemCategory = null; 075 var itemCategoryName = form.getItemCategoryName(); 076 var parameterCount = (itemCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 077 078 if(parameterCount == 1) { 079 if(itemCategoryName == null) { 080 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, 081 ComponentVendors.ECHO_THREE.name(), EntityTypes.ItemCategory.name()); 082 083 if(!hasExecutionErrors()) { 084 itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance); 085 } 086 } else { 087 itemCategory = ItemCategoryLogic.getInstance().getItemCategoryByName(this, itemCategoryName); 088 } 089 090 if(itemCategory != null) { 091 sendEvent(itemCategory.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 092 } 093 } else { 094 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 095 } 096 097 return itemCategory; 098 } 099 100 @Override 101 protected BaseResult getResult(ItemCategory itemCategory) { 102 var itemControl = Session.getModelController(ItemControl.class); 103 var result = ItemResultFactory.getGetItemCategoryResult(); 104 105 if(itemCategory != null) { 106 result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory)); 107 } 108 109 return result; 110 } 111 112}