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.CheckItemSpellingForm;
020import com.echothree.control.user.search.common.result.CheckItemSpellingResult;
021import com.echothree.control.user.search.common.result.SearchResultFactory;
022import com.echothree.model.control.item.server.search.ItemSpellCheckEvaluator;
023import com.echothree.model.control.party.server.logic.LanguageLogic;
024import com.echothree.model.control.search.common.SearchKinds;
025import com.echothree.model.control.search.server.control.SearchControl;
026import com.echothree.model.control.search.server.logic.SearchLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.persistence.Session;
032import java.util.List;
033import javax.enterprise.context.RequestScoped;
034
035@RequestScoped
036public class CheckItemSpellingCommand
037        extends BaseCheckSpellingCommand<CheckItemSpellingForm, CheckItemSpellingResult> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("Q", FieldType.STRING, false, null, null)
047        );
048    }
049
050    /** Creates a new instance of CheckItemSpellingCommand */
051    public CheckItemSpellingCommand() {
052        super(null, FORM_FIELD_DEFINITIONS, false);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var result = SearchResultFactory.getCheckItemSpellingResult();
058        var searchLogic = SearchLogic.getInstance();
059        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.ITEM.name());
060
061        if(!hasExecutionErrors()) {
062            var searchTypeName = form.getSearchTypeName();
063            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
064
065            if(!hasExecutionErrors()) {
066                var languageIsoName = form.getLanguageIsoName();
067                var language = languageIsoName == null ? null : LanguageLogic.getInstance().getLanguageByName(this, languageIsoName);
068
069                if(!hasExecutionErrors()) {
070                    var searchControl = Session.getModelController(SearchControl.class);
071                    var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType);
072                    var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail();
073                    var searchDefaultOperatorName = form.getSearchDefaultOperatorName();
074                    var searchDefaultOperator = searchDefaultOperatorName == null
075                            ? getDefaultSearchDefaultOperator(searchLogic, false, partySearchTypePreferenceDetail)
076                            : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName);
077
078                    if(!hasExecutionErrors()) {
079                        var itemSpellCheckEvaluator = new ItemSpellCheckEvaluator(getUserVisit(), language, searchDefaultOperator);
080
081                        itemSpellCheckEvaluator.setQ(this, form.getQ());
082
083                        result.setCheckSpellingWords(itemSpellCheckEvaluator.checkSpelling(this));
084                    }
085                }
086            }
087        }
088
089        return result;
090    }
091
092}