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.index.server.command; 018 019import com.echothree.control.user.index.common.form.GetIndexForm; 020import com.echothree.control.user.index.common.result.GetIndexResult; 021import com.echothree.control.user.index.common.result.IndexResultFactory; 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.index.server.control.IndexControl; 027import com.echothree.model.data.core.server.entity.EntityInstance; 028import com.echothree.model.data.index.server.entity.Index; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 034import com.echothree.util.server.control.BaseSimpleCommand; 035import com.echothree.util.server.persistence.Session; 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.List; 039 040public class GetIndexCommand 041 extends BaseSimpleCommand<GetIndexForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("IndexName", 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 GetIndexCommand */ 056 public GetIndexCommand(UserVisitPK userVisitPK, GetIndexForm form) { 057 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 @Override 061 protected BaseResult execute() { 062 GetIndexResult result = IndexResultFactory.getGetIndexResult(); 063 String indexName = form.getIndexName(); 064 var parameterCount = (indexName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 065 066 if(parameterCount == 1) { 067 var indexControl = Session.getModelController(IndexControl.class); 068 Index index = null; 069 070 if(indexName == null) { 071 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(), 072 EntityTypes.Index.name()); 073 074 if(!hasExecutionErrors()) { 075 index = indexControl.getIndexByEntityInstance(entityInstance); 076 } 077 } else { 078 index = indexControl.getIndexByName(indexName); 079 080 if(index == null) { 081 addExecutionError(ExecutionErrors.UnknownIndexName.name(), indexName); 082 } 083 } 084 085 if(!hasExecutionErrors()) { 086 result.setIndex(indexControl.getIndexTransfer(getUserVisit(), index)); 087 sendEvent(index.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 088 } 089 } else { 090 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 091 } 092 093 return result; 094 } 095 096}