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