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