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