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.GetForumsForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.forum.server.control.ForumControl;
022import com.echothree.model.data.forum.server.entity.Forum;
023import com.echothree.model.data.forum.server.factory.ForumFactory;
024import com.echothree.model.data.forum.server.factory.ForumGroupForumFactory;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.persistence.Session;
032import java.util.ArrayList;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036import javax.enterprise.context.RequestScoped;
037
038@RequestScoped
039public class GetForumsCommand
040        extends BaseSimpleCommand<GetForumsForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046            new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, false, null, null)
047        ));
048    }
049    
050    /** Creates a new instance of GetForumsCommand */
051    public GetForumsCommand() {
052        super(null, FORM_FIELD_DEFINITIONS, true);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var forumControl = Session.getModelController(ForumControl.class);
058        var result = ForumResultFactory.getGetForumsResult();
059        var forumGroupName = form.getForumGroupName();
060        var forumGroup = forumControl.getForumGroupByName(forumGroupName);
061        
062        if(forumGroupName == null || forumGroup != null) {
063            var userVisit = getUserVisit();
064            
065            if(forumGroup == null) {
066                if(session.hasLimit(ForumFactory.class)) {
067                    result.setForumCount(forumControl.countForums());
068                }
069
070                result.setForums(forumControl.getForumTransfers(userVisit));
071            } else {
072                var forumGroupForums = forumControl.getForumGroupForumsByForumGroup(forumGroup);
073                List<Forum> forums = new ArrayList<>(forumGroupForums.size());
074                
075                forumGroupForums.forEach((forumGroupForum) -> {
076                    forums.add(forumGroupForum.getForum());
077                });
078                
079                if(session.hasLimit(ForumGroupForumFactory.class)) {
080                    result.setForumCount(forumControl.countForumGroupForumsByForumGroup(forumGroup));
081                }
082
083                result.setForumGroup(forumControl.getForumGroupTransfer(userVisit, forumGroup));
084                result.setForums(forumControl.getForumTransfers(userVisit, forums));
085            }
086        } else {
087            addExecutionError(ExecutionErrors.UnknownForumGroupName.name(), forumGroupName);
088        }
089        
090        return result;
091    }
092    
093}