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.GetMessageForm;
020import com.echothree.control.user.message.common.result.GetMessageResult;
021import com.echothree.control.user.message.common.result.MessageResultFactory;
022import com.echothree.model.control.core.common.EventTypes;
023import com.echothree.model.control.message.server.control.MessageControl;
024import com.echothree.model.data.core.server.entity.ComponentVendor;
025import com.echothree.model.data.core.server.entity.EntityType;
026import com.echothree.model.data.message.server.entity.Message;
027import com.echothree.model.data.message.server.entity.MessageType;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.model.data.user.server.entity.UserVisit;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039
040public class GetMessageCommand
041        extends BaseSimpleCommand<GetMessageForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
047            new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
048            new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
049            new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null),
050            new FieldDefinition("MessageName", FieldType.ENTITY_NAME, true, null, null)
051        ));
052    }
053    
054    /** Creates a new instance of GetMessageCommand */
055    public GetMessageCommand(UserVisitPK userVisitPK, GetMessageForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        GetMessageResult result = MessageResultFactory.getGetMessageResult();
062        var coreControl = getCoreControl();
063        String componentVendorName = form.getComponentVendorName();
064        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
065        
066        if(componentVendor != null) {
067            UserVisit userVisit = getUserVisit();
068            String entityTypeName = form.getEntityTypeName();
069            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
070            
071            result.setComponentVendor(coreControl.getComponentVendorTransfer(userVisit, componentVendor));
072            
073            if(entityType != null) {
074                var messageControl = Session.getModelController(MessageControl.class);
075                String messageTypeName = form.getMessageTypeName();
076                MessageType messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
077                
078                result.setEntityType(coreControl.getEntityTypeTransfer(userVisit, entityType));
079                
080                if(messageType != null) {
081                    String messageName = form.getMessageName();
082                    Message message = messageControl.getMessageByName(messageType, messageName);
083                    
084                    result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType));
085                    
086                    if(message != null) {
087                        result.setMessage(messageControl.getMessageTransfer(userVisit, message));
088                        
089                        sendEvent(messageType.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
090                    } else {
091                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
095                }
096            } else {
097                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
098            }
099        } else {
100            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
101        }
102        
103        return result;
104    }
105    
106}