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.comment.server.command; 018 019import com.echothree.control.user.comment.common.form.CreateCommentForm; 020import com.echothree.control.user.comment.common.result.CommentResultFactory; 021import com.echothree.model.control.comment.server.control.CommentControl; 022import com.echothree.model.control.core.common.EntityAttributeTypes; 023import com.echothree.model.control.core.server.control.EntityInstanceControl; 024import com.echothree.model.control.core.server.control.MimeTypeControl; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.control.sequence.common.SequenceTypes; 027import com.echothree.model.control.sequence.server.control.SequenceControl; 028import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic; 029import com.echothree.model.control.workflow.server.control.WorkflowControl; 030import com.echothree.model.data.comment.server.entity.CommentType; 031import com.echothree.model.data.core.server.entity.EntityInstance; 032import com.echothree.model.data.core.server.entity.MimeType; 033import com.echothree.model.data.party.server.entity.Language; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.persistence.BasePK; 038import com.echothree.util.common.persistence.type.ByteArray; 039import com.echothree.util.common.validation.FieldDefinition; 040import com.echothree.util.common.validation.FieldType; 041import com.echothree.util.server.control.BaseSimpleCommand; 042import com.echothree.util.server.persistence.Session; 043import java.util.Arrays; 044import java.util.Collections; 045import java.util.List; 046import javax.enterprise.context.RequestScoped; 047 048@RequestScoped 049public class CreateCommentCommand 050 extends BaseSimpleCommand<CreateCommentForm> { 051 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("CommentedByUsername", FieldType.STRING, false, 1L, 80L), 057 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null), 058 new FieldDefinition("CommentTypeName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 061 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null), 062 new FieldDefinition("WorkflowEntranceName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("ClobComment", FieldType.STRING, false, 1L, null), 064 new FieldDefinition("StringComment", FieldType.STRING, false, 1L, 512L) 065 // BlobComment is not validated 066 )); 067 } 068 069 /** Creates a new instance of CreateCommentCommand */ 070 public CreateCommentCommand() { 071 super(null, FORM_FIELD_DEFINITIONS, false); 072 } 073 074 protected String createComment(EntityInstanceControl entityInstanceControl, CommentControl commentControl, CommentType commentType, EntityInstance commentedEntityInstance, Language language, MimeType mimeType, 075 ByteArray blobComment, String clobComment, String stringComment) { 076 var sequenceControl = Session.getModelController(SequenceControl.class); 077 BasePK createdBy = getPartyPK(); 078 EntityInstance commentedByEntityInstance = null; 079 String commentName = null; 080 var commentedByUsername = form.getCommentedByUsername(); 081 var workflowEntranceName = form.getWorkflowEntranceName(); 082 var workflowEntrance = commentType.getLastDetail().getWorkflowEntrance(); 083 084 if(commentedByUsername != null) { 085 var userControl = getUserControl(); 086 var userLogin = userControl.getUserLoginByUsername(commentedByUsername); 087 088 if(userLogin != null) { 089 commentedByEntityInstance = entityInstanceControl.getEntityInstanceByBasePK(userLogin.getPartyPK()); 090 } else { 091 addExecutionError(ExecutionErrors.UnknownRatedByUsername.name(), commentedByUsername); 092 } 093 } else { 094 commentedByEntityInstance = entityInstanceControl.getEntityInstanceByBasePK(createdBy); 095 } 096 097 if(!hasExecutionErrors() && (workflowEntranceName != null && workflowEntrance != null)) { 098 var workflowControl = Session.getModelController(WorkflowControl.class); 099 100 workflowEntrance = workflowControl.getWorkflowEntranceByName(workflowEntrance.getLastDetail().getWorkflow(), 101 workflowEntranceName); 102 103 if(workflowEntrance == null) { 104 addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowEntranceName); 105 } 106 } 107 108 if(!hasExecutionErrors()) { 109 var description = form.getDescription(); 110 var commentSequence = commentType.getLastDetail().getCommentSequence(); 111 112 if(commentSequence == null) { 113 commentSequence = sequenceControl.getDefaultSequenceUsingNames(SequenceTypes.COMMENT.name()); 114 } 115 116 commentName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(commentSequence); 117 118 var comment = commentControl.createComment(commentName, commentType, commentedEntityInstance, 119 commentedByEntityInstance, language == null? getPreferredLanguage(): language, description, mimeType, createdBy); 120 121 if(blobComment != null) { 122 commentControl.createCommentBlob(comment, blobComment, createdBy); 123 } else if(clobComment != null) { 124 commentControl.createCommentClob(comment, clobComment, createdBy); 125 } else if(stringComment != null) { 126 commentControl.createCommentString(comment, stringComment, createdBy); 127 } 128 129 if(workflowEntrance != null) { 130 var workflowControl = Session.getModelController(WorkflowControl.class); 131 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(comment.getPrimaryKey()); 132 133 // TODO: WorkEffort should be created for addEntityToWorkflow 134 workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy); 135 } 136 } 137 138 return commentName; 139 } 140 141 @Override 142 protected BaseResult execute() { 143 var result = CommentResultFactory.getCreateCommentResult(); 144 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 145 String commentName = null; 146 var entityRef = form.getEntityRef(); 147 var commentedEntityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef); 148 149 if(commentedEntityInstance != null) { 150 var commentControl = Session.getModelController(CommentControl.class); 151 var commentTypeName = form.getCommentTypeName(); 152 var commentType = commentControl.getCommentTypeByName(commentedEntityInstance.getEntityType(), 153 commentTypeName); 154 155 if(commentType != null) { 156 var partyControl = Session.getModelController(PartyControl.class); 157 var languageIsoName = form.getLanguageIsoName(); 158 var language = languageIsoName == null? null: partyControl.getLanguageByIsoName(languageIsoName); 159 160 if(languageIsoName == null || language != null) { 161 var mimeTypeName = form.getMimeTypeName(); 162 163 if(mimeTypeName == null) { 164 var commentString = form.getStringComment(); 165 166 if(commentString != null) { 167 commentName = createComment(entityInstanceControl, commentControl, commentType, commentedEntityInstance, language, null, null, 168 null, commentString); 169 } else { 170 addExecutionError(ExecutionErrors.MissingStringComment.name()); 171 } 172 } else { 173 var mimeTypeControl = Session.getModelController(MimeTypeControl.class); 174 var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName); 175 176 if(mimeType != null) { 177 var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType(); 178 var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName(); 179 180 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 181 var blobComment = form.getBlobComment(); 182 183 if(blobComment != null) { 184 commentName = createComment(entityInstanceControl, commentControl, commentType, commentedEntityInstance, 185 language, mimeType, blobComment, null, null); 186 } else { 187 addExecutionError(ExecutionErrors.MissingBlobComment.name()); 188 } 189 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 190 var clobComment = form.getClobComment(); 191 192 if(clobComment != null) { 193 commentName = createComment(entityInstanceControl, commentControl, commentType, commentedEntityInstance, 194 language, mimeType, null, clobComment, null); 195 } else { 196 addExecutionError(ExecutionErrors.MissingClobComment.name()); 197 } 198 } else { 199 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 200 } 201 } else { 202 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 203 } 204 } 205 } else { 206 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 207 } 208 } else { 209 addExecutionError(ExecutionErrors.UnknownCommentTypeName.name(), commentTypeName); 210 } 211 } else { 212 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 213 } 214 215 result.setCommentName(commentName); 216 217 return result; 218 } 219 220}