001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.MessageResultFactory;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.message.server.control.MessageControl;
023import com.echothree.model.data.user.common.pk.UserVisitPK;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.server.control.BaseSimpleCommand;
029import com.echothree.util.server.persistence.Session;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032
033@Dependent
034public class GetEntityMessageCommand
035        extends BaseSimpleCommand<GetEntityMessageForm> {
036    
037    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
038    
039    static {
040        FORM_FIELD_DEFINITIONS = List.of(
041            new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null),
042            new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
043            new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
044            new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null),
045            new FieldDefinition("MessageName", FieldType.TAG, true, null, null)
046        );
047    }
048    
049    /** Creates a new instance of GetEntityMessageCommand */
050    public GetEntityMessageCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
057        var result = MessageResultFactory.getGetEntityMessageResult();
058        var entityRef = form.getEntityRef();
059        var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
060        
061        if(entityInstance != null) {
062            var componentVendorName = form.getComponentVendorName();
063            var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
064            var userVisit = getUserVisit();
065            
066            result.setComponentVendor(componentControl.getComponentVendorTransfer(userVisit, componentVendor));
067            
068            if(componentVendor != null) {
069                var entityTypeName = form.getEntityTypeName();
070                var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
071                
072                result.setEntityType(entityTypeControl.getEntityTypeTransfer(userVisit, entityType));
073                
074                if(entityType != null) {
075                    var messageControl = Session.getModelController(MessageControl.class);
076                    var messageTypeName = form.getMessageTypeName();
077                    var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
078                    
079                    result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false));
080                    
081                    if(messageType != null) {
082                        var messageName = form.getMessageName();
083                        var message = messageControl.getMessageByName(messageType, messageName);
084                        
085                        result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType));
086                        
087                        if(message != null) {
088                            var entityMessage = messageControl.getEntityMessage(entityInstance, message);
089                            
090                            result.setMessage(messageControl.getMessageTransfer(userVisit, message));
091                            
092                            if(entityMessage != null) {
093                                result.setEntityMessage(messageControl.getEntityMessageTransfer(userVisit, entityMessage));
094                            } else {
095                                addExecutionError(ExecutionErrors.UnknownEntityMessage.name());
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        } else {
110            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
111        }
112        
113        return result;
114    }
115    
116}