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.GetForumMessageAttachmentsForm;
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.ForumMessageLogic;
024import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
025import com.echothree.model.data.forum.server.entity.ForumMessage;
026import com.echothree.model.data.forum.server.entity.ForumMessageAttachment;
027import com.echothree.model.data.forum.server.factory.ForumMessageAttachmentFactory;
028import com.echothree.util.common.command.BaseResult;
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.server.control.BasePaginatedMultipleEntitiesCommand;
033import java.util.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class GetForumMessageAttachmentsCommand
040        extends BasePaginatedMultipleEntitiesCommand<ForumMessageAttachment, GetForumMessageAttachmentsForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, true, null, null)
047        );
048    }
049
050    @Inject
051    ForumControl forumControl;
052
053    @Inject
054    ForumMessageLogic forumMessageLogic;
055
056    @Inject
057    ForumRoleTypeLogic forumRoleTypeLogic;
058
059    /** Creates a new instance of GetForumMessageAttachmentsCommand */
060    public GetForumMessageAttachmentsCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063
064    ForumMessage forumMessage;
065
066    @Override
067    protected void handleForm() {
068        var forumMessageName = form.getForumMessageName();
069
070        forumMessage = forumMessageLogic.getForumMessageByName(this, forumMessageName);
071
072        if(!hasExecutionErrors()) {
073            if(!forumRoleTypeLogic.isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) {
074                addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
075                forumMessage = null;
076            }
077        }
078    }
079
080    @Override
081    protected Long getTotalEntities() {
082        return forumMessage == null ? null : forumControl.countForumMessageAttachmentsByForumMessage(forumMessage);
083    }
084
085    @Override
086    protected Collection<ForumMessageAttachment> getEntities() {
087        return forumMessage == null ? null : forumControl.getForumMessageAttachmentsByForumMessage(forumMessage);
088    }
089
090    @Override
091    protected BaseResult getResult(Collection<ForumMessageAttachment> entities) {
092        var result = ForumResultFactory.getGetForumMessageAttachmentsResult();
093
094        if(forumMessage != null) {
095            var userVisit = getUserVisit();
096
097            result.setForumMessage(forumControl.getForumMessageTransfer(userVisit, forumMessage));
098
099            if(session.hasLimit(ForumMessageAttachmentFactory.class)) {
100                result.setForumMessageAttachmentCount(getTotalEntities());
101            }
102
103            result.setForumMessageAttachments(forumControl.getForumMessageAttachmentTransfers(userVisit, entities));
104        }
105
106        return result;
107    }
108    
109}