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    /** Creates a new instance of GetEntityMessageCommand */
060    public GetEntityMessageCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063    
064    @Override
065    protected EntityMessage getEntity() {
066        EntityMessage entityMessage = null;
067        var entityRef = form.getEntityRef();
068        var entityInstance = entityInstanceLogic.getEntityInstanceByEntityRef(this, entityRef);
069
070        if(!hasExecutionErrors()) {
071            var componentVendorName = form.getComponentVendorName();
072            var entityTypeName = form.getEntityTypeName();
073            var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName);
074
075            if(!hasExecutionErrors()) {
076                var messageTypeName = form.getMessageTypeName();
077                var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
078
079                if(messageType != null) {
080                    var messageName = form.getMessageName();
081                    var message = messageControl.getMessageByName(messageType, messageName);
082
083                    if(message != null) {
084                        entityMessage = messageControl.getEntityMessage(entityInstance, message);
085
086                        if(entityMessage == null) {
087                            addExecutionError(ExecutionErrors.UnknownEntityMessage.name());
088                        }
089                    } else {
090                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageTypeName, messageName);
091                    }
092                } else {
093                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
094                }
095            }
096        }
097
098        return entityMessage;
099    }
100    
101    @Override
102    protected BaseResult getResult(EntityMessage entityMessage) {
103        var result = MessageResultFactory.getGetEntityMessageResult();
104
105        if(entityMessage != null) {
106            result.setEntityMessage(messageControl.getEntityMessageTransfer(getUserVisit(), entityMessage));
107        }
108
109        return result;
110    }
111    
112}