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.DeleteMessageDescriptionForm;
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 DeleteMessageDescriptionCommand
040        extends BaseSimpleCommand<DeleteMessageDescriptionForm> {
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        ));
052    }
053    
054    /** Creates a new instance of DeleteMessageDescriptionCommand */
055    public DeleteMessageDescriptionCommand(UserVisitPK userVisitPK, DeleteMessageDescriptionForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        var coreControl = getCoreControl();
062        String componentVendorName = form.getComponentVendorName();
063        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
064        
065        if(componentVendor != null) {
066            String entityTypeName = form.getEntityTypeName();
067            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
068            
069            if(entityType != null) {
070                var messageControl = Session.getModelController(MessageControl.class);
071                String messageTypeName = form.getMessageTypeName();
072                MessageType messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
073                
074                if(messageType != null) {
075                    String messageName = form.getMessageName();
076                    Message message = messageControl.getMessageByName(messageType, messageName);
077                    
078                    if(message != 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                            MessageDescription messageDescription = messageControl.getMessageDescriptionForUpdate(message, language);
085                            
086                            if(messageDescription != null) {
087                                messageControl.deleteMessageDescription(messageDescription, getPartyPK());
088                            } else {
089                                addExecutionError(ExecutionErrors.UnknownMessageDescription.name());
090                            }
091                        } else {
092                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
093                        }
094                    } else {
095                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
096                    }
097                } else {
098                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
099                }
100            } else {
101                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
102            }
103        } else {
104            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
105        }
106        
107        return null;
108    }
109    
110}