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