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.core.server.command;
018
019import com.echothree.control.user.core.common.form.GetCommandDescriptionForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetCommandDescriptionResult;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.core.server.entity.Command;
024import com.echothree.model.data.core.server.entity.CommandDescription;
025import com.echothree.model.data.core.server.entity.ComponentVendor;
026import com.echothree.model.data.party.server.entity.Language;
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 GetCommandDescriptionCommand
039        extends BaseSimpleCommand<GetCommandDescriptionForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("CommandName", FieldType.COMMAND_NAME, true, null, null)
047                ));
048    }
049    
050    /** Creates a new instance of GetCommandDescriptionCommand */
051    public GetCommandDescriptionCommand(UserVisitPK userVisitPK, GetCommandDescriptionForm form) {
052        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var coreControl = getCoreControl();
058        GetCommandDescriptionResult result = CoreResultFactory.getGetCommandDescriptionResult();
059        String componentVendorName = form.getComponentVendorName();
060        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
061
062        if(componentVendor != null) {
063            String commandName = form.getCommandName();
064            Command command = coreControl.getCommandByName(componentVendor, commandName);
065
066            if(command != null) {
067                var partyControl = Session.getModelController(PartyControl.class);
068                String languageIsoName = form.getLanguageIsoName();
069                Language language = partyControl.getLanguageByIsoName(languageIsoName);
070
071                if(language != null) {
072                    CommandDescription commandDescription = coreControl.getCommandDescription(command, language);
073
074                    if(commandDescription != null) {
075                        result.setCommandDescription(coreControl.getCommandDescriptionTransfer(getUserVisit(), commandDescription));
076                    } else {
077                        addExecutionError(ExecutionErrors.UnknownCommandDescription.name());
078                    }
079                } else {
080                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
081                }
082            } else {
083                addExecutionError(ExecutionErrors.UnknownCommandName.name(), commandName);
084            }
085        } else {
086            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
087        }
088
089        return result;
090    }
091    
092}