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 053 054 private Forum forum; 055 private ForumThread forumThread; 056 057 /** Creates a new instance of GetForumForumThreadsCommand */ 058 public GetForumForumThreadsCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 @Override 063 protected void handleForm() { 064 var forumName = form.getForumName(); 065 var forumThreadName = form.getForumThreadName(); 066 var parameterCount = (forumName != null? 1: 0) + (forumThreadName != null? 1: 0); 067 068 if(parameterCount == 1) { 069 if(forumName != null) { 070 forum = forumControl.getForumByName(forumName); 071 072 if(forum == null) { 073 addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName); 074 } 075 } else { 076 forumThread = forumControl.getForumThreadByName(forumThreadName); 077 078 if(forumThread == null) { 079 addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName); 080 } 081 } 082 } else { 083 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 084 } 085 } 086 087 @Override 088 protected Long getTotalEntities() { 089 Long total = null; 090 091 if(forum != null) { 092 total = forumControl.countForumForumThreadsByForum(forum); 093 } else if(forumThread != null) { 094 total = forumControl.countForumForumThreadsByForumThread(forumThread); 095 } 096 097 return total; 098 } 099 100 @Override 101 protected Collection<ForumForumThread> getEntities() { 102 Collection<ForumForumThread> forumForumThreads = null; 103 104 if(forum != null) { 105 forumForumThreads = forumControl.getForumForumThreadsByForum(forum); 106 } else if(forumThread != null) { 107 forumForumThreads = forumControl.getForumForumThreadsByForumThread(forumThread); 108 } 109 110 return forumForumThreads; 111 } 112 113 @Override 114 protected BaseResult getResult(Collection<ForumForumThread> entities) { 115 var result = ForumResultFactory.getGetForumForumThreadsResult(); 116 117 if(entities != null) { 118 var userVisit = getUserVisit(); 119 120 if(forum != null) { 121 result.setForum(forumControl.getForumTransfer(userVisit, forum)); 122 } else if(forumThread != null) { 123 result.setForumThread(forumControl.getForumThreadTransfer(userVisit, forumThread)); 124 } 125 126 if(session.hasLimit(ForumForumThreadFactory.class)) { 127 result.setForumForumThreadCount(getTotalEntities()); 128 } 129 130 result.setForumForumThreads(forumControl.getForumForumThreadTransfers(userVisit, entities)); 131 } 132 133 return result; 134 } 135 136}