001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.ForumMessageLogic;
025import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
026import com.echothree.model.data.forum.server.entity.ForumMessageAttachment;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetForumMessageAttachmentCommand
038        extends BaseSingleEntityCommand<ForumMessageAttachment, GetForumMessageAttachmentForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ForumMessageAttachmentSequence", FieldType.UNSIGNED_INTEGER, true, null, null),
046                new FieldDefinition("Referrer", FieldType.URL, false, null, null)
047        );
048    }
049
050    @Inject
051    ForumControl forumControl;
052
053    @Inject
054    ContentLogic contentLogic;
055
056    @Inject
057    ForumMessageLogic forumMessageLogic;
058
059    @Inject
060    ForumRoleTypeLogic forumRoleTypeLogic;
061
062    /** Creates a new instance of GetForumMessageAttachmentCommand */
063    public GetForumMessageAttachmentCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    @Override
068    protected ForumMessageAttachment getEntity() {
069        ForumMessageAttachment forumMessageAttachment = null;
070        contentLogic.checkReferrer(this, form.getReferrer());
071
072        if(!hasExecutionErrors()) {
073            var forumMessageName = form.getForumMessageName();
074            var forumMessage = forumMessageLogic.getForumMessageByNameForUpdate(this, forumMessageName);
075
076            if(!hasExecutionErrors()) {
077                if(forumRoleTypeLogic.isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) {
078                    var forumMessageAttachmentSequence = Integer.valueOf(form.getForumMessageAttachmentSequence());
079                    forumMessageAttachment = forumControl.getForumMessageAttachmentBySequence(forumMessage, forumMessageAttachmentSequence);
080
081                    if(forumMessageAttachment == null) {
082                        addExecutionError(ExecutionErrors.UnknownForumMessageAttachment.name(), forumMessageName, forumMessageAttachmentSequence.toString());
083                    }
084                } else {
085                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
086                }
087            }
088        }
089
090        return hasExecutionErrors() ? null : forumMessageAttachment;
091    }
092
093    @Override
094    protected BaseResult getResult(ForumMessageAttachment forumMessageAttachment) {
095        var result = ForumResultFactory.getGetForumMessageAttachmentResult();
096
097        if(forumMessageAttachment != null) {
098            result.setForumMessageAttachment(forumControl.getForumMessageAttachmentTransfer(getUserVisit(), forumMessageAttachment));
099        }
100
101        return result;
102    }
103    
104}