001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.CreateEntityMessageForm; 020import com.echothree.model.control.message.server.control.MessageControl; 021import com.echothree.model.data.core.server.entity.ComponentVendor; 022import com.echothree.model.data.core.server.entity.EntityInstance; 023import com.echothree.model.data.core.server.entity.EntityType; 024import com.echothree.model.data.message.server.entity.EntityMessage; 025import com.echothree.model.data.message.server.entity.Message; 026import com.echothree.model.data.message.server.entity.MessageType; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.Arrays; 035import java.util.Collections; 036import java.util.List; 037 038public class CreateEntityMessageCommand 039 extends BaseSimpleCommand<CreateEntityMessageForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 045 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null), 046 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null), 048 new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("MessageName", FieldType.TAG, true, null, null) 050 )); 051 } 052 053 /** Creates a new instance of CreateEntityMessageCommand */ 054 public CreateEntityMessageCommand(UserVisitPK userVisitPK, CreateEntityMessageForm form) { 055 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 056 } 057 058 @Override 059 protected BaseResult execute() { 060 var coreControl = getCoreControl(); 061 String entityRef = form.getEntityRef(); 062 EntityInstance entityInstance = coreControl.getEntityInstanceByEntityRef(entityRef); 063 064 if(entityInstance != null) { 065 String componentVendorName = form.getComponentVendorName(); 066 ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName); 067 068 if(componentVendor != null) { 069 String entityTypeName = form.getEntityTypeName(); 070 EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName); 071 072 if(entityType != null) { 073 var messageControl = Session.getModelController(MessageControl.class); 074 String messageTypeName = form.getMessageTypeName(); 075 MessageType messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 076 077 if(messageType != null) { 078 String messageName = form.getMessageName(); 079 Message message = messageControl.getMessageByName(messageType, messageName); 080 081 if(message != null) { 082 EntityMessage entityMessage = messageControl.getEntityMessage(entityInstance, message); 083 084 if(entityMessage == null) { 085 messageControl.createEntityMessage(entityInstance, message, getPartyPK()); 086 } else { 087 addExecutionError(ExecutionErrors.DuplicateEntityMessage.name()); 088 } 089 } else { 090 addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName); 091 } 092 } else { 093 addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 094 } 095 } else { 096 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName); 097 } 098 } else { 099 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 103 } 104 105 return null; 106 } 107 108}