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.GetEntityMessageForm; 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 GetEntityMessageCommand 037 extends BaseSimpleCommand<GetEntityMessageForm> { 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, true, 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, true, null, null), 047 new FieldDefinition("MessageName", FieldType.TAG, true, null, null) 048 )); 049 } 050 051 /** Creates a new instance of GetEntityMessageCommand */ 052 public GetEntityMessageCommand() { 053 super(null, FORM_FIELD_DEFINITIONS, true); 054 } 055 056 @Override 057 protected BaseResult execute() { 058 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 059 var result = MessageResultFactory.getGetEntityMessageResult(); 060 var entityRef = form.getEntityRef(); 061 var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef); 062 063 if(entityInstance != null) { 064 var componentVendorName = form.getComponentVendorName(); 065 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 066 var userVisit = getUserVisit(); 067 068 result.setComponentVendor(componentControl.getComponentVendorTransfer(userVisit, componentVendor)); 069 070 if(componentVendor != null) { 071 var entityTypeName = form.getEntityTypeName(); 072 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 073 074 result.setEntityType(entityTypeControl.getEntityTypeTransfer(userVisit, entityType)); 075 076 if(entityType != null) { 077 var messageControl = Session.getModelController(MessageControl.class); 078 var messageTypeName = form.getMessageTypeName(); 079 var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 080 081 result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false)); 082 083 if(messageType != null) { 084 var messageName = form.getMessageName(); 085 var message = messageControl.getMessageByName(messageType, messageName); 086 087 result.setMessageType(messageControl.getMessageTypeTransfer(userVisit, messageType)); 088 089 if(message != null) { 090 var entityMessage = messageControl.getEntityMessage(entityInstance, message); 091 092 result.setMessage(messageControl.getMessageTransfer(userVisit, message)); 093 094 if(entityMessage != null) { 095 result.setEntityMessage(messageControl.getEntityMessageTransfer(userVisit, entityMessage)); 096 } else { 097 addExecutionError(ExecutionErrors.UnknownEntityMessage.name()); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName); 107 } 108 } else { 109 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 113 } 114 115 return result; 116 } 117 118}