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