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.GetItemForm; 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.ItemLogic; 027import com.echothree.model.data.item.server.entity.Item; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BaseSingleEntityCommand; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 037 038@Dependent 039public class GetItemCommand 040 extends BaseSingleEntityCommand<Item, GetItemForm> { 041 042 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null), 048 new FieldDefinition("ItemNameOrAlias", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 050 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 051 ); 052 } 053 054 @Inject 055 ItemControl itemControl; 056 057 @Inject 058 EntityInstanceLogic entityInstanceLogic; 059 060 @Inject 061 ItemLogic itemLogic; 062 063 064 /** Creates a new instance of GetItemCommand */ 065 public GetItemCommand() { 066 super(null, FORM_FIELD_DEFINITIONS, true); 067 } 068 069 @Override 070 protected Item getEntity() { 071 Item item = null; 072 var itemName = form.getItemName(); 073 var itemNameOrAlias = form.getItemNameOrAlias(); 074 var parameterCount = (itemName == null ? 0 : 1) + (itemNameOrAlias == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form); 075 076 if(parameterCount == 1) { 077 if(itemName == null && itemNameOrAlias == null) { 078 var entityInstance = entityInstanceLogic.getEntityInstance(this, form, 079 ComponentVendors.ECHO_THREE.name(), EntityTypes.Item.name()); 080 081 if(!hasExecutionErrors()) { 082 item = itemControl.getItemByEntityInstance(entityInstance); 083 } 084 } else { 085 item = itemNameOrAlias == null ? itemLogic.getItemByName(this, itemName) : itemLogic.getItemByNameThenAlias(this, itemNameOrAlias); 086 } 087 088 if(item != null) { 089 sendEvent(item.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 090 } 091 } else { 092 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 093 } 094 095 return item; 096 } 097 098 @Override 099 protected BaseResult getResult(Item item) { 100 var result = ItemResultFactory.getGetItemResult(); 101 102 if(item != null) { 103 result.setItem(itemControl.getItemTransfer(getUserVisit(), item)); 104 } 105 106 return result; 107 } 108 109}