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.GetSelectorNodeDescriptionsForm;
020import com.echothree.control.user.selector.common.result.GetSelectorNodeDescriptionsResult;
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.SelectorNode;
026import com.echothree.model.data.selector.server.entity.SelectorType;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collections;
036import java.util.List;
037
038public class GetSelectorNodeDescriptionsCommand
039        extends BaseSimpleCommand<GetSelectorNodeDescriptionsForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045            new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
046            new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
047            new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
048            new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null)
049        ));
050    }
051    
052    /** Creates a new instance of GetSelectorNodeDescriptionsCommand */
053    public GetSelectorNodeDescriptionsCommand(UserVisitPK userVisitPK, GetSelectorNodeDescriptionsForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var selectorControl = Session.getModelController(SelectorControl.class);
060        GetSelectorNodeDescriptionsResult result = SelectorResultFactory.getGetSelectorNodeDescriptionsResult();
061        String selectorKindName = form.getSelectorKindName();
062        SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
063        
064        if(selectorKind != null) {
065            String selectorTypeName = form.getSelectorTypeName();
066            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
067            
068            result.setSelectorKind(selectorControl.getSelectorKindTransfer(getUserVisit(), selectorKind));
069            
070            if(selectorType != null) {
071                String selectorName = form.getSelectorName();
072                Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
073                
074                result.setSelectorType(selectorControl.getSelectorTypeTransfer(getUserVisit(), selectorType));
075                
076                if(selector != null) {
077                    String selectorNodeName = form.getSelectorNodeName();
078                    SelectorNode selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
079                    
080                    result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
081                    
082                    if(selectorNode != null) {
083                        result.setSelectorNode(selectorControl.getSelectorNodeTransfer(getUserVisit(), selectorNode));
084                        result.setSelectorNodeDescriptions(selectorControl.getSelectorNodeDescriptionTransfers(getUserVisit(), selectorNode));
085                    } else {
086                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorNodeName);
087                    }
088                } else {
089                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
090                }
091            } else {
092                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
093            }
094        } else {
095            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
096        }
097        
098        return result;
099    }
100    
101}