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.CreateMessageForm; 020import com.echothree.model.control.core.common.EntityAttributeTypes; 021import com.echothree.model.control.message.server.control.MessageControl; 022import com.echothree.model.data.core.server.entity.MimeType; 023import com.echothree.model.data.message.server.entity.Message; 024import com.echothree.model.data.message.server.entity.MessageType; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.persistence.BasePK; 029import com.echothree.util.common.persistence.type.ByteArray; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 036 037@Dependent 038public class CreateMessageCommand 039 extends BaseSimpleCommand<CreateMessageForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null), 047 new FieldDefinition("MessageTypeName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("MessageName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 050 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 051 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, false, null, null), 052 new FieldDefinition("ClobMessage", FieldType.STRING, false, 1L, null), 053 new FieldDefinition("StringMessage", FieldType.STRING, false, 1L, 512L), 054 // BlobMessage is not validated 055 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 056 ); 057 } 058 059 @Inject 060 MessageControl messageControl; 061 062 063 /** Creates a new instance of CreateMessageCommand */ 064 public CreateMessageCommand() { 065 super(null, FORM_FIELD_DEFINITIONS, false); 066 } 067 068 protected Message createMessage(MessageControl messageControl, MessageType messageType, String messageName, 069 Boolean includeByDefault, Boolean isDefault, Integer sortOrder, MimeType mimeType, ByteArray blobMessage, 070 String clobMessage, String stringMessage, BasePK createdBy) { 071 var message = messageControl.createMessage(messageType, messageName, includeByDefault, isDefault, sortOrder, 072 createdBy); 073 074 if(blobMessage != null) { 075 messageControl.createMessageBlob(message, getPreferredLanguage(), mimeType, blobMessage, createdBy); 076 } else if(clobMessage != null) { 077 messageControl.createMessageClob(message, getPreferredLanguage(), mimeType, clobMessage, createdBy); 078 } else if(stringMessage != null) { 079 messageControl.createMessageString(message, getPreferredLanguage(), stringMessage, createdBy); 080 } 081 082 return message; 083 } 084 085 @Override 086 protected BaseResult execute() { 087 var componentVendorName = form.getComponentVendorName(); 088 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 089 090 if(componentVendor != null) { 091 var entityTypeName = form.getEntityTypeName(); 092 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 093 094 if(entityType != null) { 095 var messageTypeName = form.getMessageTypeName(); 096 var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 097 098 if(messageType != null) { 099 var messageName = form.getMessageName(); 100 var message = messageControl.getMessageByName(messageType, messageName); 101 102 if(message == null) { 103 var mimeTypeName = form.getMimeTypeName(); 104 var includeByDefault = Boolean.valueOf(form.getIncludeByDefault()); 105 var isDefault = Boolean.valueOf(form.getIsDefault()); 106 var sortOrder = Integer.valueOf(form.getSortOrder()); 107 var description = form.getDescription(); 108 BasePK createdBy = getPartyPK(); 109 110 if(mimeTypeName == null) { 111 var messageString = form.getStringMessage(); 112 113 if(messageString != null) { 114 message = createMessage(messageControl, messageType, messageName, includeByDefault, isDefault, 115 sortOrder, null, null, null, messageString, createdBy); 116 } else { 117 addExecutionError(ExecutionErrors.MissingStringMessage.name()); 118 } 119 } else { 120 var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName); 121 122 if(mimeType != null) { 123 var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType(); 124 var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName(); 125 126 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 127 var blobMessage = form.getBlobMessage(); 128 129 if(blobMessage != null) { 130 message = createMessage(messageControl, messageType, messageName, includeByDefault, 131 isDefault, sortOrder, mimeType, blobMessage, null, null, createdBy); 132 } else { 133 addExecutionError(ExecutionErrors.MissingBlobMessage.name()); 134 } 135 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 136 var clobMessage = form.getClobMessage(); 137 138 if(clobMessage != null) { 139 message = createMessage(messageControl, messageType, messageName, includeByDefault, 140 isDefault, sortOrder, mimeType, null, clobMessage, null, createdBy); 141 } else { 142 addExecutionError(ExecutionErrors.MissingClobMessage.name()); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 149 } 150 } 151 152 if(description != null) { 153 messageControl.createMessageDescription(message, getPreferredLanguage(), description, createdBy); 154 } 155 } else { 156 addExecutionError(ExecutionErrors.DuplicateMessageName.name(), messageName); 157 } 158 } else { 159 addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 160 } 161 } else { 162 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 166 } 167 168 return null; 169 } 170 171}