001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.GetEntityMessagesForm; 020import com.echothree.control.user.message.common.result.MessageResultFactory; 021import com.echothree.model.control.core.server.control.EntityInstanceControl; 022import com.echothree.model.control.message.server.control.MessageControl; 023import com.echothree.model.data.user.common.pk.UserVisitPK; 024import com.echothree.util.common.command.BaseResult; 025import com.echothree.util.common.message.ExecutionErrors; 026import com.echothree.util.common.validation.FieldDefinition; 027import com.echothree.util.common.validation.FieldType; 028import com.echothree.util.server.control.BaseSimpleCommand; 029import com.echothree.util.server.persistence.Session; 030import java.util.Arrays; 031import java.util.Collections; 032import java.util.List; 033import javax.enterprise.context.RequestScoped; 034 035@RequestScoped 036public class GetEntityMessagesCommand 037 extends BaseSimpleCommand<GetEntityMessagesForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 041 static { 042 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 043 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 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, false, null, null), 047 new FieldDefinition("MessageName", FieldType.TAG, false, null, null) 048 )); 049 } 050 051 /** Creates a new instance of GetEntityMessagesCommand */ 052 public GetEntityMessagesCommand() { 053 super(null, FORM_FIELD_DEFINITIONS, true); 054 } 055 056 @Override 057 protected BaseResult execute() { 058 var result = MessageResultFactory.getGetEntityMessagesResult(); 059 var componentVendorName = form.getComponentVendorName(); 060 var entityTypeName = form.getEntityTypeName(); 061 var messageTypeName = form.getMessageTypeName(); 062 var messageName = form.getMessageName(); 063 var entityRef = form.getEntityRef(); 064 var parameterCount = (componentVendorName == null && entityTypeName == null && messageTypeName == null && messageName == null ? 0 : 1) + (entityRef == null ? 0 : 1); 065 066 if(parameterCount == 1) { 067 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 068 var messageControl = Session.getModelController(MessageControl.class); 069 var userVisit = getUserVisit(); 070 071 if(entityRef != null) { 072 var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef); 073 074 if(entityInstance != null) { 075 result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false)); 076 result.setEntityMessages(messageControl.getEntityMessageTransfersByEntityInstance(userVisit, entityInstance)); 077 } else { 078 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 079 } 080 } else { 081 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 082 083 result.setComponentVendor(componentControl.getComponentVendorTransfer(userVisit, componentVendor)); 084 085 if(componentVendor != null) { 086 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 087 088 result.setEntityType(entityTypeControl.getEntityTypeTransfer(userVisit, entityType)); 089 090 if(entityType != null) { 091 var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 092 093 if(messageType != null) { 094 var message = messageControl.getMessageByName(messageType, messageName); 095 096 result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType)); 097 098 if(message != null) { 099 result.setMessage(messageControl.getMessageTransfer(userVisit, message)); 100 result.setEntityMessages(messageControl.getEntityMessageTransfersByMessage(userVisit, message)); 101 } else { 102 addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName); 103 } 104 } else { 105 addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 112 } 113 } 114 } else { 115 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 116 } 117 118 return result; 119 } 120 121}