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.GetForumMessageResultsForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.forum.server.control.ForumMessageControl;
022import com.echothree.model.control.search.common.SearchKinds;
023import com.echothree.model.control.search.server.control.SearchControl;
024import com.echothree.model.control.search.server.logic.SearchLogic;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.persistence.Session;
032import java.util.Arrays;
033import java.util.Collections;
034import java.util.List;
035import javax.enterprise.context.RequestScoped;
036
037@RequestScoped
038public class GetForumMessageResultsCommand
039        extends BaseSimpleCommand<GetForumMessageResultsForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null)
046                ));
047    }
048
049    /** Creates a new instance of GetForumMessageResultsCommand */
050    public GetForumMessageResultsCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var result = SearchResultFactory.getGetForumMessageResultsResult();
057        
058        setupPreferredClobMimeType();
059        
060        if(!hasExecutionErrors()) {
061            var searchControl = Session.getModelController(SearchControl.class);
062            var searchKind = searchControl.getSearchKindByName(SearchKinds.FORUM_MESSAGE.name());
063
064            if(searchKind != null) {
065                var searchTypeName = form.getSearchTypeName();
066                var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName);
067
068                if(searchType != null) {
069                    var userVisit = getUserVisit();
070                    var userVisitSearch = searchControl.getUserVisitSearch(userVisit, searchType);
071
072                    if(userVisitSearch != null) {
073                        var forumMessageControl = Session.getModelController(ForumMessageControl.class);
074
075                        if(session.hasLimit(com.echothree.model.data.search.server.factory.SearchResultFactory.class)) {
076                            result.setForumMessageResultCount(SearchLogic.getInstance().countSearchResults(userVisitSearch.getSearch()));
077                        }
078
079                        result.setForumMessageResults(forumMessageControl.getForumMessageResultTransfers(userVisit, userVisitSearch));
080                    } else {
081                        addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name());
082                    }
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.FORUM_MESSAGE.name(), searchTypeName);
085                }
086            } else {
087                addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.FORUM_MESSAGE.name());
088            }
089        }
090        
091        return result;
092    }
093}