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.selector.server.command;
018
019import com.echothree.control.user.selector.common.form.GetSelectorNodeForm;
020import com.echothree.control.user.selector.common.result.SelectorResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.selector.server.control.SelectorControl;
023import com.echothree.model.control.selector.server.logic.SelectorKindLogic;
024import com.echothree.model.control.selector.server.logic.SelectorLogic;
025import com.echothree.model.control.selector.server.logic.SelectorTypeLogic;
026import com.echothree.model.data.selector.server.entity.SelectorNode;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetSelectorNodeCommand
038        extends BaseSingleEntityCommand<SelectorNode, GetSelectorNodeForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null)
048        );
049    }
050
051    @Inject
052    SelectorControl selectorControl;
053
054    @Inject
055    SelectorKindLogic selectorKindLogic;
056
057    @Inject
058    SelectorLogic selectorLogic;
059
060    @Inject
061    SelectorTypeLogic selectorTypeLogic;
062
063    /** Creates a new instance of GetSelectorNodeCommand */
064    public GetSelectorNodeCommand() {
065        super(null, FORM_FIELD_DEFINITIONS, true);
066    }
067    
068    @Override
069    protected SelectorNode getEntity() {
070        var selectorKindName = form.getSelectorKindName();
071        var selectorKind = selectorKindLogic.getSelectorKindByName(this, selectorKindName);
072        SelectorNode selectorNode = null;
073        
074        if(!hasExecutionErrors()) {
075            var selectorTypeName = form.getSelectorTypeName();
076            var selectorType = selectorTypeLogic.getSelectorTypeByName(this, selectorKind, selectorTypeName);
077            
078            if(!hasExecutionErrors()) {
079                var selectorName = form.getSelectorName();
080                var selector = selectorLogic.getSelectorByName(this, selectorType, selectorName);
081                
082                if(!hasExecutionErrors()) {
083                    var selectorNodeName = form.getSelectorNodeName();
084                    selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
085
086                    if(selectorNode == null) {
087                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorKindName, selectorTypeName,
088                                selectorName, selectorNodeName);
089                    } else {
090                        sendEvent(selectorNode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
091                    }
092                }
093            }
094        }
095
096        return selectorNode;
097    }
098
099    @Override
100    protected BaseResult getResult(SelectorNode selectorNode) {
101        var result = SelectorResultFactory.getGetSelectorNodeResult();
102        var userVisit = getUserVisit();
103
104        if(selectorNode != null) {
105            result.setSelectorNode(selectorControl.getSelectorNodeTransfer(userVisit, selectorNode));
106        }
107
108        return result;
109    }
110    
111}