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.model.control.search.server.graphql;
018
019import com.echothree.model.control.search.common.transfer.CheckSpellingWordTransfer;
020import com.echothree.model.control.search.server.control.SearchControl;
021import com.echothree.util.server.persistence.Session;
022import graphql.annotations.annotationTypes.GraphQLDescription;
023import graphql.annotations.annotationTypes.GraphQLField;
024import graphql.annotations.annotationTypes.GraphQLName;
025import graphql.annotations.annotationTypes.GraphQLNonNull;
026import graphql.schema.DataFetchingEnvironment;
027import java.util.ArrayList;
028import static java.util.Collections.emptyList;
029import java.util.List;
030
031@GraphQLDescription("check spelling word")
032@GraphQLName("CheckSpellingWord")
033public class CheckSpellingWordObject {
034
035    private final CheckSpellingWordTransfer checkSpellingWord;
036
037    public CheckSpellingWordObject(CheckSpellingWordTransfer checkSpellingWord) {
038        this.checkSpellingWord = checkSpellingWord;
039    }
040
041    @GraphQLField
042    @GraphQLDescription("word")
043    @GraphQLNonNull
044    public String getWord() {
045        return checkSpellingWord.getWord();
046    }
047
048    @GraphQLField
049    @GraphQLDescription("search check spelling action type")
050    public SearchCheckSpellingActionTypeObject getSearchCheckSpellingActionType(final DataFetchingEnvironment env) {
051        SearchCheckSpellingActionTypeObject object;
052
053        if(SearchSecurityUtils.getHasSearchCheckSpellingActionTypeAccess(env)) {
054            var searchControl = Session.getModelController(SearchControl.class);
055            var entity = searchControl.getSearchCheckSpellingActionTypeByName(
056                    checkSpellingWord.getSearchCheckSpellingActionType().getSearchCheckSpellingActionTypeName());
057
058            object = new SearchCheckSpellingActionTypeObject(entity);
059        } else {
060            object = null;
061        }
062
063        return object;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("check spelling suggestions")
068    @GraphQLNonNull
069    public List<CheckSpellingSuggestionObject> getCheckSpellingSuggestions(final DataFetchingEnvironment env) {
070        List<CheckSpellingSuggestionObject> objects;
071        var checkSpellingSuggestions = checkSpellingWord.getCheckSpellingSuggestions().getList();
072
073        if(checkSpellingSuggestions == null) {
074            objects = emptyList();
075        } else {
076            objects = new ArrayList<>(checkSpellingSuggestions.size());
077
078            checkSpellingSuggestions.stream()
079                    .map(CheckSpellingSuggestionObject::new)
080                    .forEachOrdered(objects::add);
081        }
082
083        return objects;
084    }
085
086}