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