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.GetForumThreadsForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.core.common.ComponentVendors;
022import com.echothree.model.control.core.common.EntityTypes;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.forum.common.ForumConstants;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.control.forum.server.logic.ForumLogic;
027import com.echothree.model.data.forum.server.entity.Forum;
028import com.echothree.model.data.forum.server.factory.ForumThreadFactory;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039import javax.enterprise.context.RequestScoped;
040
041@RequestScoped
042public class GetForumThreadsCommand
043        extends BaseSimpleCommand<GetForumThreadsForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
049                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
051                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
052                new FieldDefinition("IncludeFutureForumThreads", FieldType.BOOLEAN, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of GetForumThreadsCommand */
057    public GetForumThreadsCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, true);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var result = ForumResultFactory.getGetForumThreadsResult();
064        var forumName = form.getForumName();
065        var parameterCount = (forumName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
066
067        if(parameterCount == 1) {
068            var forumControl = Session.getModelController(ForumControl.class);
069            Forum forum = null;
070
071            if(forumName == null) {
072                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
073                        EntityTypes.Forum.name());
074                
075                if(!hasExecutionErrors()) {
076                    forum = forumControl.getForumByEntityInstance(entityInstance);
077                }
078            } else {
079                forum = forumControl.getForumByName(forumName);
080
081                if(forum == null) {
082                    addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName);
083                }
084            }
085
086            // If the UUID for the Forum is specified, then bypass the ForumRoleType check.
087            if(!hasExecutionErrors()) {
088                if(form.getUuid() != null || ForumLogic.getInstance().isForumRoleTypePermitted(this, forum, getParty(), ForumConstants.ForumRoleType_READER)) {
089                    var includeFutureForumThreads = Boolean.parseBoolean(form.getIncludeFutureForumThreads());
090                    var userVisit = getUserVisit();
091
092                    if(session.hasLimit(ForumThreadFactory.class)) {
093                        result.setForumThreadCount(forumControl.countForumThreadsByForum(forum, includeFutureForumThreads));
094                    }
095
096                    result.setForum(forumControl.getForumTransfer(userVisit, forum));
097                    result.setForumThreads(forumControl.getForumThreadTransfersByForum(userVisit, forum, includeFutureForumThreads));
098                } else {
099                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
100                }
101            }
102        } else {
103            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
104        }
105
106        return result;
107    }
108    
109}