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.GetForumMessageAttachmentForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.control.user.forum.common.result.GetForumMessageAttachmentResult;
022import com.echothree.model.control.content.server.logic.ContentLogic;
023import com.echothree.model.control.forum.common.ForumConstants;
024import com.echothree.model.control.forum.server.control.ForumControl;
025import com.echothree.model.control.forum.server.logic.ForumLogic;
026import com.echothree.model.data.forum.server.entity.ForumMessage;
027import com.echothree.model.data.forum.server.entity.ForumMessageAttachment;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class GetForumMessageAttachmentCommand
040        extends BaseSimpleCommand<GetForumMessageAttachmentForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("ForumMessageAttachmentSequence", FieldType.UNSIGNED_INTEGER, true, null, null),
048                new FieldDefinition("Referrer", FieldType.URL, false, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetForumMessageAttachmentCommand */
053    public GetForumMessageAttachmentCommand(UserVisitPK userVisitPK, GetForumMessageAttachmentForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        GetForumMessageAttachmentResult result = ForumResultFactory.getGetForumMessageAttachmentResult();
060        ContentLogic.getInstance().checkReferrer(this, form.getReferrer());
061
062        if(!hasExecutionErrors()) {
063            var forumControl = Session.getModelController(ForumControl.class);
064            String forumMessageName = form.getForumMessageName();
065            ForumMessage forumMessage = forumControl.getForumMessageByNameForUpdate(forumMessageName);
066
067            if(forumMessage != null) {
068                if(ForumLogic.getInstance().isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) {
069                    Integer forumMessageAttachmentSequence = Integer.valueOf(form.getForumMessageAttachmentSequence());
070                    ForumMessageAttachment forumMessageAttachment = forumControl.getForumMessageAttachmentBySequence(forumMessage, forumMessageAttachmentSequence);
071
072                    if(forumMessageAttachment != null) {
073                        result.setForumMessageAttachment(forumControl.getForumMessageAttachmentTransfer(getUserVisit(), forumMessageAttachment));
074                    } else {
075                        addExecutionError(ExecutionErrors.UnknownForumMessageAttachment.name(), forumMessageName, forumMessageAttachmentSequence.toString());
076                    }
077                } else {
078                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
079                }
080            } else {
081                addExecutionError(ExecutionErrors.UnknownForumMessageName.name(), forumMessageName);
082            }
083        }
084
085        return result;
086    }
087    
088}