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.selector.server.command;
018
019import com.echothree.control.user.selector.common.form.GetSelectorNodesForm;
020import com.echothree.control.user.selector.common.result.GetSelectorNodesResult;
021import com.echothree.control.user.selector.common.result.SelectorResultFactory;
022import com.echothree.model.control.selector.server.control.SelectorControl;
023import com.echothree.model.data.selector.server.entity.Selector;
024import com.echothree.model.data.selector.server.entity.SelectorKind;
025import com.echothree.model.data.selector.server.entity.SelectorType;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class GetSelectorNodesCommand
038        extends BaseSimpleCommand<GetSelectorNodesForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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        ));
048    }
049    
050    /** Creates a new instance of GetSelectorNodesCommand */
051    public GetSelectorNodesCommand(UserVisitPK userVisitPK, GetSelectorNodesForm form) {
052        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var selectorControl = Session.getModelController(SelectorControl.class);
058        GetSelectorNodesResult result = SelectorResultFactory.getGetSelectorNodesResult();
059        String selectorKindName = form.getSelectorKindName();
060        SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
061        
062        if(selectorKind != null) {
063            String selectorTypeName = form.getSelectorTypeName();
064            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
065            
066            result.setSelectorKind(selectorControl.getSelectorKindTransfer(getUserVisit(), selectorKind));
067            
068            if(selectorType != null) {
069                String selectorName = form.getSelectorName();
070                Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
071                
072                result.setSelectorType(selectorControl.getSelectorTypeTransfer(getUserVisit(), selectorType));
073                
074                if(selector != null) {
075                    result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
076                    result.setSelectorNodes(selectorControl.getSelectorNodeTransfersBySelector(getUserVisit(), selector));
077                } else {
078                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
079                }
080            } else {
081                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
082            }
083        } else {
084            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
085        }
086        
087        return result;
088    }
089    
090}