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
061    
062    /** Creates a new instance of GetForumGroupForumsCommand */
063    public GetForumGroupForumsCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    private ForumGroup forumGroup;
068    private Forum forum;
069
070    @Override
071    protected void handleForm() {
072        var forumGroupName = form.getForumGroupName();
073        var forumName = form.getForumName();
074        var parameterCount = (forumGroupName != null ? 1 : 0) + (forumName != null ? 1 : 0);
075
076        if(parameterCount == 1) {
077            if(forumGroupName != null) {
078                forumGroup = forumGroupLogic.getForumGroupByName(this, forumGroupName);
079            } else {
080                forum = forumLogic.getForumByName(this, forumName);
081            }
082        } else {
083            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
084        }
085    }
086
087    @Override
088    protected Long getTotalEntities() {
089        Long totalEntities = null;
090
091        if(!hasExecutionErrors()) {
092            if(forumGroup != null) {
093                totalEntities = forumControl.countForumGroupForumsByForumGroup(forumGroup);
094            } else {
095                totalEntities = forumControl.countForumGroupForumsByForum(forum);
096            }
097        }
098
099        return totalEntities;
100    }
101
102    @Override
103    protected Collection<ForumGroupForum> getEntities() {
104        Collection<ForumGroupForum> forumGroupForums = null;
105
106        if(!hasExecutionErrors()) {
107            if(forumGroup != null) {
108                forumGroupForums = forumControl.getForumGroupForumsByForumGroup(forumGroup);
109            } else {
110                forumGroupForums = forumControl.getForumGroupForumsByForum(forum);
111            }
112        }
113
114        return forumGroupForums;
115    }
116
117    @Override
118    protected BaseResult getResult(Collection<ForumGroupForum> entities) {
119        var result = ForumResultFactory.getGetForumGroupForumsResult();
120
121        if(entities != null) {
122            var userVisit = getUserVisit();
123
124            if(forumGroup != null) {
125                result.setForumGroup(forumControl.getForumGroupTransfer(userVisit, forumGroup));
126            } else {
127                result.setForum(forumControl.getForumTransfer(userVisit, forum));
128            }
129
130            if(session.hasLimit(ForumGroupForumFactory.class)) {
131                result.setForumGroupForumCount(getTotalEntities());
132            }
133
134            result.setForumGroupForums(forumControl.getForumGroupForumTransfers(userVisit, entities));
135        }
136
137        return result;
138    }
139    
140}