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.model.control.search.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.graphql.count.Connections;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.search.server.control.SearchControl;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.data.search.common.SearchSortOrderConstants;
029import com.echothree.model.data.search.common.SearchTypeConstants;
030import com.echothree.model.data.search.server.entity.SearchKind;
031import com.echothree.model.data.search.server.entity.SearchKindDetail;
032import com.echothree.util.server.persistence.Session;
033import graphql.annotations.annotationTypes.GraphQLDescription;
034import graphql.annotations.annotationTypes.GraphQLField;
035import graphql.annotations.annotationTypes.GraphQLName;
036import graphql.annotations.annotationTypes.GraphQLNonNull;
037import graphql.annotations.connection.GraphQLConnection;
038import graphql.schema.DataFetchingEnvironment;
039import java.util.ArrayList;
040import java.util.stream.Collectors;
041
042@GraphQLDescription("search kind object")
043@GraphQLName("SearchKind")
044public class SearchKindObject
045        extends BaseEntityInstanceObject {
046
047    private final SearchKind searchKind; // Always Present
048
049    public SearchKindObject(SearchKind searchKind) {
050        super(searchKind.getPrimaryKey());
051        
052        this.searchKind = searchKind;
053    }
054
055    private SearchKindDetail searchKindDetail; // Optional, use getSearchKindDetail()
056    
057    private SearchKindDetail getSearchKindDetail() {
058        if(searchKindDetail == null) {
059            searchKindDetail = searchKind.getLastDetail();
060        }
061        
062        return searchKindDetail;
063    }
064    
065    @GraphQLField
066    @GraphQLDescription("search kind name")
067    @GraphQLNonNull
068    public String getSearchKindName() {
069        return getSearchKindDetail().getSearchKindName();
070    }
071    
072    @GraphQLField
073    @GraphQLDescription("is default")
074    @GraphQLNonNull
075    public boolean getIsDefault() {
076        return getSearchKindDetail().getIsDefault();
077    }
078    
079    @GraphQLField
080    @GraphQLDescription("sort order")
081    @GraphQLNonNull
082    public int getSortOrder() {
083        return getSearchKindDetail().getSortOrder();
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("description")
088    @GraphQLNonNull
089    public String getDescription(final DataFetchingEnvironment env) {
090        var searchControl = Session.getModelController(SearchControl.class);
091        var userControl = Session.getModelController(UserControl.class);
092
093        return searchControl.getBestSearchKindDescription(searchKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
094    }
095
096    @GraphQLField
097    @GraphQLDescription("search types")
098    @GraphQLNonNull
099    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
100    public CountingPaginatedData<SearchTypeObject> getSearchTypes(final DataFetchingEnvironment env) {
101        if(SearchSecurityUtils.getHasSearchTypesAccess(env)) {
102            var searchControl = Session.getModelController(SearchControl.class);
103            var totalCount = searchControl.countSearchTypesBySearchKind(searchKind);
104
105            try(var objectLimiter = new ObjectLimiter(env, SearchTypeConstants.COMPONENT_VENDOR_NAME, SearchTypeConstants.ENTITY_TYPE_NAME, totalCount)) {
106                var entities = searchControl.getSearchTypes(searchKind);
107                var searchTypes = entities.stream().map(SearchTypeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
108
109                return new CountedObjects<>(objectLimiter, searchTypes);
110            }
111        } else {
112            return Connections.emptyConnection();
113        }
114    }
115
116    @GraphQLField
117    @GraphQLDescription("search sort orders")
118    @GraphQLNonNull
119    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
120    public CountingPaginatedData<SearchSortOrderObject> getSearchSortOrders(final DataFetchingEnvironment env) {
121        if(SearchSecurityUtils.getHasSearchSortOrdersAccess(env)) {
122            var searchControl = Session.getModelController(SearchControl.class);
123            var totalCount = searchControl.countSearchSortOrdersBySearchKind(searchKind);
124
125            try(var objectLimiter = new ObjectLimiter(env, SearchSortOrderConstants.COMPONENT_VENDOR_NAME, SearchSortOrderConstants.ENTITY_TYPE_NAME, totalCount)) {
126                var entities = searchControl.getSearchSortOrders(searchKind);
127                var searchSortOrders = entities.stream().map(SearchSortOrderObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
128
129                return new CountedObjects<>(objectLimiter, searchSortOrders);
130            }
131        } else {
132            return Connections.emptyConnection();
133        }
134    }
135
136}