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.index.server.command;
018
019import com.echothree.control.user.index.common.form.GetIndexForm;
020import com.echothree.control.user.index.common.result.IndexResultFactory;
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.index.server.control.IndexControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.index.server.entity.Index;
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.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class GetIndexCommand
044        extends BaseSingleEntityCommand<Index, GetIndexForm> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.Index.name(), SecurityRoles.Review.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("IndexName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
060                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
061        );
062    }
063
064    @Inject
065    IndexControl indexControl;
066
067    @Inject
068    EntityInstanceLogic entityInstanceLogic;
069
070    /** Creates a new instance of GetIndexCommand */
071    public GetIndexCommand() {
072        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
073    }
074    
075    @Override
076    protected Index getEntity() {
077        Index index = null;
078        var indexName = form.getIndexName();
079        var parameterCount = (indexName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
080
081        if(parameterCount == 1) {
082            if(indexName == null) {
083                var entityInstance = entityInstanceLogic.getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
084                        EntityTypes.Index.name());
085
086                if(!hasExecutionErrors()) {
087                    index = indexControl.getIndexByEntityInstance(entityInstance);
088                }
089            } else {
090                index = indexControl.getIndexByName(indexName);
091
092                if(index == null) {
093                    addExecutionError(ExecutionErrors.UnknownIndexName.name(), indexName);
094                }
095            }
096
097            if(!hasExecutionErrors()) {
098                sendEvent(index.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
099            }
100        } else {
101            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
102        }
103
104        return index;
105    }
106
107    @Override
108    protected BaseResult getResult(Index index) {
109        var result = IndexResultFactory.getGetIndexResult();
110
111        if(index != null) {
112            result.setIndex(indexControl.getIndexTransfer(getUserVisit(), index));
113        }
114
115        return result;
116    }
117    
118}