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.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 java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
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    @Inject
051    SearchControl searchControl;
052
053    @Inject
054    LanguageLogic languageLogic;
055
056    @Inject
057    SearchLogic searchLogic;
058
059    /** Creates a new instance of CheckItemSpellingCommand */
060    public CheckItemSpellingCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, false);
062    }
063    
064    @Override
065    protected BaseResult execute() {
066        var result = SearchResultFactory.getCheckItemSpellingResult();
067        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.ITEM.name());
068
069        if(!hasExecutionErrors()) {
070            var searchTypeName = form.getSearchTypeName();
071            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
072
073            if(!hasExecutionErrors()) {
074                var languageIsoName = form.getLanguageIsoName();
075                var language = languageIsoName == null ? null : languageLogic.getLanguageByName(this, languageIsoName);
076
077                if(!hasExecutionErrors()) {
078                    var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType);
079                    var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail();
080                    var searchDefaultOperatorName = form.getSearchDefaultOperatorName();
081                    var searchDefaultOperator = searchDefaultOperatorName == null
082                            ? getDefaultSearchDefaultOperator(searchLogic, false, partySearchTypePreferenceDetail)
083                            : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName);
084
085                    if(!hasExecutionErrors()) {
086                        var itemSpellCheckEvaluator = new ItemSpellCheckEvaluator(getUserVisit(), language, searchDefaultOperator);
087
088                        itemSpellCheckEvaluator.setQ(this, form.getQ());
089
090                        result.setCheckSpellingWords(itemSpellCheckEvaluator.checkSpelling(this));
091                    }
092                }
093            }
094        }
095
096        return result;
097    }
098
099}