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.GetForumMessageAttachmentForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.content.server.logic.ContentLogic;
022import com.echothree.model.control.forum.common.ForumConstants;
023import com.echothree.model.control.forum.server.control.ForumControl;
024import com.echothree.model.control.forum.server.logic.ForumLogic;
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 GetForumMessageAttachmentCommand
039        extends BaseSimpleCommand<GetForumMessageAttachmentForm> {
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("Referrer", FieldType.URL, false, null, null)
048                ));
049    }
050    
051    /** Creates a new instance of GetForumMessageAttachmentCommand */
052    public GetForumMessageAttachmentCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, true);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var result = ForumResultFactory.getGetForumMessageAttachmentResult();
059        ContentLogic.getInstance().checkReferrer(this, form.getReferrer());
060
061        if(!hasExecutionErrors()) {
062            var forumControl = Session.getModelController(ForumControl.class);
063            var forumMessageName = form.getForumMessageName();
064            var forumMessage = forumControl.getForumMessageByNameForUpdate(forumMessageName);
065
066            if(forumMessage != null) {
067                if(ForumLogic.getInstance().isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) {
068                    var forumMessageAttachmentSequence = Integer.valueOf(form.getForumMessageAttachmentSequence());
069                    var forumMessageAttachment = forumControl.getForumMessageAttachmentBySequence(forumMessage, forumMessageAttachmentSequence);
070
071                    if(forumMessageAttachment != null) {
072                        result.setForumMessageAttachment(forumControl.getForumMessageAttachmentTransfer(getUserVisit(), forumMessageAttachment));
073                    } else {
074                        addExecutionError(ExecutionErrors.UnknownForumMessageAttachment.name(), forumMessageName, forumMessageAttachmentSequence.toString());
075                    }
076                } else {
077                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
078                }
079            } else {
080                addExecutionError(ExecutionErrors.UnknownForumMessageName.name(), forumMessageName);
081            }
082        }
083
084        return result;
085    }
086    
087}