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.control.forum.server.logic.ForumGroupLogic; 023import com.echothree.model.data.forum.server.entity.Forum; 024import com.echothree.model.data.forum.server.entity.ForumGroup; 025import com.echothree.model.data.forum.server.factory.ForumFactory; 026import com.echothree.model.data.forum.server.factory.ForumGroupForumFactory; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 032import java.util.ArrayList; 033import java.util.Collection; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 037 038@Dependent 039public class GetForumsCommand 040 extends BasePaginatedMultipleEntitiesCommand<Forum, GetForumsForm> { 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 ); 048 } 049 050 @Inject 051 ForumControl forumControl; 052 053 @Inject 054 ForumGroupLogic forumGroupLogic; 055 056 057 /** Creates a new instance of GetForumsCommand */ 058 public GetForumsCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 private ForumGroup forumGroup; 063 064 @Override 065 protected void handleForm() { 066 var forumGroupName = form.getForumGroupName(); 067 068 if(forumGroupName != null) { 069 forumGroup = forumGroupLogic.getForumGroupByName(this, forumGroupName); 070 } 071 } 072 073 @Override 074 protected Long getTotalEntities() { 075 return forumGroup == null ? 076 forumControl.countForums() : 077 forumControl.countForumGroupForumsByForumGroup(forumGroup); 078 } 079 080 @Override 081 protected Collection<Forum> getEntities() { 082 Collection<Forum> forums = null; 083 084 if(!hasExecutionErrors()) { 085 if(forumGroup == null) { 086 forums = forumControl.getForums(); 087 } else { 088 var forumGroupForums = forumControl.getForumGroupForumsByForumGroup(forumGroup); 089 090 forums = new ArrayList<>(forumGroupForums.size()); 091 for(var forumGroupForum : forumGroupForums) { 092 forums.add(forumGroupForum.getForum()); 093 } 094 } 095 } 096 097 return forums; 098 } 099 100 @Override 101 protected BaseResult getResult(Collection<Forum> entities) { 102 var result = ForumResultFactory.getGetForumsResult(); 103 104 if(entities != null) { 105 var userVisit = getUserVisit(); 106 107 if(forumGroup == null) { 108 if(session.hasLimit(ForumFactory.class)) { 109 result.setForumCount(getTotalEntities()); 110 } 111 } else { 112 if(session.hasLimit(ForumGroupForumFactory.class)) { 113 result.setForumCount(getTotalEntities()); 114 } 115 116 result.setForumGroup(forumControl.getForumGroupTransfer(userVisit, forumGroup)); 117 } 118 119 result.setForums(forumControl.getForumTransfers(userVisit, new ArrayList<>(entities))); 120 } 121 122 return result; 123 } 124 125}