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.edit.CommentEdit;
020import com.echothree.control.user.comment.common.edit.CommentEditFactory;
021import com.echothree.control.user.comment.common.form.EditCommentForm;
022import com.echothree.control.user.comment.common.result.CommentResultFactory;
023import com.echothree.control.user.comment.common.spec.CommentSpec;
024import com.echothree.model.control.comment.server.control.CommentControl;
025import com.echothree.model.control.core.common.EntityAttributeTypes;
026import com.echothree.model.control.core.server.control.MimeTypeControl;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.data.comment.server.entity.Comment;
029import com.echothree.model.data.core.server.entity.MimeType;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.persistence.BasePK;
036import com.echothree.util.common.persistence.type.ByteArray;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseEditCommand;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditCommentCommand
046        extends BaseEditCommand<CommentSpec, CommentEdit> {
047    
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        SPEC_FIELD_DEFINITIONS = List.of(
053            new FieldDefinition("CommentName", FieldType.ENTITY_NAME, true, null, null)
054        );
055        
056        EDIT_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
059                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
060                new FieldDefinition("ClobComment", FieldType.STRING, false, 1L, null),
061                new FieldDefinition("StringComment", FieldType.STRING, false, 1L, 512L)
062                // BlobComment is not validated
063        );
064    }
065    
066    /** Creates a new instance of EditCommentCommand */
067    public EditCommentCommand() {
068        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
069    }
070    
071    protected void updateComment(CommentControl commentControl, Comment comment, Language language, String description, MimeType mimeType, BasePK updatedBy, ByteArray blob, String clob,
072            String string) {
073        if(lockEntityForUpdate(comment)) {
074            try {
075                var commentDetailValue = commentControl.getCommentDetailValueForUpdate(comment);
076                
077                commentDetailValue.setLanguagePK(language.getPrimaryKey());
078                commentDetailValue.setDescription(description);
079                commentDetailValue.setMimeTypePK(mimeType == null? null: mimeType.getPrimaryKey());
080                commentControl.updateCommentFromValue(commentDetailValue, updatedBy);
081
082                var commentBlob = commentControl.getCommentBlobForUpdate(comment);
083                
084                if(commentBlob != null) {
085                    if(blob == null) {
086                        commentControl.deleteCommentBlob(commentBlob, updatedBy);
087                    } else {
088                        var commentBlobValue = commentControl.getCommentBlobValue(commentBlob);
089                        
090                        commentBlobValue.setBlob(blob);
091                        commentControl.updateCommentBlobFromValue(commentBlobValue, updatedBy);
092                    }
093                } else if(blob != null) {
094                    commentControl.createCommentBlob(comment, blob, updatedBy);
095                }
096
097                var commentClob = commentControl.getCommentClobForUpdate(comment);
098                
099                if(commentClob != null) {
100                    if(clob == null) {
101                        commentControl.deleteCommentClob(commentClob, updatedBy);
102                    } else {
103                        var commentClobValue = commentControl.getCommentClobValue(commentClob);
104                        
105                        commentClobValue.setClob(clob);
106                        commentControl.updateCommentClobFromValue(commentClobValue, updatedBy);
107                    }
108                } else if(clob != null) {
109                    commentControl.createCommentClob(comment, clob, updatedBy);
110                }
111
112                var commentString = commentControl.getCommentStringForUpdate(comment);
113                
114                if(commentString != null) {
115                    if(string == null) {
116                        commentControl.deleteCommentString(commentString, updatedBy);
117                    } else {
118                        var commentStringValue = commentControl.getCommentStringValue(commentString);
119                        
120                        commentStringValue.setString(string);
121                        commentControl.updateCommentStringFromValue(commentStringValue, updatedBy);
122                    }
123                } else if(string != null) {
124                    commentControl.createCommentString(comment, string, updatedBy);
125                }
126            } finally {
127                unlockEntity(comment);
128            }
129        } else {
130            addExecutionError(ExecutionErrors.EntityLockStale.name());
131        }
132    }
133    
134    @Override
135    protected BaseResult execute() {
136        var commentControl = Session.getModelController(CommentControl.class);
137        var result = CommentResultFactory.getEditCommentResult();
138        var commentName = spec.getCommentName();
139        var comment = commentControl.getCommentByName(commentName);
140
141        if(comment != null) {
142            if(editMode.equals(EditMode.LOCK)) {
143                if(lockEntity(comment)) {
144                    var commentDetail = comment.getLastDetail();
145                    var edit = CommentEditFactory.getCommentEdit();
146                    var mimeType = commentDetail.getMimeType();
147
148                    result.setEdit(edit);
149
150                    edit.setLanguageIsoName(commentDetail.getLanguage().getLanguageIsoName());
151                    edit.setDescription(commentDetail.getDescription());
152                    edit.setMimeTypeName(mimeType == null ? null : mimeType.getLastDetail().getMimeTypeName());
153
154                    if(mimeType == null) {
155                        var commentString = commentControl.getCommentString(comment);
156
157                        if(commentString != null) {
158                            edit.setStringComment(commentString.getString());
159                        }
160                    } else {
161                        var entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
162
163                        // EntityAttributeTypes.BLOB.name() does not return anything in edit
164                        if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
165                            var commentClob = commentControl.getCommentClob(comment);
166
167                            if(commentClob != null) {
168                                edit.setClobComment(commentClob.getClob());
169                            }
170                        }
171                    }
172                } else {
173                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
174                }
175
176                result.setComment(commentControl.getCommentTransfer(getUserVisit(), comment));
177                result.setEntityLock(getEntityLockTransfer(comment));
178            } else if(editMode.equals(EditMode.ABANDON)) {
179                unlockEntity(comment);
180            } else if(editMode.equals(EditMode.UPDATE)) {
181                var partyControl = Session.getModelController(PartyControl.class);
182                var languageIsoName = edit.getLanguageIsoName();
183                var language = partyControl.getLanguageByIsoName(languageIsoName);
184
185                if(language != null) {
186                    BasePK updatedBy = getPartyPK();
187                    var description = edit.getDescription();
188                    var mimeTypeName = edit.getMimeTypeName();
189                    var mimeTypeUsageType = comment.getLastDetail().getCommentType().getLastDetail().getMimeTypeUsageType();
190
191                    if(mimeTypeName == null) {
192                        if(mimeTypeUsageType == null) {
193                            var string = edit.getStringComment();
194
195                            if(string != null) {
196                                updateComment(commentControl, comment, language, description, null, updatedBy, null, null, string);
197                            } else {
198                                addExecutionError(ExecutionErrors.MissingCommentString.name());
199                            }
200                        } else {
201                            // No mimeTypeName was supplied, but yet we required a MimeTypeUsageType
202                            addExecutionError(ExecutionErrors.InvalidMimeType.name());
203                        }
204                    } else {
205                        var mimeTypeControl = Session.getModelController(MimeTypeControl.class);
206                        var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
207
208                        if(mimeType != null) {
209                            if(mimeTypeUsageType != null) {
210                                var mimeTypeUsage = mimeTypeControl.getMimeTypeUsage(mimeType, mimeTypeUsageType);
211
212                                if(mimeTypeUsage != null) {
213                                    var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType();
214                                    var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
215
216                                    if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
217                                        var blob = edit.getBlobComment();
218
219                                        if(blob != null) {
220                                            updateComment(commentControl, comment, language, description, mimeType, updatedBy, blob, null, null);
221                                        } else {
222                                            addExecutionError(ExecutionErrors.MissingCommentBlob.name());
223                                        }
224                                    } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
225                                        var clob = edit.getClobComment();
226
227                                        if(clob != null) {
228                                            updateComment(commentControl, comment, language, description, mimeType, updatedBy, null, clob, null);
229                                        } else {
230                                            addExecutionError(ExecutionErrors.MissingCommentClob.name());
231                                        }
232                                    } else {
233                                        addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(),
234                                                entityAttributeTypeName);
235                                    }
236                                } else {
237                                    addExecutionError(ExecutionErrors.UnknownMimeTypeUsage.name());
238                                }
239                            } else {
240                                // mimeTypeName was supplied, and there shouldn't be one
241                                addExecutionError(ExecutionErrors.InvalidMimeType.name());
242                            }
243                        } else {
244                            addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
245                        }
246                    }
247                } else {
248                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
249                }
250                
251                if(hasExecutionErrors()) {
252                    result.setComment(commentControl.getCommentTransfer(getUserVisit(), comment));
253                    result.setEntityLock(getEntityLockTransfer(comment));
254                }
255            }
256        } else {
257            addExecutionError(ExecutionErrors.UnknownCommentName.name(), commentName);
258        }
259
260        return result;
261    }
262    
263}