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