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.GetForumMessagesForm; 020import com.echothree.control.user.forum.common.result.ForumResultFactory; 021import com.echothree.model.control.core.common.ComponentVendors; 022import com.echothree.model.control.core.common.EntityTypes; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.forum.common.ForumConstants; 025import com.echothree.model.control.forum.server.control.ForumControl; 026import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.data.forum.server.entity.ForumMessage; 029import com.echothree.model.data.forum.server.entity.ForumThread; 030import com.echothree.model.data.forum.server.factory.ForumMessageFactory; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 036import java.util.Collection; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class GetForumMessagesCommand 043 extends BasePaginatedMultipleEntitiesCommand<ForumMessage, GetForumMessagesForm> { 044 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 FORM_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 051 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 052 ); 053 } 054 055 @Inject 056 ForumControl forumControl; 057 058 @Inject 059 ForumRoleTypeLogic forumLogic; 060 061 /** Creates a new instance of GetForumMessagesCommand */ 062 public GetForumMessagesCommand() { 063 super(null, FORM_FIELD_DEFINITIONS, true); 064 } 065 066 ForumThread forumThread; 067 068 @Override 069 protected void handleForm() { 070 var forumThreadName = form.getForumThreadName(); 071 var parameterCount = (forumThreadName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 072 073 if(parameterCount == 1) { 074 if(forumThreadName == null) { 075 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(), 076 EntityTypes.ForumThread.name()); 077 078 if(!hasExecutionErrors()) { 079 forumThread = forumControl.getForumThreadByEntityInstance(entityInstance); 080 } 081 } else { 082 forumThread = forumControl.getForumThreadByName(forumThreadName); 083 084 if(forumThread == null) { 085 addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName); 086 } 087 } 088 089 if(!hasExecutionErrors()) { 090 if(forumThread.getLastDetail().getPostedTime() > session.getStartTime() 091 && (getParty() == null || !getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) { 092 addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName()); 093 } else if(form.getUuid() == null && !forumLogic.isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) { 094 forumThread = null; // TODO: This should be an ExecutionError also. 095 } 096 } 097 } else { 098 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 099 } 100 } 101 102 @Override 103 protected Long getTotalEntities() { 104 return forumThread == null ? null : forumControl.countForumMessagesByForumThread(forumThread); 105 } 106 107 @Override 108 protected Collection<ForumMessage> getEntities() { 109 return forumThread == null ? null : forumControl.getForumMessagesByForumThread(forumThread); 110 } 111 112 @Override 113 protected BaseResult getResult(Collection<ForumMessage> entities) { 114 var result = ForumResultFactory.getGetForumMessagesResult(); 115 116 if(entities != null) { 117 result.setForumThread(forumControl.getForumThreadTransfer(getUserVisit(), forumThread)); 118 119 if(session.hasLimit(ForumMessageFactory.class)) { 120 result.setForumMessageCount(getTotalEntities()); 121 } 122 123 result.setForumMessages(forumControl.getForumMessageTransfers(getUserVisit(), entities)); 124 } 125 126 return result; 127 } 128 129}