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