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