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