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.message.server.command;
018
019import com.echothree.control.user.message.common.form.CreateMessageDescriptionForm;
020import com.echothree.model.control.message.server.control.MessageControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.core.server.entity.ComponentVendor;
023import com.echothree.model.data.core.server.entity.EntityType;
024import com.echothree.model.data.message.server.entity.Message;
025import com.echothree.model.data.message.server.entity.MessageDescription;
026import com.echothree.model.data.message.server.entity.MessageType;
027import com.echothree.model.data.party.server.entity.Language;
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 CreateMessageDescriptionCommand
040        extends BaseSimpleCommand<CreateMessageDescriptionForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046            new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
047            new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
048            new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null),
049            new FieldDefinition("MessageName", 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 CreateMessageDescriptionCommand */
056    public CreateMessageDescriptionCommand(UserVisitPK userVisitPK, CreateMessageDescriptionForm form) {
057        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var coreControl = getCoreControl();
063        String componentVendorName = form.getComponentVendorName();
064        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
065        
066        if(componentVendor != null) {
067            String entityTypeName = form.getEntityTypeName();
068            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
069            
070            if(entityType != null) {
071                var messageControl = Session.getModelController(MessageControl.class);
072                String messageTypeName = form.getMessageTypeName();
073                MessageType messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
074                
075                if(messageType != null) {
076                    String messageName = form.getMessageName();
077                    Message message = messageControl.getMessageByName(messageType, messageName);
078                    
079                    if(message != null) {
080                        var partyControl = Session.getModelController(PartyControl.class);
081                        String languageIsoName = form.getLanguageIsoName();
082                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
083                        
084                        if(language != null) {
085                            MessageDescription messageDescription = messageControl.getMessageDescription(message, language);
086                            
087                            if(messageDescription == null) {
088                                var description = form.getDescription();
089                                
090                                messageControl.createMessageDescription(message, language, description, getPartyPK());
091                            } else {
092                                addExecutionError(ExecutionErrors.DuplicateMessageDescription.name());
093                            }
094                        } else {
095                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
096                        }
097                    } else {
098                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
099                    }
100                } else {
101                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
102                }
103            } else {
104                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
105            }
106        } else {
107            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
108        }
109        
110        return null;
111    }
112    
113}