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    ComponentVendorLogic componentVendorLogic;
052
053    @Inject
054    EntityTypeLogic entityTypeLogic;
055
056    @Inject
057    MessageControl messageControl;
058
059    /** Creates a new instance of GetMessageCommand */
060    public GetMessageCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063    
064    @Override
065    protected Message getEntity() {
066        Message message = null;
067        var componentVendorName = form.getComponentVendorName();
068        var entityTypeName = form.getEntityTypeName();
069        var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName);
070
071        if(!hasExecutionErrors()) {
072            var messageTypeName = form.getMessageTypeName();
073            var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
074
075            if(messageType != null) {
076                var messageName = form.getMessageName();
077
078                message = messageControl.getMessageByName(messageType, messageName);
079
080                if(message != null) {
081                    sendEvent(message.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
084                }
085            } else {
086                addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
087            }
088        }
089
090        return message;
091    }
092    
093    @Override
094    protected BaseResult getResult(Message message) {
095        var result = MessageResultFactory.getGetMessageResult();
096        
097        if(message != null) {
098            result.setMessage(messageControl.getMessageTransfer(getUserVisit(), message));
099        }
100        
101        return result;
102    }
103    
104}