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.forum.server.command;
018
019import com.echothree.control.user.forum.common.form.DeleteForumMessageAttachmentDescriptionForm;
020import com.echothree.model.control.forum.server.control.ForumControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.forum.server.entity.ForumMessage;
023import com.echothree.model.data.forum.server.entity.ForumMessageAttachment;
024import com.echothree.model.data.forum.server.entity.ForumMessageAttachmentDescription;
025import com.echothree.model.data.party.server.entity.Language;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class DeleteForumMessageAttachmentDescriptionCommand
038        extends BaseSimpleCommand<DeleteForumMessageAttachmentDescriptionForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ForumMessageAttachmentSequence", FieldType.UNSIGNED_INTEGER, true, null, null),
046                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
047                ));
048    }
049    
050    /** Creates a new instance of DeleteForumMessageAttachmentDescriptionCommand */
051    public DeleteForumMessageAttachmentDescriptionCommand(UserVisitPK userVisitPK, DeleteForumMessageAttachmentDescriptionForm form) {
052        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var forumControl = Session.getModelController(ForumControl.class);
058        String forumMessageName = form.getForumMessageName();
059        ForumMessage forumMessage = forumControl.getForumMessageByNameForUpdate(forumMessageName);
060
061        if(forumMessage != null) {
062            Integer forumMessageAttachmentSequence = Integer.valueOf(form.getForumMessageAttachmentSequence());
063            ForumMessageAttachment forumMessageAttachment = forumControl.getForumMessageAttachmentBySequence(forumMessage, forumMessageAttachmentSequence);
064
065            if(forumMessageAttachment != null) {
066                var partyControl = Session.getModelController(PartyControl.class);
067                String languageIsoName = form.getLanguageIsoName();
068                Language language = partyControl.getLanguageByIsoName(languageIsoName);
069
070                if(language != null) {
071                    ForumMessageAttachmentDescription forumMessageAttachmentDescription = forumControl.getForumMessageAttachmentDescriptionForUpdate(forumMessageAttachment, language);
072
073                    if(forumMessageAttachmentDescription != null) {
074                        forumControl.deleteForumMessageAttachmentDescription(forumMessageAttachmentDescription, getPartyPK());
075                    } else {
076                        addExecutionError(ExecutionErrors.UnknownForumMessageAttachmentDescription.name(), forumMessageName,
077                                forumMessageAttachmentSequence.toString(), languageIsoName);
078                    }
079                } else {
080                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
081                }
082            } else {
083                addExecutionError(ExecutionErrors.UnknownForumMessageAttachment.name(), forumMessageName, forumMessageAttachmentSequence.toString());
084            }
085        } else {
086            addExecutionError(ExecutionErrors.UnknownForumMessageName.name(), forumMessageName);
087        }
088
089        return null;
090    }
091    
092}