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.GetMessageForm;
020import com.echothree.control.user.message.common.result.MessageResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.core.server.logic.ComponentVendorLogic;
023import com.echothree.model.control.core.server.logic.EntityTypeLogic;
024import com.echothree.model.control.message.server.control.MessageControl;
025import com.echothree.model.data.message.server.entity.Message;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSingleEntityCommand;
031import java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
036public class GetMessageCommand
037        extends BaseSingleEntityCommand<Message, GetMessageForm> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
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.ENTITY_NAME, true, null, null)
047        );
048    }
049
050    @Inject
051    MessageControl messageControl;
052
053    @Inject
054    ComponentVendorLogic componentVendorLogic;
055
056    @Inject
057    EntityTypeLogic entityTypeLogic;
058
059
060    /** Creates a new instance of GetMessageCommand */
061    public GetMessageCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    @Override
066    protected Message getEntity() {
067        Message message = null;
068        var componentVendorName = form.getComponentVendorName();
069        var entityTypeName = form.getEntityTypeName();
070        var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName);
071
072        if(!hasExecutionErrors()) {
073            var messageTypeName = form.getMessageTypeName();
074            var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
075
076            if(messageType != null) {
077                var messageName = form.getMessageName();
078
079                message = messageControl.getMessageByName(messageType, messageName);
080
081                if(message != null) {
082                    sendEvent(message.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
085                }
086            } else {
087                addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
088            }
089        }
090
091        return message;
092    }
093    
094    @Override
095    protected BaseResult getResult(Message message) {
096        var result = MessageResultFactory.getGetMessageResult();
097        
098        if(message != null) {
099            result.setMessage(messageControl.getMessageTransfer(getUserVisit(), message));
100        }
101        
102        return result;
103    }
104    
105}