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.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.List;
034import javax.enterprise.context.Dependent;
035
036@Dependent
037public class GetForumsCommand
038        extends BaseSimpleCommand<GetForumsForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044            new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, false, null, null)
045        );
046    }
047    
048    /** Creates a new instance of GetForumsCommand */
049    public GetForumsCommand() {
050        super(null, FORM_FIELD_DEFINITIONS, true);
051    }
052    
053    @Override
054    protected BaseResult execute() {
055        var forumControl = Session.getModelController(ForumControl.class);
056        var result = ForumResultFactory.getGetForumsResult();
057        var forumGroupName = form.getForumGroupName();
058        var forumGroup = forumControl.getForumGroupByName(forumGroupName);
059        
060        if(forumGroupName == null || forumGroup != null) {
061            var userVisit = getUserVisit();
062            
063            if(forumGroup == null) {
064                if(session.hasLimit(ForumFactory.class)) {
065                    result.setForumCount(forumControl.countForums());
066                }
067
068                result.setForums(forumControl.getForumTransfers(userVisit));
069            } else {
070                var forumGroupForums = forumControl.getForumGroupForumsByForumGroup(forumGroup);
071                List<Forum> forums = new ArrayList<>(forumGroupForums.size());
072                
073                forumGroupForums.forEach((forumGroupForum) -> {
074                    forums.add(forumGroupForum.getForum());
075                });
076                
077                if(session.hasLimit(ForumGroupForumFactory.class)) {
078                    result.setForumCount(forumControl.countForumGroupForumsByForumGroup(forumGroup));
079                }
080
081                result.setForumGroup(forumControl.getForumGroupTransfer(userVisit, forumGroup));
082                result.setForums(forumControl.getForumTransfers(userVisit, forums));
083            }
084        } else {
085            addExecutionError(ExecutionErrors.UnknownForumGroupName.name(), forumGroupName);
086        }
087        
088        return result;
089    }
090    
091}