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.CreateSelectorNodeDescriptionForm;
020import com.echothree.model.control.party.server.control.PartyControl;
021import com.echothree.model.control.selector.server.control.SelectorControl;
022import com.echothree.model.data.party.server.entity.Language;
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.SelectorNodeDescription;
027import com.echothree.model.data.selector.server.entity.SelectorType;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class CreateSelectorNodeDescriptionCommand
040        extends BaseSimpleCommand<CreateSelectorNodeDescriptionForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046            new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
047            new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
048            new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
049            new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null),
050            new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
051            new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
052        ));
053    }
054    
055    /** Creates a new instance of CreateSelectorNodeDescriptionCommand */
056    public CreateSelectorNodeDescriptionCommand(UserVisitPK userVisitPK, CreateSelectorNodeDescriptionForm form) {
057        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var selectorControl = Session.getModelController(SelectorControl.class);
063        String selectorKindName = form.getSelectorKindName();
064        SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
065        
066        if(selectorKind != null) {
067            String selectorTypeName = form.getSelectorTypeName();
068            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
069            
070            if(selectorType != null) {
071                String selectorName = form.getSelectorName();
072                Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
073                
074                if(selector != null) {
075                    String selectorNodeName = form.getSelectorNodeName();
076                    SelectorNode selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
077                    
078                    if(selectorNode != null) {
079                        var partyControl = Session.getModelController(PartyControl.class);
080                        String languageIsoName = form.getLanguageIsoName();
081                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
082                        
083                        if(language != null) {
084                            SelectorNodeDescription selectorNodeDescription = selectorControl.getSelectorNodeDescription(selectorNode, language);
085                            
086                            if(selectorNodeDescription == null) {
087                                var description = form.getDescription();
088                                
089                                selectorControl.createSelectorNodeDescription(selectorNode, language, description, getPartyPK());
090                            } else {
091                                addExecutionError(ExecutionErrors.DuplicateSelectorNodeDescription.name());
092                            }
093                        } else {
094                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
095                        }
096                    } else {
097                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorNodeName);
098                    }
099                } else {
100                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
101                }
102            } else {
103                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
104            }
105        } else {
106            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
107        }
108        
109        return null;
110    }
111    
112}