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 060 /** Creates a new instance of GetMessagesCommand */ 061 public GetMessagesCommand() { 062 super(null, FORM_FIELD_DEFINITIONS, true); 063 } 064 065 MessageType messageType; 066 067 @Override 068 protected void handleForm() { 069 var componentVendorName = form.getComponentVendorName(); 070 var entityTypeName = form.getEntityTypeName(); 071 var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName); 072 073 if(!hasExecutionErrors()) { 074 var messageTypeName = form.getMessageTypeName(); 075 076 messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 077 078 if(messageType == null) { 079 addExecutionError(com.echothree.util.common.message.ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 080 } 081 } 082 } 083 084 @Override 085 protected Long getTotalEntities() { 086 return hasExecutionErrors() ? null : messageControl.countMessagesByMessageType(messageType); 087 } 088 089 @Override 090 protected Collection<Message> getEntities() { 091 return hasExecutionErrors() ? null : messageControl.getMessagesByMessageType(messageType); 092 } 093 094 @Override 095 protected BaseResult getResult(Collection<Message> entities) { 096 var result = MessageResultFactory.getGetMessagesResult(); 097 098 if(entities != null) { 099 var userVisit = getUserVisit(); 100 101 result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType)); 102 103 if(session.hasLimit(MessageFactory.class)) { 104 result.setMessageCount(getTotalEntities()); 105 } 106 107 result.setMessages(messageControl.getMessageTransfers(userVisit, entities)); 108 } 109 110 return result; 111 } 112 113}