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