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.GetForumGroupForumsForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.forum.server.control.ForumControl;
022import com.echothree.model.control.forum.server.logic.ForumGroupLogic;
023import com.echothree.model.control.forum.server.logic.ForumLogic;
024import com.echothree.model.data.forum.server.entity.Forum;
025import com.echothree.model.data.forum.server.entity.ForumGroup;
026import com.echothree.model.data.forum.server.entity.ForumGroupForum;
027import com.echothree.model.data.forum.server.factory.ForumGroupForumFactory;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
033import java.util.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class GetForumGroupForumsCommand
040        extends BasePaginatedMultipleEntitiesCommand<ForumGroupForum, GetForumGroupForumsForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null)
048        );
049    }
050    
051    @Inject
052    ForumControl forumControl;
053
054    @Inject
055    ForumGroupLogic forumGroupLogic;
056
057    @Inject
058    ForumLogic forumLogic;
059    
060    /** Creates a new instance of GetForumGroupForumsCommand */
061    public GetForumGroupForumsCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    private ForumGroup forumGroup;
066    private Forum forum;
067
068    @Override
069    protected void handleForm() {
070        var forumGroupName = form.getForumGroupName();
071        var forumName = form.getForumName();
072        var parameterCount = (forumGroupName != null ? 1 : 0) + (forumName != null ? 1 : 0);
073
074        if(parameterCount == 1) {
075            if(forumGroupName != null) {
076                forumGroup = forumGroupLogic.getForumGroupByName(this, forumGroupName);
077            } else {
078                forum = forumLogic.getForumByName(this, forumName);
079            }
080        } else {
081            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
082        }
083    }
084
085    @Override
086    protected Long getTotalEntities() {
087        Long totalEntities = null;
088
089        if(!hasExecutionErrors()) {
090            if(forumGroup != null) {
091                totalEntities = forumControl.countForumGroupForumsByForumGroup(forumGroup);
092            } else {
093                totalEntities = forumControl.countForumGroupForumsByForum(forum);
094            }
095        }
096
097        return totalEntities;
098    }
099
100    @Override
101    protected Collection<ForumGroupForum> getEntities() {
102        Collection<ForumGroupForum> forumGroupForums = null;
103
104        if(!hasExecutionErrors()) {
105            if(forumGroup != null) {
106                forumGroupForums = forumControl.getForumGroupForumsByForumGroup(forumGroup);
107            } else {
108                forumGroupForums = forumControl.getForumGroupForumsByForum(forum);
109            }
110        }
111
112        return forumGroupForums;
113    }
114
115    @Override
116    protected BaseResult getResult(Collection<ForumGroupForum> entities) {
117        var result = ForumResultFactory.getGetForumGroupForumsResult();
118
119        if(entities != null) {
120            var userVisit = getUserVisit();
121
122            if(forumGroup != null) {
123                result.setForumGroup(forumControl.getForumGroupTransfer(userVisit, forumGroup));
124            } else {
125                result.setForum(forumControl.getForumTransfer(userVisit, forum));
126            }
127
128            if(session.hasLimit(ForumGroupForumFactory.class)) {
129                result.setForumGroupForumCount(getTotalEntities());
130            }
131
132            result.setForumGroupForums(forumControl.getForumGroupForumTransfers(userVisit, entities));
133        }
134
135        return result;
136    }
137    
138}