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.GetMessagesForm;
020import com.echothree.control.user.message.common.result.MessageResultFactory;
021import com.echothree.model.control.core.server.logic.ComponentVendorLogic;
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.Message;
025import com.echothree.model.data.message.server.entity.MessageType;
026import com.echothree.model.data.message.server.factory.MessageFactory;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
031import java.util.Collection;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetMessagesCommand
038        extends BasePaginatedMultipleEntitiesCommand<Message, GetMessagesForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
046                new FieldDefinition("MessageTypeName", 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    /** Creates a new instance of GetMessagesCommand */
060    public GetMessagesCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063
064    MessageType messageType;
065
066    @Override
067    protected void handleForm() {
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
075            messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
076
077            if(messageType == null) {
078                addExecutionError(com.echothree.util.common.message.ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
079            }
080        }
081    }
082
083    @Override
084    protected Long getTotalEntities() {
085        return hasExecutionErrors() ? null : messageControl.countMessagesByMessageType(messageType);
086    }
087
088    @Override
089    protected Collection<Message> getEntities() {
090        return hasExecutionErrors() ? null : messageControl.getMessagesByMessageType(messageType);
091    }
092
093    @Override
094    protected BaseResult getResult(Collection<Message> entities) {
095        var result = MessageResultFactory.getGetMessagesResult();
096
097        if(entities != null) {
098            var userVisit = getUserVisit();
099
100            result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType));
101
102            if(session.hasLimit(MessageFactory.class)) {
103                result.setMessageCount(getTotalEntities());
104            }
105
106            result.setMessages(messageControl.getMessageTransfers(userVisit, entities));
107        }
108
109        return result;
110    }
111    
112}