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.DeleteSelectorNodeDescriptionForm;
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 DeleteSelectorNodeDescriptionCommand
040        extends BaseSimpleCommand<DeleteSelectorNodeDescriptionForm> {
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        ));
052    }
053    
054    /** Creates a new instance of DeleteSelectorNodeDescriptionCommand */
055    public DeleteSelectorNodeDescriptionCommand(UserVisitPK userVisitPK, DeleteSelectorNodeDescriptionForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        var selectorControl = Session.getModelController(SelectorControl.class);
062        String selectorKindName = form.getSelectorKindName();
063        SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
064        
065        if(selectorKind != null) {
066            String selectorTypeName = form.getSelectorTypeName();
067            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
068            
069            if(selectorType != null) {
070                String selectorName = form.getSelectorName();
071                Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
072                
073                if(selector != null) {
074                    String selectorNodeName = form.getSelectorNodeName();
075                    SelectorNode selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
076                    
077                    if(selectorNode != null) {
078                        var partyControl = Session.getModelController(PartyControl.class);
079                        String languageIsoName = form.getLanguageIsoName();
080                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
081                        
082                        if(language != null) {
083                            SelectorNodeDescription selectorNodeDescription = selectorControl.getSelectorNodeDescriptionForUpdate(selectorNode, language);
084                            
085                            if(selectorNodeDescription != null) {
086                                selectorControl.deleteSelectorNodeDescription(selectorNodeDescription, getPartyPK());
087                            } else {
088                                addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name());
089                            }
090                        } else {
091                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
092                        }
093                    } else {
094                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorNodeName);
095                    }
096                } else {
097                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
098                }
099            } else {
100                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
101            }
102        } else {
103            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
104        }
105        
106        return null;
107    }
108    
109}