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.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.ForumRoleTypeLogic;
027import com.echothree.model.data.forum.server.entity.Forum;
028import com.echothree.model.data.forum.server.entity.ForumThread;
029import com.echothree.model.data.forum.server.factory.ForumThreadFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import java.util.Collection;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class GetForumThreadsCommand
042        extends BasePaginatedMultipleEntitiesCommand<ForumThread, GetForumThreadsForm> {
043
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045
046    static {
047        FORM_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
050                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
051                new FieldDefinition("IncludeFutureForumThreads", FieldType.BOOLEAN, true, null, null)
052        );
053    }
054
055    @Inject
056    ForumControl forumControl;
057
058    @Inject
059    EntityInstanceLogic entityInstanceLogic;
060
061    @Inject
062    ForumRoleTypeLogic forumLogic;
063
064    /** Creates a new instance of GetForumThreadsCommand */
065    public GetForumThreadsCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    Forum forum;
070    boolean includeFutureForumThreads;
071
072    @Override
073    protected void handleForm() {
074        var forumName = form.getForumName();
075        var parameterCount = (forumName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
076
077        if(parameterCount == 1) {
078            if(forumName == null) {
079                var entityInstance = entityInstanceLogic.getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
080                        EntityTypes.Forum.name());
081
082                if(!hasExecutionErrors()) {
083                    forum = forumControl.getForumByEntityInstance(entityInstance);
084                }
085            } else {
086                forum = forumControl.getForumByName(forumName);
087
088                if(forum == null) {
089                    addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName);
090                }
091            }
092
093            if(!hasExecutionErrors()) {
094                if(form.getUuid() == null && !forumLogic.isForumRoleTypePermitted(this, forum, getParty(), ForumConstants.ForumRoleType_READER)) {
095                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
096                }
097            }
098        } else {
099            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
100        }
101
102        includeFutureForumThreads = Boolean.parseBoolean(form.getIncludeFutureForumThreads());
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        return hasExecutionErrors() ? null : forumControl.countForumThreadsByForum(forum, includeFutureForumThreads);
108    }
109
110    @Override
111    protected Collection<ForumThread> getEntities() {
112        return hasExecutionErrors() ? null : forumControl.getForumThreadsByForum(forum, includeFutureForumThreads);
113    }
114
115    @Override
116    protected BaseResult getResult(Collection<ForumThread> entities) {
117        var result = ForumResultFactory.getGetForumThreadsResult();
118
119        if(entities != null) {
120            var userVisit = getUserVisit();
121
122            result.setForum(forumControl.getForumTransfer(userVisit, forum));
123
124            if(session.hasLimit(ForumThreadFactory.class)) {
125                result.setForumThreadCount(getTotalEntities());
126            }
127
128            result.setForumThreads(forumControl.getForumThreadTransfers(userVisit, entities));
129        }
130
131        return result;
132    }
133
134}