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.GetForumForumThreadsForm; 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.entity.ForumForumThread; 024import com.echothree.model.data.forum.server.entity.ForumThread; 025import com.echothree.model.data.forum.server.factory.ForumForumThreadFactory; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 031import java.util.Collection; 032import java.util.List; 033import javax.enterprise.context.Dependent; 034import javax.inject.Inject; 035 036@Dependent 037public class GetForumForumThreadsCommand 038 extends BasePaginatedMultipleEntitiesCommand<ForumForumThread, GetForumForumThreadsForm> { 039 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = List.of( 044 new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null), 045 new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null) 046 ); 047 } 048 049 @Inject 050 protected ForumControl forumControl; 051 052 private Forum forum; 053 private ForumThread forumThread; 054 055 /** Creates a new instance of GetForumForumThreadsCommand */ 056 public GetForumForumThreadsCommand() { 057 super(null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 @Override 061 protected void handleForm() { 062 var forumName = form.getForumName(); 063 var forumThreadName = form.getForumThreadName(); 064 var parameterCount = (forumName != null? 1: 0) + (forumThreadName != null? 1: 0); 065 066 if(parameterCount == 1) { 067 if(forumName != null) { 068 forum = forumControl.getForumByName(forumName); 069 070 if(forum == null) { 071 addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName); 072 } 073 } else { 074 forumThread = forumControl.getForumThreadByName(forumThreadName); 075 076 if(forumThread == null) { 077 addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName); 078 } 079 } 080 } else { 081 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 082 } 083 } 084 085 @Override 086 protected Long getTotalEntities() { 087 Long total = null; 088 089 if(forum != null) { 090 total = forumControl.countForumForumThreadsByForum(forum); 091 } else if(forumThread != null) { 092 total = forumControl.countForumForumThreadsByForumThread(forumThread); 093 } 094 095 return total; 096 } 097 098 @Override 099 protected Collection<ForumForumThread> getEntities() { 100 Collection<ForumForumThread> forumForumThreads = null; 101 102 if(forum != null) { 103 forumForumThreads = forumControl.getForumForumThreadsByForum(forum); 104 } else if(forumThread != null) { 105 forumForumThreads = forumControl.getForumForumThreadsByForumThread(forumThread); 106 } 107 108 return forumForumThreads; 109 } 110 111 @Override 112 protected BaseResult getResult(Collection<ForumForumThread> entities) { 113 var result = ForumResultFactory.getGetForumForumThreadsResult(); 114 115 if(entities != null) { 116 var userVisit = getUserVisit(); 117 118 if(forum != null) { 119 result.setForum(forumControl.getForumTransfer(userVisit, forum)); 120 } else if(forumThread != null) { 121 result.setForumThread(forumControl.getForumThreadTransfer(userVisit, forumThread)); 122 } 123 124 if(session.hasLimit(ForumForumThreadFactory.class)) { 125 result.setForumForumThreadCount(getTotalEntities()); 126 } 127 128 result.setForumForumThreads(forumControl.getForumForumThreadTransfers(userVisit, entities)); 129 } 130 131 return result; 132 } 133 134}