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