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