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.search.server.command; 018 019import com.echothree.control.user.search.common.form.SearchForumMessagesForm; 020import com.echothree.control.user.search.common.result.SearchForumMessagesResult; 021import com.echothree.control.user.search.common.result.SearchResultFactory; 022import com.echothree.model.control.forum.common.ForumConstants; 023import com.echothree.model.control.forum.server.control.ForumControl; 024import com.echothree.model.control.forum.server.logic.ForumLogic; 025import com.echothree.model.control.party.server.logic.LanguageLogic; 026import com.echothree.model.control.search.common.SearchKinds; 027import com.echothree.model.control.search.server.control.SearchControl; 028import com.echothree.model.control.forum.server.search.ForumMessageSearchEvaluator; 029import com.echothree.model.control.search.server.logic.SearchLogic; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.persistence.Session; 036import com.google.common.base.Splitter; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039 040@Dependent 041public class SearchForumMessagesCommand 042 extends BaseSearchCommand<SearchForumMessagesForm, SearchForumMessagesResult> { 043 044 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 045 046 static { 047 FORM_FIELD_DEFINITIONS = List.of( 048 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("SearchSortDirectionName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("ForumName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("ForumMessageType", FieldType.ENTITY_NAME, false, null, null), 055 new FieldDefinition("IncludeFutureForumThreads", FieldType.BOOLEAN, true, null, null), 056 new FieldDefinition("Q", FieldType.STRING, false, null, null), 057 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 058 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 059 new FieldDefinition("Fields", FieldType.STRING, false, null, null), 060 new FieldDefinition("RememberPreferences", FieldType.BOOLEAN, false, null, null), 061 new FieldDefinition("SearchUseTypeName", FieldType.ENTITY_NAME, false, null, null) 062 ); 063 } 064 065 /** Creates a new instance of SearchForumMessagesCommand */ 066 public SearchForumMessagesCommand() { 067 super(null, FORM_FIELD_DEFINITIONS, false); 068 } 069 070 @Override 071 protected BaseResult execute() { 072 var result = SearchResultFactory.getSearchForumMessagesResult(); 073 var searchLogic = SearchLogic.getInstance(); 074 var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.FORUM_MESSAGE.name()); 075 076 if(!hasExecutionErrors()) { 077 var searchTypeName = form.getSearchTypeName(); 078 var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName); 079 080 if(!hasExecutionErrors()) { 081 var languageIsoName = form.getLanguageIsoName(); 082 var language = languageIsoName == null ? null : LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 083 084 if(!hasExecutionErrors()) { 085 var searchControl = Session.getModelController(SearchControl.class); 086 var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType); 087 var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail(); 088 boolean rememberPreferences = Boolean.valueOf(form.getRememberPreferences()); 089 var searchDefaultOperatorName = form.getSearchDefaultOperatorName(); 090 var searchDefaultOperator = searchDefaultOperatorName == null 091 ? getDefaultSearchDefaultOperator(searchLogic, rememberPreferences, partySearchTypePreferenceDetail) 092 : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName); 093 094 if(!hasExecutionErrors()) { 095 var searchSortOrderName = form.getSearchSortOrderName(); 096 var searchSortOrder = searchSortOrderName == null 097 ? getDefaultSearchSortOrder(searchLogic, rememberPreferences, searchKind, partySearchTypePreferenceDetail) 098 : searchLogic.getSearchSortOrderByName(this, searchKind, searchSortOrderName); 099 100 if(!hasExecutionErrors()) { 101 var searchSortDirectionName = form.getSearchSortDirectionName(); 102 var searchSortDirection = searchSortDirectionName == null 103 ? getDefaultSearchSortDirection(searchLogic, rememberPreferences, partySearchTypePreferenceDetail) 104 : searchLogic.getSearchSortDirectionByName(this, searchSortDirectionName); 105 106 if(!hasExecutionErrors()) { 107 var searchUseTypeName = form.getSearchUseTypeName(); 108 var searchUseType = searchUseTypeName == null ? null : SearchLogic.getInstance().getSearchUseTypeByName(this, searchUseTypeName); 109 110 if(!hasExecutionErrors()) { 111 var forumControl = Session.getModelController(ForumControl.class); 112 var forumName = form.getForumName(); 113 var forum = forumControl.getForumByName(forumName); 114 115 if(forum != null) { 116 if(ForumLogic.getInstance().isForumRoleTypePermitted(this, forum, getParty(), ForumConstants.ForumRoleType_READER)) { 117 var forumMessageTypeName = form.getForumMessageTypeName(); 118 var forumMessageType = forumMessageTypeName == null ? null : forumControl.getForumMessageTypeByName(forumMessageTypeName); 119 120 if(forumMessageTypeName == null || forumMessageType != null) { 121 var forumType = forum.getLastDetail().getForumType(); 122 var forumTypeMessageType = forumMessageType == null ? forumControl.getDefaultForumTypeMessageType(forumType) 123 : forumControl.getForumTypeMessageType(forumType, forumMessageType); 124 125 if(forumTypeMessageType != null) { 126 var forumMessageSearchEvaluator = new ForumMessageSearchEvaluator(getUserVisit(), 127 language, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, searchUseType, 128 forum, forumTypeMessageType.getForumMessageType()); 129 var createdSince = form.getCreatedSince(); 130 var modifiedSince = form.getModifiedSince(); 131 var fields = form.getFields(); 132 133 forumMessageSearchEvaluator.setIncludeFutureForumThreads(Boolean.parseBoolean(form.getIncludeFutureForumThreads())); 134 forumMessageSearchEvaluator.setQ(this, form.getQ()); 135 forumMessageSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 136 forumMessageSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 137 forumMessageSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 138 139 if(!hasExecutionErrors()) { 140 result.setCount(forumMessageSearchEvaluator.execute(this)); 141 } 142 } else { 143 if(forumMessageTypeName == null) { 144 addExecutionError(ExecutionErrors.UnknownDefaultForumTypeMessageType.name(), forumType.getForumTypeName()); 145 } else { 146 addExecutionError(ExecutionErrors.UnknownForumTypeMessageType.name(), forumType.getForumTypeName(), forumMessageTypeName); 147 } 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownForumMessageTypeName.name(), forumMessageTypeName); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER); 154 } 155 } else { 156 addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName); 157 } 158 } 159 } 160 } 161 } 162 } 163 } 164 } 165 166 return result; 167 } 168}