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