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