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.logic.EntityInstanceLogic;
022import com.echothree.model.control.core.server.logic.EntityTypeLogic;
023import com.echothree.model.control.message.server.control.MessageControl;
024import com.echothree.model.data.message.server.entity.EntityMessage;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSingleEntityCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class GetEntityMessageCommand
036        extends BaseSingleEntityCommand<EntityMessage, GetEntityMessageForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null),
043                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
045                new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("MessageName", FieldType.TAG, true, null, null)
047        );
048    }
049
050    @Inject
051    MessageControl messageControl;
052
053    @Inject
054    EntityInstanceLogic entityInstanceLogic;
055
056    @Inject
057    EntityTypeLogic entityTypeLogic;
058
059
060    /** Creates a new instance of GetEntityMessageCommand */
061    public GetEntityMessageCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    @Override
066    protected EntityMessage getEntity() {
067        EntityMessage entityMessage = null;
068        var entityRef = form.getEntityRef();
069        var entityInstance = entityInstanceLogic.getEntityInstanceByEntityRef(this, entityRef);
070
071        if(!hasExecutionErrors()) {
072            var componentVendorName = form.getComponentVendorName();
073            var entityTypeName = form.getEntityTypeName();
074            var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName);
075
076            if(!hasExecutionErrors()) {
077                var messageTypeName = form.getMessageTypeName();
078                var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
079
080                if(messageType != null) {
081                    var messageName = form.getMessageName();
082                    var message = messageControl.getMessageByName(messageType, messageName);
083
084                    if(message != null) {
085                        entityMessage = messageControl.getEntityMessage(entityInstance, message);
086
087                        if(entityMessage == null) {
088                            addExecutionError(ExecutionErrors.UnknownEntityMessage.name());
089                        }
090                    } else {
091                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageTypeName, messageName);
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
095                }
096            }
097        }
098
099        return entityMessage;
100    }
101    
102    @Override
103    protected BaseResult getResult(EntityMessage entityMessage) {
104        var result = MessageResultFactory.getGetEntityMessageResult();
105
106        if(entityMessage != null) {
107            result.setEntityMessage(messageControl.getEntityMessageTransfer(getUserVisit(), entityMessage));
108        }
109
110        return result;
111    }
112    
113}