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.GetEntityMessagesForm;
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 GetEntityMessagesCommand
035        extends BaseSimpleCommand<GetEntityMessagesForm> {
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, false, 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, false, null, null),
045            new FieldDefinition("MessageName", FieldType.TAG, false, null, null)
046        );
047    }
048    
049    /** Creates a new instance of GetEntityMessagesCommand */
050    public GetEntityMessagesCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var result = MessageResultFactory.getGetEntityMessagesResult();
057        var componentVendorName = form.getComponentVendorName();
058        var entityTypeName = form.getEntityTypeName();
059        var messageTypeName = form.getMessageTypeName();
060        var messageName = form.getMessageName();
061        var entityRef = form.getEntityRef();
062        var parameterCount = (componentVendorName == null && entityTypeName == null && messageTypeName == null && messageName == null ? 0 : 1) + (entityRef == null ? 0 : 1);
063        
064        if(parameterCount == 1) {
065            var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
066            var messageControl = Session.getModelController(MessageControl.class);
067            var userVisit = getUserVisit();
068            
069            if(entityRef != null) {
070                var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
071                
072                if(entityInstance != null) {
073                    result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false));
074                    result.setEntityMessages(messageControl.getEntityMessageTransfersByEntityInstance(userVisit, entityInstance));
075                } else {
076                    addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
077                }
078            } else {
079                var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
080                
081                result.setComponentVendor(componentControl.getComponentVendorTransfer(userVisit, componentVendor));
082                
083                if(componentVendor != null) {
084                    var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
085                    
086                    result.setEntityType(entityTypeControl.getEntityTypeTransfer(userVisit, entityType));
087                    
088                    if(entityType != null) {
089                        var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
090                        
091                        if(messageType != null) {
092                            var message = messageControl.getMessageByName(messageType, messageName);
093                            
094                            result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType));
095                            
096                            if(message != null) {
097                                result.setMessage(messageControl.getMessageTransfer(userVisit, message));
098                                result.setEntityMessages(messageControl.getEntityMessageTransfersByMessage(userVisit, message));
099                            } else {
100                                addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
101                            }
102                        } else {
103                            addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
104                        }
105                    } else {
106                        addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
107                    }
108                } else {
109                    addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
110                }
111            }
112        } else {
113            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
114        }
115        
116        return result;
117    }
118    
119}